Home | History | Annotate | Line # | Download | only in usbhidaction
usbhidaction.c revision 1.6
      1  1.6  augustss /*      $NetBSD: usbhidaction.c,v 1.6 2001/12/29 23:17:50 augustss Exp $ */
      2  1.2  augustss 
      3  1.2  augustss /*
      4  1.2  augustss  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.2  augustss  * All rights reserved.
      6  1.2  augustss  *
      7  1.2  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2  augustss  * by Lennart Augustsson <lennart (at) augustsson.net>.
      9  1.2  augustss  *
     10  1.2  augustss  * Redistribution and use in source and binary forms, with or without
     11  1.2  augustss  * modification, are permitted provided that the following conditions
     12  1.2  augustss  * are met:
     13  1.2  augustss  * 1. Redistributions of source code must retain the above copyright
     14  1.2  augustss  *    notice, this list of conditions and the following disclaimer.
     15  1.2  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2  augustss  *    notice, this list of conditions and the following disclaimer in the
     17  1.2  augustss  *    documentation and/or other materials provided with the distribution.
     18  1.2  augustss  * 3. All advertising materials mentioning features or use of this software
     19  1.2  augustss  *    must display the following acknowledgement:
     20  1.2  augustss  *        This product includes software developed by the NetBSD
     21  1.2  augustss  *        Foundation, Inc. and its contributors.
     22  1.2  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.2  augustss  *    contributors may be used to endorse or promote products derived
     24  1.2  augustss  *    from this software without specific prior written permission.
     25  1.2  augustss  *
     26  1.2  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.2  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.2  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.2  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.2  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.2  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.2  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.2  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.2  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.2  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.2  augustss  * POSSIBILITY OF SUCH DAMAGE.
     37  1.2  augustss  */
     38  1.2  augustss 
     39  1.1  augustss #include <stdio.h>
     40  1.1  augustss #include <stdlib.h>
     41  1.1  augustss #include <string.h>
     42  1.1  augustss #include <ctype.h>
     43  1.1  augustss #include <err.h>
     44  1.1  augustss #include <fcntl.h>
     45  1.5  augustss #include <limits.h>
     46  1.1  augustss #include <unistd.h>
     47  1.1  augustss #include <sys/types.h>
     48  1.1  augustss #include <sys/ioctl.h>
     49  1.1  augustss #include <dev/usb/usb.h>
     50  1.1  augustss #include <dev/usb/usbhid.h>
     51  1.4  augustss #include <usbhid.h>
     52  1.1  augustss #include <util.h>
     53  1.1  augustss 
     54  1.1  augustss int verbose = 0;
     55  1.1  augustss 
     56  1.1  augustss struct command {
     57  1.1  augustss 	struct command *next;
     58  1.1  augustss 	int line;
     59  1.1  augustss 
     60  1.1  augustss 	struct hid_item item;
     61  1.1  augustss 	int value;
     62  1.1  augustss 	char anyvalue;
     63  1.1  augustss 	char *name;
     64  1.1  augustss 	char *action;
     65  1.1  augustss };
     66  1.1  augustss struct command *commands;
     67  1.1  augustss 
     68  1.1  augustss #define SIZE 4000
     69  1.1  augustss 
     70  1.1  augustss void usage(void);
     71  1.5  augustss void parse_conf(const char *, report_desc_t, int, int);
     72  1.5  augustss void docmd(struct command *, int, const char *, int, char **);
     73  1.1  augustss 
     74  1.1  augustss int
     75  1.1  augustss main(int argc, char **argv)
     76  1.1  augustss {
     77  1.1  augustss 	const char *conf = NULL;
     78  1.5  augustss 	const char *dev = NULL;
     79  1.5  augustss 	int fd, ch, sz, n, val, i;
     80  1.1  augustss 	int demon, ignore;
     81  1.1  augustss 	report_desc_t repd;
     82  1.1  augustss 	char buf[100];
     83  1.5  augustss 	char devnamebuf[PATH_MAX];
     84  1.1  augustss 	struct command *cmd;
     85  1.4  augustss 	int reportid;
     86  1.1  augustss 
     87  1.1  augustss 	demon = 1;
     88  1.1  augustss 	ignore = 0;
     89  1.5  augustss 	while ((ch = getopt(argc, argv, "c:df:iv")) != -1) {
     90  1.1  augustss 		switch(ch) {
     91  1.1  augustss 		case 'c':
     92  1.1  augustss 			conf = optarg;
     93  1.1  augustss 			break;
     94  1.1  augustss 		case 'd':
     95  1.1  augustss 			demon ^= 1;
     96  1.1  augustss 			break;
     97  1.1  augustss 		case 'i':
     98  1.1  augustss 			ignore++;
     99  1.1  augustss 			break;
    100  1.1  augustss 		case 'f':
    101  1.5  augustss 			dev = optarg;
    102  1.1  augustss 			break;
    103  1.1  augustss 		case 'v':
    104  1.1  augustss 			demon = 0;
    105  1.1  augustss 			verbose++;
    106  1.1  augustss 			break;
    107  1.1  augustss 		case '?':
    108  1.1  augustss 		default:
    109  1.1  augustss 			usage();
    110  1.1  augustss 		}
    111  1.1  augustss 	}
    112  1.1  augustss 	argc -= optind;
    113  1.1  augustss 	argv += optind;
    114  1.1  augustss 
    115  1.5  augustss 	if (conf == NULL || dev == NULL)
    116  1.1  augustss 		usage();
    117  1.1  augustss 
    118  1.1  augustss 	hid_init(NULL);
    119  1.1  augustss 
    120  1.5  augustss 	if (dev[0] != '/') {
    121  1.5  augustss 		snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
    122  1.5  augustss 			 isdigit(dev[0]) ? "uhid" : "", dev);
    123  1.5  augustss 		dev = devnamebuf;
    124  1.5  augustss 	}
    125  1.5  augustss 
    126  1.5  augustss 	fd = open(dev, O_RDWR);
    127  1.1  augustss 	if (fd < 0)
    128  1.5  augustss 		err(1, "%s", dev);
    129  1.4  augustss 	if (ioctl(fd, USB_GET_REPORT_ID, &reportid) < 0)
    130  1.4  augustss 		reportid = -1;
    131  1.1  augustss 	repd = hid_get_report_desc(fd);
    132  1.1  augustss 	if (repd == NULL)
    133  1.1  augustss 		err(1, "hid_get_report_desc() failed\n");
    134  1.1  augustss 
    135  1.4  augustss 	parse_conf(conf, repd, reportid, ignore);
    136  1.1  augustss 
    137  1.5  augustss 	sz = hid_report_size(repd, hid_input, reportid);
    138  1.1  augustss 	hid_dispose_report_desc(repd);
    139  1.1  augustss 
    140  1.1  augustss 	if (verbose)
    141  1.1  augustss 		printf("report size %d\n", sz);
    142  1.1  augustss 	if (sz > sizeof buf)
    143  1.1  augustss 		errx(1, "report too large");
    144  1.1  augustss 
    145  1.1  augustss 	if (demon) {
    146  1.1  augustss 		if (daemon(0, 0) < 0)
    147  1.1  augustss 			err(1, "daemon()");
    148  1.1  augustss 		pidfile(NULL);
    149  1.1  augustss 	}
    150  1.1  augustss 
    151  1.1  augustss 	for(;;) {
    152  1.1  augustss 		n = read(fd, buf, sz);
    153  1.5  augustss 		if (verbose > 2) {
    154  1.5  augustss 			printf("read %d bytes:", n);
    155  1.5  augustss 			for (i = 0; i < n; i++)
    156  1.5  augustss 				printf(" %02x", buf[i]);
    157  1.5  augustss 			printf("\n");
    158  1.5  augustss 		}
    159  1.1  augustss 		if (n < 0) {
    160  1.1  augustss 			if (verbose)
    161  1.1  augustss 				err(1, "read");
    162  1.1  augustss 			else
    163  1.1  augustss 				exit(1);
    164  1.1  augustss 		}
    165  1.5  augustss #if 0
    166  1.5  augustss 		if (n != sz) {
    167  1.5  augustss 			err(2, "read size");
    168  1.5  augustss 		}
    169  1.5  augustss #endif
    170  1.1  augustss 		for (cmd = commands; cmd; cmd = cmd->next) {
    171  1.1  augustss 			val = hid_get_data(buf, &cmd->item);
    172  1.1  augustss 			if (cmd->value == val || cmd->anyvalue)
    173  1.5  augustss 				docmd(cmd, val, dev, argc, argv);
    174  1.1  augustss 		}
    175  1.1  augustss 	}
    176  1.1  augustss 
    177  1.1  augustss 	exit(0);
    178  1.1  augustss }
    179  1.1  augustss 
    180  1.1  augustss void
    181  1.1  augustss usage(void)
    182  1.1  augustss {
    183  1.1  augustss 
    184  1.1  augustss 	fprintf(stderr, "Usage: %s -c config_file [-d] -f hid_dev "
    185  1.5  augustss 		"[-i] [-v]\n", getprogname());
    186  1.1  augustss 	exit(1);
    187  1.1  augustss }
    188  1.1  augustss 
    189  1.1  augustss static int
    190  1.1  augustss peek(FILE *f)
    191  1.1  augustss {
    192  1.1  augustss 	int c;
    193  1.1  augustss 
    194  1.1  augustss 	c = getc(f);
    195  1.1  augustss 	if (c != EOF)
    196  1.1  augustss 		ungetc(c, f);
    197  1.1  augustss 	return c;
    198  1.1  augustss }
    199  1.1  augustss 
    200  1.1  augustss void
    201  1.4  augustss parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
    202  1.1  augustss {
    203  1.1  augustss 	FILE *f;
    204  1.1  augustss 	char *p;
    205  1.1  augustss 	int line;
    206  1.1  augustss 	char buf[SIZE], name[SIZE], value[SIZE], action[SIZE];
    207  1.1  augustss 	char usage[SIZE], coll[SIZE];
    208  1.1  augustss 	struct command *cmd;
    209  1.1  augustss 	struct hid_data *d;
    210  1.1  augustss 	struct hid_item h;
    211  1.6  augustss 	int u, lo, hi, range;
    212  1.1  augustss 
    213  1.1  augustss 	f = fopen(conf, "r");
    214  1.1  augustss 	if (f == NULL)
    215  1.1  augustss 		err(1, "%s", conf);
    216  1.1  augustss 
    217  1.1  augustss 	for (line = 1; ; line++) {
    218  1.1  augustss 		if (fgets(buf, sizeof buf, f) == NULL)
    219  1.1  augustss 			break;
    220  1.1  augustss 		if (buf[0] == '#' || buf[0] == '\n')
    221  1.1  augustss 			continue;
    222  1.1  augustss 		p = strchr(buf, '\n');
    223  1.1  augustss 		while (p && isspace(peek(f))) {
    224  1.1  augustss 			if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
    225  1.1  augustss 				break;
    226  1.1  augustss 			p = strchr(buf, '\n');
    227  1.1  augustss 		}
    228  1.1  augustss 		if (p)
    229  1.1  augustss 			*p = 0;
    230  1.1  augustss 		if (sscanf(buf, "%s %s %[^\n]", name, value, action) != 3) {
    231  1.1  augustss 			errx(1, "config file `%s', line %d, syntax error: %s",
    232  1.1  augustss 			     conf, line, buf);
    233  1.1  augustss 		}
    234  1.1  augustss 
    235  1.1  augustss 		cmd = malloc(sizeof *cmd);
    236  1.1  augustss 		if (cmd == NULL)
    237  1.1  augustss 			err(1, "malloc failed");
    238  1.1  augustss 		cmd->next = commands;
    239  1.1  augustss 		commands = cmd;
    240  1.1  augustss 		cmd->line = line;
    241  1.1  augustss 
    242  1.1  augustss 		if (strcmp(value, "*") == 0) {
    243  1.1  augustss 			cmd->anyvalue = 1;
    244  1.1  augustss 		} else {
    245  1.1  augustss 			cmd->anyvalue = 0;
    246  1.1  augustss 			if (sscanf(value, "%d", &cmd->value) != 1)
    247  1.1  augustss 				errx(1, "config file `%s', line %d, "
    248  1.1  augustss 				     "bad value: %s\n", conf, line, value);
    249  1.1  augustss 		}
    250  1.1  augustss 
    251  1.1  augustss 		coll[0] = 0;
    252  1.4  augustss 		for (d = hid_start_parse(repd, 1 << hid_input, reportid);
    253  1.1  augustss 		     hid_get_item(d, &h); ) {
    254  1.1  augustss 			if (verbose > 2)
    255  1.1  augustss 				printf("kind=%d usage=%x\n", h.kind, h.usage);
    256  1.1  augustss 			if (h.flags & HIO_CONST)
    257  1.1  augustss 				continue;
    258  1.1  augustss 			switch (h.kind) {
    259  1.1  augustss 			case hid_input:
    260  1.6  augustss 				if (h.usage_minimum != 0 ||
    261  1.6  augustss 				    h.usage_maximum != 0) {
    262  1.6  augustss 					lo = h.usage_minimum;
    263  1.6  augustss 					hi = h.usage_maximum;
    264  1.6  augustss 					range = 1;
    265  1.6  augustss 				} else {
    266  1.6  augustss 					lo = h.usage;
    267  1.6  augustss 					hi = h.usage;
    268  1.6  augustss 					range = 0;
    269  1.6  augustss 				}
    270  1.6  augustss 				for (u = lo; u <= hi; u++) {
    271  1.6  augustss 					snprintf(usage, sizeof usage,  "%s:%s",
    272  1.6  augustss 						 hid_usage_page(HID_PAGE(u)),
    273  1.6  augustss 						 hid_usage_in_page(u));
    274  1.1  augustss 					if (verbose > 2)
    275  1.1  augustss 						printf("usage %s\n", usage);
    276  1.6  augustss 					if (!strcasecmp(usage, name))
    277  1.1  augustss 						goto foundhid;
    278  1.6  augustss 					if (coll[0]) {
    279  1.6  augustss 						snprintf(usage, sizeof usage,
    280  1.6  augustss 						  "%s.%s:%s", coll+1,
    281  1.6  augustss 						  hid_usage_page(HID_PAGE(u)),
    282  1.6  augustss 						  hid_usage_in_page(u));
    283  1.6  augustss 						if (verbose > 2)
    284  1.6  augustss 							printf("usage %s\n",
    285  1.6  augustss 							       usage);
    286  1.6  augustss 						if (!strcasecmp(usage, name))
    287  1.6  augustss 							goto foundhid;
    288  1.6  augustss 					}
    289  1.1  augustss 				}
    290  1.1  augustss 				break;
    291  1.1  augustss 			case hid_collection:
    292  1.1  augustss 				snprintf(coll + strlen(coll),
    293  1.1  augustss 				    sizeof coll - strlen(coll),  ".%s:%s",
    294  1.1  augustss 				    hid_usage_page(HID_PAGE(h.usage)),
    295  1.1  augustss 				    hid_usage_in_page(h.usage));
    296  1.1  augustss 				break;
    297  1.1  augustss 			case hid_endcollection:
    298  1.1  augustss 				if (coll[0])
    299  1.1  augustss 					*strrchr(coll, '.') = 0;
    300  1.1  augustss 				break;
    301  1.1  augustss 			default:
    302  1.1  augustss 				break;
    303  1.1  augustss 			}
    304  1.1  augustss 		}
    305  1.1  augustss 		if (ignore) {
    306  1.1  augustss 			if (verbose)
    307  1.1  augustss 				warnx("ignore item '%s'\n", name);
    308  1.1  augustss 			continue;
    309  1.1  augustss 		}
    310  1.1  augustss 		errx(1, "config file `%s', line %d, HID item not found: `%s'\n",
    311  1.1  augustss 		     conf, line, name);
    312  1.1  augustss 
    313  1.1  augustss 	foundhid:
    314  1.1  augustss 		hid_end_parse(d);
    315  1.1  augustss 		cmd->item = h;
    316  1.1  augustss 		cmd->name = strdup(name);
    317  1.1  augustss 		cmd->action = strdup(action);
    318  1.6  augustss 		if (range) {
    319  1.6  augustss 			if (cmd->value == 1)
    320  1.6  augustss 				cmd->value = u - lo;
    321  1.6  augustss 			else
    322  1.6  augustss 				cmd->value = -1;
    323  1.6  augustss 		}
    324  1.1  augustss 
    325  1.1  augustss 		if (verbose)
    326  1.1  augustss 			printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
    327  1.1  augustss 			       cmd->value, cmd->action);
    328  1.1  augustss 	}
    329  1.1  augustss 	fclose(f);
    330  1.1  augustss }
    331  1.1  augustss 
    332  1.1  augustss void
    333  1.1  augustss docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
    334  1.1  augustss {
    335  1.1  augustss 	char cmdbuf[SIZE], *p, *q;
    336  1.1  augustss 	size_t len;
    337  1.1  augustss 	int n, r;
    338  1.1  augustss 
    339  1.1  augustss 	for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
    340  1.1  augustss 		if (*p == '$') {
    341  1.1  augustss 			p++;
    342  1.1  augustss 			len = &cmdbuf[SIZE-1] - q;
    343  1.1  augustss 			if (isdigit(*p)) {
    344  1.1  augustss 				n = strtol(p, &p, 10) - 1;
    345  1.1  augustss 				if (n >= 0 && n < argc) {
    346  1.1  augustss 					strncpy(q, argv[n], len);
    347  1.1  augustss 					q += strlen(q);
    348  1.1  augustss 				}
    349  1.1  augustss 			} else if (*p == 'V') {
    350  1.1  augustss 				p++;
    351  1.1  augustss 				snprintf(q, len, "%d", value);
    352  1.1  augustss 				q += strlen(q);
    353  1.1  augustss 			} else if (*p == 'N') {
    354  1.1  augustss 				p++;
    355  1.1  augustss 				strncpy(q, cmd->name, len);
    356  1.1  augustss 				q += strlen(q);
    357  1.1  augustss 			} else if (*p == 'H') {
    358  1.1  augustss 				p++;
    359  1.1  augustss 				strncpy(q, hid, len);
    360  1.1  augustss 				q += strlen(q);
    361  1.1  augustss 			} else if (*p) {
    362  1.1  augustss 				*q++ = *p++;
    363  1.1  augustss 			}
    364  1.1  augustss 		} else {
    365  1.1  augustss 			*q++ = *p++;
    366  1.1  augustss 		}
    367  1.1  augustss 	}
    368  1.1  augustss 	*q = 0;
    369  1.1  augustss 
    370  1.1  augustss 	if (verbose)
    371  1.1  augustss 		printf("system '%s'\n", cmdbuf);
    372  1.1  augustss 	r = system(cmdbuf);
    373  1.1  augustss 	if (verbose > 1 && r)
    374  1.1  augustss 		printf("return code = 0x%x\n", r);
    375  1.1  augustss }
    376