usbhidaction.c revision 1.12 1 /* $NetBSD: usbhidaction.c,v 1.12 2004/10/10 01:16:17 mrg 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.12 2004/10/10 01:16:17 mrg 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 = 1;
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
103 demon = 1;
104 ignore = 0;
105 while ((ch = getopt(argc, argv, "c:df:iv")) != -1) {
106 switch(ch) {
107 case 'c':
108 conf = optarg;
109 break;
110 case 'd':
111 demon ^= 1;
112 break;
113 case 'i':
114 ignore++;
115 break;
116 case 'f':
117 dev = optarg;
118 break;
119 case 'v':
120 demon = 0;
121 verbose++;
122 break;
123 case '?':
124 default:
125 usage();
126 }
127 }
128 argc -= optind;
129 argv += optind;
130
131 if (conf == NULL || dev == NULL)
132 usage();
133
134 hid_init(NULL);
135
136 if (dev[0] != '/') {
137 snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
138 isdigit(dev[0]) ? "uhid" : "", dev);
139 dev = devnamebuf;
140 }
141
142 fd = open(dev, O_RDWR);
143 if (fd < 0)
144 err(1, "%s", dev);
145 if (ioctl(fd, USB_GET_REPORT_ID, &reportid) < 0)
146 reportid = -1;
147 repd = hid_get_report_desc(fd);
148 if (repd == NULL)
149 err(1, "hid_get_report_desc() failed");
150
151 commands = parse_conf(conf, repd, reportid, ignore);
152
153 sz = hid_report_size(repd, hid_input, reportid);
154
155 if (verbose)
156 printf("report size %d\n", sz);
157 if (sz > sizeof buf)
158 errx(1, "report too large");
159
160 (void)signal(SIGHUP, sighup);
161
162 if (demon) {
163 if (daemon(0, 0) < 0)
164 err(1, "daemon()");
165 pidfile(NULL);
166 isdemon = 1;
167 }
168
169 for(;;) {
170 n = read(fd, buf, sz);
171 if (verbose > 2) {
172 printf("read %d bytes:", n);
173 for (i = 0; i < n; i++)
174 printf(" %02x", buf[i]);
175 printf("\n");
176 }
177 if (n < 0) {
178 if (verbose)
179 err(1, "read");
180 else
181 exit(1);
182 }
183 #if 0
184 if (n != sz) {
185 err(2, "read size");
186 }
187 #endif
188 for (cmd = commands; cmd; cmd = cmd->next) {
189 val = hid_get_data(buf, &cmd->item);
190 if (cmd->value == val || cmd->anyvalue)
191 docmd(cmd, val, dev, argc, argv);
192 }
193 if (reparse) {
194 struct command *cmds =
195 parse_conf(conf, repd, reportid, ignore);
196 if (cmds) {
197 freecommands(commands);
198 commands = cmds;
199 }
200 reparse = 0;
201 }
202 }
203
204 exit(0);
205 }
206
207 void
208 usage(void)
209 {
210
211 fprintf(stderr, "usage: %s -c config_file [-d] -f hid_dev "
212 "[-i] [-v]\n", getprogname());
213 exit(1);
214 }
215
216 static int
217 peek(FILE *f)
218 {
219 int c;
220
221 c = getc(f);
222 if (c != EOF)
223 ungetc(c, f);
224 return c;
225 }
226
227 struct command *
228 parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
229 {
230 FILE *f;
231 char *p;
232 int line;
233 char buf[SIZE], name[SIZE], value[SIZE], action[SIZE];
234 char usage[SIZE], coll[SIZE];
235 struct command *cmd, *cmds;
236 struct hid_data *d;
237 struct hid_item h;
238 int u, lo, hi, range;
239
240
241 f = fopen(conf, "r");
242 if (f == NULL)
243 err(1, "%s", conf);
244
245 cmds = NULL;
246 for (line = 1; ; line++) {
247 if (fgets(buf, sizeof buf, f) == NULL)
248 break;
249 if (buf[0] == '#' || buf[0] == '\n')
250 continue;
251 p = strchr(buf, '\n');
252 while (p && isspace(peek(f))) {
253 if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
254 break;
255 p = strchr(buf, '\n');
256 }
257 if (p)
258 *p = 0;
259 /* XXX SIZE == 4000 */
260 if (sscanf(buf, "%3999s %3999s %[^\n]", name, value, action) != 3) {
261 if (isdemon) {
262 syslog(LOG_WARNING, "config file `%s', line %d"
263 ", syntax error: %s", conf, line, buf);
264 freecommands(cmds);
265 return (NULL);
266 } else {
267 errx(1, "config file `%s', line %d,"
268 ", syntax error: %s", conf, line, buf);
269 }
270 }
271
272 cmd = malloc(sizeof *cmd);
273 if (cmd == NULL)
274 err(1, "malloc failed");
275 cmd->next = cmds;
276 cmds = cmd;
277 cmd->line = line;
278
279 if (strcmp(value, "*") == 0) {
280 cmd->anyvalue = 1;
281 } else {
282 cmd->anyvalue = 0;
283 if (sscanf(value, "%d", &cmd->value) != 1) {
284 if (isdemon) {
285 syslog(LOG_WARNING,
286 "config file `%s', line %d, "
287 "bad value: %s\n",
288 conf, line, value);
289 freecommands(cmds);
290 return (NULL);
291 } else {
292 errx(1, "config file `%s', line %d, "
293 "bad value: %s\n",
294 conf, line, value);
295 }
296 }
297 }
298
299 coll[0] = 0;
300 for (d = hid_start_parse(repd, 1 << hid_input, reportid);
301 hid_get_item(d, &h); ) {
302 if (verbose > 2)
303 printf("kind=%d usage=%x\n", h.kind, h.usage);
304 if (h.flags & HIO_CONST)
305 continue;
306 switch (h.kind) {
307 case hid_input:
308 if (h.usage_minimum != 0 ||
309 h.usage_maximum != 0) {
310 lo = h.usage_minimum;
311 hi = h.usage_maximum;
312 range = 1;
313 } else {
314 lo = h.usage;
315 hi = h.usage;
316 range = 0;
317 }
318 for (u = lo; u <= hi; u++) {
319 snprintf(usage, sizeof usage, "%s:%s",
320 hid_usage_page(HID_PAGE(u)),
321 hid_usage_in_page(u));
322 if (verbose > 2)
323 printf("usage %s\n", usage);
324 if (!strcasecmp(usage, name))
325 goto foundhid;
326 if (coll[0]) {
327 snprintf(usage, sizeof usage,
328 "%s.%s:%s", coll+1,
329 hid_usage_page(HID_PAGE(u)),
330 hid_usage_in_page(u));
331 if (verbose > 2)
332 printf("usage %s\n",
333 usage);
334 if (!strcasecmp(usage, name))
335 goto foundhid;
336 }
337 }
338 break;
339 case hid_collection:
340 snprintf(coll + strlen(coll),
341 sizeof coll - strlen(coll), ".%s:%s",
342 hid_usage_page(HID_PAGE(h.usage)),
343 hid_usage_in_page(h.usage));
344 break;
345 case hid_endcollection:
346 if (coll[0])
347 *strrchr(coll, '.') = 0;
348 break;
349 default:
350 break;
351 }
352 }
353 if (ignore) {
354 if (verbose)
355 warnx("ignore item '%s'", name);
356 continue;
357 }
358 if (isdemon) {
359 syslog(LOG_WARNING, "config file `%s', line %d, HID "
360 "item not found: `%s'", conf, line, name);
361 freecommands(cmds);
362 return (NULL);
363 } else {
364 errx(1, "config file `%s', line %d, HID item "
365 "not found: `%s'", conf, line, name);
366 }
367
368 foundhid:
369 hid_end_parse(d);
370 cmd->item = h;
371 cmd->name = strdup(name);
372 cmd->action = strdup(action);
373 if (range) {
374 if (cmd->value == 1)
375 cmd->value = u - lo;
376 else
377 cmd->value = -1;
378 }
379
380 if (verbose)
381 printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
382 cmd->value, cmd->action);
383 }
384 fclose(f);
385 return (cmds);
386 }
387
388 void
389 docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
390 {
391 char cmdbuf[SIZE], *p, *q;
392 size_t len;
393 int n, r;
394
395 for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
396 if (*p == '$') {
397 p++;
398 len = &cmdbuf[SIZE-1] - q;
399 if (isdigit(*p)) {
400 n = strtol(p, &p, 10) - 1;
401 if (n >= 0 && n < argc) {
402 strncpy(q, argv[n], len);
403 q += strlen(q);
404 }
405 } else if (*p == 'V') {
406 p++;
407 snprintf(q, len, "%d", value);
408 q += strlen(q);
409 } else if (*p == 'N') {
410 p++;
411 strncpy(q, cmd->name, len);
412 q += strlen(q);
413 } else if (*p == 'H') {
414 p++;
415 strncpy(q, hid, len);
416 q += strlen(q);
417 } else if (*p) {
418 *q++ = *p++;
419 }
420 } else {
421 *q++ = *p++;
422 }
423 }
424 *q = 0;
425
426 if (verbose)
427 printf("system '%s'\n", cmdbuf);
428 r = system(cmdbuf);
429 if (verbose > 1 && r)
430 printf("return code = 0x%x\n", r);
431 }
432
433 void
434 freecommands(struct command *cmd)
435 {
436 struct command *next;
437
438 while (cmd) {
439 next = cmd->next;
440 free(cmd);
441 cmd = next;
442 }
443 }
444