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