Home | History | Annotate | Line # | Download | only in usbhidaction
usbhidaction.c revision 1.5
      1 /*      $NetBSD: usbhidaction.c,v 1.5 2001/12/29 22:15:32 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 
    212 	f = fopen(conf, "r");
    213 	if (f == NULL)
    214 		err(1, "%s", conf);
    215 
    216 	for (line = 1; ; line++) {
    217 		if (fgets(buf, sizeof buf, f) == NULL)
    218 			break;
    219 		if (buf[0] == '#' || buf[0] == '\n')
    220 			continue;
    221 		p = strchr(buf, '\n');
    222 		while (p && isspace(peek(f))) {
    223 			if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
    224 				break;
    225 			p = strchr(buf, '\n');
    226 		}
    227 		if (p)
    228 			*p = 0;
    229 		if (sscanf(buf, "%s %s %[^\n]", name, value, action) != 3) {
    230 			errx(1, "config file `%s', line %d, syntax error: %s",
    231 			     conf, line, buf);
    232 		}
    233 
    234 		cmd = malloc(sizeof *cmd);
    235 		if (cmd == NULL)
    236 			err(1, "malloc failed");
    237 		cmd->next = commands;
    238 		commands = cmd;
    239 		cmd->line = line;
    240 
    241 		if (strcmp(value, "*") == 0) {
    242 			cmd->anyvalue = 1;
    243 		} else {
    244 			cmd->anyvalue = 0;
    245 			if (sscanf(value, "%d", &cmd->value) != 1)
    246 				errx(1, "config file `%s', line %d, "
    247 				     "bad value: %s\n", conf, line, value);
    248 		}
    249 
    250 		coll[0] = 0;
    251 		for (d = hid_start_parse(repd, 1 << hid_input, reportid);
    252 		     hid_get_item(d, &h); ) {
    253 			if (verbose > 2)
    254 				printf("kind=%d usage=%x\n", h.kind, h.usage);
    255 			if (h.flags & HIO_CONST)
    256 				continue;
    257 			switch (h.kind) {
    258 			case hid_input:
    259 				snprintf(usage, sizeof usage,  "%s:%s",
    260 					 hid_usage_page(HID_PAGE(h.usage)),
    261 					 hid_usage_in_page(h.usage));
    262 				if (verbose > 2)
    263 					printf("usage %s\n", usage);
    264 				if (strcasecmp(usage, name) == 0)
    265 					goto foundhid;
    266 				if (coll[0]) {
    267 					snprintf(usage, sizeof usage,
    268 					    "%s.%s:%s", coll+1,
    269 					    hid_usage_page(HID_PAGE(h.usage)),
    270 					    hid_usage_in_page(h.usage));
    271 					if (verbose > 2)
    272 						printf("usage %s\n", usage);
    273 					if (strcasecmp(usage, name) == 0)
    274 						goto foundhid;
    275 				}
    276 				break;
    277 			case hid_collection:
    278 				snprintf(coll + strlen(coll),
    279 				    sizeof coll - strlen(coll),  ".%s:%s",
    280 				    hid_usage_page(HID_PAGE(h.usage)),
    281 				    hid_usage_in_page(h.usage));
    282 				break;
    283 			case hid_endcollection:
    284 				if (coll[0])
    285 					*strrchr(coll, '.') = 0;
    286 				break;
    287 			default:
    288 				break;
    289 			}
    290 		}
    291 		if (ignore) {
    292 			if (verbose)
    293 				warnx("ignore item '%s'\n", name);
    294 			continue;
    295 		}
    296 		errx(1, "config file `%s', line %d, HID item not found: `%s'\n",
    297 		     conf, line, name);
    298 
    299 	foundhid:
    300 		hid_end_parse(d);
    301 		cmd->item = h;
    302 		cmd->name = strdup(name);
    303 		cmd->action = strdup(action);
    304 
    305 		if (verbose)
    306 			printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
    307 			       cmd->value, cmd->action);
    308 	}
    309 	fclose(f);
    310 }
    311 
    312 void
    313 docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
    314 {
    315 	char cmdbuf[SIZE], *p, *q;
    316 	size_t len;
    317 	int n, r;
    318 
    319 	for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
    320 		if (*p == '$') {
    321 			p++;
    322 			len = &cmdbuf[SIZE-1] - q;
    323 			if (isdigit(*p)) {
    324 				n = strtol(p, &p, 10) - 1;
    325 				if (n >= 0 && n < argc) {
    326 					strncpy(q, argv[n], len);
    327 					q += strlen(q);
    328 				}
    329 			} else if (*p == 'V') {
    330 				p++;
    331 				snprintf(q, len, "%d", value);
    332 				q += strlen(q);
    333 			} else if (*p == 'N') {
    334 				p++;
    335 				strncpy(q, cmd->name, len);
    336 				q += strlen(q);
    337 			} else if (*p == 'H') {
    338 				p++;
    339 				strncpy(q, hid, len);
    340 				q += strlen(q);
    341 			} else if (*p) {
    342 				*q++ = *p++;
    343 			}
    344 		} else {
    345 			*q++ = *p++;
    346 		}
    347 	}
    348 	*q = 0;
    349 
    350 	if (verbose)
    351 		printf("system '%s'\n", cmdbuf);
    352 	r = system(cmdbuf);
    353 	if (verbose > 1 && r)
    354 		printf("return code = 0x%x\n", r);
    355 }
    356