usbhidaction.c revision 1.3 1 1.3 cgd /* $NetBSD: usbhidaction.c,v 1.3 2001/02/20 23:55:42 cgd 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
165 1.1 augustss fprintf(stderr, "Usage: %s -c config_file [-d] -f hid_dev "
166 1.3 cgd " [-v]\n", getprogname());
167 1.1 augustss exit(1);
168 1.1 augustss }
169 1.1 augustss
170 1.1 augustss static int
171 1.1 augustss peek(FILE *f)
172 1.1 augustss {
173 1.1 augustss int c;
174 1.1 augustss
175 1.1 augustss c = getc(f);
176 1.1 augustss if (c != EOF)
177 1.1 augustss ungetc(c, f);
178 1.1 augustss return c;
179 1.1 augustss }
180 1.1 augustss
181 1.1 augustss void
182 1.1 augustss parse_conf(const char *conf, report_desc_t repd, int ignore)
183 1.1 augustss {
184 1.1 augustss FILE *f;
185 1.1 augustss char *p;
186 1.1 augustss int line;
187 1.1 augustss char buf[SIZE], name[SIZE], value[SIZE], action[SIZE];
188 1.1 augustss char usage[SIZE], coll[SIZE];
189 1.1 augustss struct command *cmd;
190 1.1 augustss struct hid_data *d;
191 1.1 augustss struct hid_item h;
192 1.1 augustss
193 1.1 augustss f = fopen(conf, "r");
194 1.1 augustss if (f == NULL)
195 1.1 augustss err(1, "%s", conf);
196 1.1 augustss
197 1.1 augustss for (line = 1; ; line++) {
198 1.1 augustss if (fgets(buf, sizeof buf, f) == NULL)
199 1.1 augustss break;
200 1.1 augustss if (buf[0] == '#' || buf[0] == '\n')
201 1.1 augustss continue;
202 1.1 augustss p = strchr(buf, '\n');
203 1.1 augustss while (p && isspace(peek(f))) {
204 1.1 augustss if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
205 1.1 augustss break;
206 1.1 augustss p = strchr(buf, '\n');
207 1.1 augustss }
208 1.1 augustss if (p)
209 1.1 augustss *p = 0;
210 1.1 augustss if (sscanf(buf, "%s %s %[^\n]", name, value, action) != 3) {
211 1.1 augustss errx(1, "config file `%s', line %d, syntax error: %s",
212 1.1 augustss conf, line, buf);
213 1.1 augustss }
214 1.1 augustss
215 1.1 augustss cmd = malloc(sizeof *cmd);
216 1.1 augustss if (cmd == NULL)
217 1.1 augustss err(1, "malloc failed");
218 1.1 augustss cmd->next = commands;
219 1.1 augustss commands = cmd;
220 1.1 augustss cmd->line = line;
221 1.1 augustss
222 1.1 augustss if (strcmp(value, "*") == 0) {
223 1.1 augustss cmd->anyvalue = 1;
224 1.1 augustss } else {
225 1.1 augustss cmd->anyvalue = 0;
226 1.1 augustss if (sscanf(value, "%d", &cmd->value) != 1)
227 1.1 augustss errx(1, "config file `%s', line %d, "
228 1.1 augustss "bad value: %s\n", conf, line, value);
229 1.1 augustss }
230 1.1 augustss
231 1.1 augustss coll[0] = 0;
232 1.1 augustss for (d = hid_start_parse(repd, 1 << hid_input);
233 1.1 augustss hid_get_item(d, &h); ) {
234 1.1 augustss if (verbose > 2)
235 1.1 augustss printf("kind=%d usage=%x\n", h.kind, h.usage);
236 1.1 augustss if (h.flags & HIO_CONST)
237 1.1 augustss continue;
238 1.1 augustss switch (h.kind) {
239 1.1 augustss case hid_input:
240 1.1 augustss snprintf(usage, sizeof usage, "%s:%s",
241 1.1 augustss hid_usage_page(HID_PAGE(h.usage)),
242 1.1 augustss hid_usage_in_page(h.usage));
243 1.1 augustss if (verbose > 2)
244 1.1 augustss printf("usage %s\n", usage);
245 1.1 augustss if (strcasecmp(usage, name) == 0)
246 1.1 augustss goto foundhid;
247 1.1 augustss if (coll[0]) {
248 1.1 augustss snprintf(usage, sizeof usage,
249 1.1 augustss "%s.%s:%s", coll+1,
250 1.1 augustss hid_usage_page(HID_PAGE(h.usage)),
251 1.1 augustss hid_usage_in_page(h.usage));
252 1.1 augustss if (verbose > 2)
253 1.1 augustss printf("usage %s\n", usage);
254 1.1 augustss if (strcasecmp(usage, name) == 0)
255 1.1 augustss goto foundhid;
256 1.1 augustss }
257 1.1 augustss break;
258 1.1 augustss case hid_collection:
259 1.1 augustss snprintf(coll + strlen(coll),
260 1.1 augustss sizeof coll - strlen(coll), ".%s:%s",
261 1.1 augustss hid_usage_page(HID_PAGE(h.usage)),
262 1.1 augustss hid_usage_in_page(h.usage));
263 1.1 augustss break;
264 1.1 augustss case hid_endcollection:
265 1.1 augustss if (coll[0])
266 1.1 augustss *strrchr(coll, '.') = 0;
267 1.1 augustss break;
268 1.1 augustss default:
269 1.1 augustss break;
270 1.1 augustss }
271 1.1 augustss }
272 1.1 augustss if (ignore) {
273 1.1 augustss if (verbose)
274 1.1 augustss warnx("ignore item '%s'\n", name);
275 1.1 augustss continue;
276 1.1 augustss }
277 1.1 augustss errx(1, "config file `%s', line %d, HID item not found: `%s'\n",
278 1.1 augustss conf, line, name);
279 1.1 augustss
280 1.1 augustss foundhid:
281 1.1 augustss hid_end_parse(d);
282 1.1 augustss cmd->item = h;
283 1.1 augustss cmd->name = strdup(name);
284 1.1 augustss cmd->action = strdup(action);
285 1.1 augustss
286 1.1 augustss if (verbose)
287 1.1 augustss printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
288 1.1 augustss cmd->value, cmd->action);
289 1.1 augustss }
290 1.1 augustss fclose(f);
291 1.1 augustss }
292 1.1 augustss
293 1.1 augustss void
294 1.1 augustss docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
295 1.1 augustss {
296 1.1 augustss char cmdbuf[SIZE], *p, *q;
297 1.1 augustss size_t len;
298 1.1 augustss int n, r;
299 1.1 augustss
300 1.1 augustss for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
301 1.1 augustss if (*p == '$') {
302 1.1 augustss p++;
303 1.1 augustss len = &cmdbuf[SIZE-1] - q;
304 1.1 augustss if (isdigit(*p)) {
305 1.1 augustss n = strtol(p, &p, 10) - 1;
306 1.1 augustss if (n >= 0 && n < argc) {
307 1.1 augustss strncpy(q, argv[n], len);
308 1.1 augustss q += strlen(q);
309 1.1 augustss }
310 1.1 augustss } else if (*p == 'V') {
311 1.1 augustss p++;
312 1.1 augustss snprintf(q, len, "%d", value);
313 1.1 augustss q += strlen(q);
314 1.1 augustss } else if (*p == 'N') {
315 1.1 augustss p++;
316 1.1 augustss strncpy(q, cmd->name, len);
317 1.1 augustss q += strlen(q);
318 1.1 augustss } else if (*p == 'H') {
319 1.1 augustss p++;
320 1.1 augustss strncpy(q, hid, len);
321 1.1 augustss q += strlen(q);
322 1.1 augustss } else if (*p) {
323 1.1 augustss *q++ = *p++;
324 1.1 augustss }
325 1.1 augustss } else {
326 1.1 augustss *q++ = *p++;
327 1.1 augustss }
328 1.1 augustss }
329 1.1 augustss *q = 0;
330 1.1 augustss
331 1.1 augustss if (verbose)
332 1.1 augustss printf("system '%s'\n", cmdbuf);
333 1.1 augustss r = system(cmdbuf);
334 1.1 augustss if (verbose > 1 && r)
335 1.1 augustss printf("return code = 0x%x\n", r);
336 1.1 augustss }
337