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