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