usbhidaction.c revision 1.13 1 /* $NetBSD: usbhidaction.c,v 1.13 2004/10/20 13:49:15 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.13 2004/10/20 13:49:15 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
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 flags=%x\n",
304 h.kind, h.usage, h.flags);
305 if ((h.flags & HIO_CONST) && h.kind != hid_collection)
306 continue;
307 switch (h.kind) {
308 case hid_input:
309 if (h.usage_minimum != 0 ||
310 h.usage_maximum != 0) {
311 lo = h.usage_minimum;
312 hi = h.usage_maximum;
313 range = 1;
314 } else {
315 lo = h.usage;
316 hi = h.usage;
317 range = 0;
318 }
319 for (u = lo; u <= hi; u++) {
320 snprintf(usage, sizeof usage, "%s:%s",
321 hid_usage_page(HID_PAGE(u)),
322 hid_usage_in_page(u));
323 if (verbose > 2)
324 printf("usage %s\n", usage);
325 if (!strcasecmp(usage, name))
326 goto foundhid;
327 if (coll[0]) {
328 snprintf(usage, sizeof usage,
329 "%s.%s:%s", coll+1,
330 hid_usage_page(HID_PAGE(u)),
331 hid_usage_in_page(u));
332 if (verbose > 2)
333 printf("usage %s\n",
334 usage);
335 if (!strcasecmp(usage, name))
336 goto foundhid;
337 }
338 }
339 break;
340 case hid_collection:
341 snprintf(coll + strlen(coll),
342 sizeof coll - strlen(coll), ".%s:%s",
343 hid_usage_page(HID_PAGE(h.usage)),
344 hid_usage_in_page(h.usage));
345 if (verbose > 2)
346 printf("coll '%s'\n", coll);
347 break;
348 case hid_endcollection:
349 if (coll[0])
350 *strrchr(coll, '.') = 0;
351 break;
352 default:
353 break;
354 }
355 }
356 if (ignore) {
357 if (verbose)
358 warnx("ignore item '%s'", name);
359 continue;
360 }
361 if (isdemon) {
362 syslog(LOG_WARNING, "config file `%s', line %d, HID "
363 "item not found: `%s'", conf, line, name);
364 freecommands(cmds);
365 return (NULL);
366 } else {
367 errx(1, "config file `%s', line %d, HID item "
368 "not found: `%s'", conf, line, name);
369 }
370
371 foundhid:
372 hid_end_parse(d);
373 cmd->item = h;
374 cmd->name = strdup(name);
375 cmd->action = strdup(action);
376 if (range) {
377 if (cmd->value == 1)
378 cmd->value = u - lo;
379 else
380 cmd->value = -1;
381 }
382
383 if (verbose)
384 printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
385 cmd->value, cmd->action);
386 }
387 fclose(f);
388 return (cmds);
389 }
390
391 void
392 docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
393 {
394 char cmdbuf[SIZE], *p, *q;
395 size_t len;
396 int n, r;
397
398 for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
399 if (*p == '$') {
400 p++;
401 len = &cmdbuf[SIZE-1] - q;
402 if (isdigit(*p)) {
403 n = strtol(p, &p, 10) - 1;
404 if (n >= 0 && n < argc) {
405 strncpy(q, argv[n], len);
406 q += strlen(q);
407 }
408 } else if (*p == 'V') {
409 p++;
410 snprintf(q, len, "%d", value);
411 q += strlen(q);
412 } else if (*p == 'N') {
413 p++;
414 strncpy(q, cmd->name, len);
415 q += strlen(q);
416 } else if (*p == 'H') {
417 p++;
418 strncpy(q, hid, len);
419 q += strlen(q);
420 } else if (*p) {
421 *q++ = *p++;
422 }
423 } else {
424 *q++ = *p++;
425 }
426 }
427 *q = 0;
428
429 if (verbose)
430 printf("system '%s'\n", cmdbuf);
431 r = system(cmdbuf);
432 if (verbose > 1 && r)
433 printf("return code = 0x%x\n", r);
434 }
435
436 void
437 freecommands(struct command *cmd)
438 {
439 struct command *next;
440
441 while (cmd) {
442 next = cmd->next;
443 free(cmd);
444 cmd = next;
445 }
446 }
447