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