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