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