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