[BACK]Return to usbhidaction.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / usbhidaction

Annotation of src/usr.bin/usbhidaction/usbhidaction.c, Revision 1.5

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