7 * Copyright (c) 2006 Maksim Yevmenkin <m_evmenkin@yahoo.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $Id: parser.y,v 1.7 2006/09/07 21:06:53 max Exp $
35 #include <sys/queue.h>
36 #define L2CAP_SOCKET_CHECKED
37 #include <bluetooth.h>
38 #include <dev/usb/usb.h>
39 #include <dev/usb/usbhid.h>
52 #define LOGCRIT LOG_CRIT
53 #define LOGERR LOG_ERR
54 #define LOGWARNING LOG_WARNING
57 #define SYSLOG fprintf
58 #define LOGCRIT stderr
60 #define LOGWARNING stderr
62 #endif /* ndef BTHIDCONTROL */
64 #include "bthid_config.h"
67 void yyerror (char const *);
68 static int32_t check_hid_device(hid_device_p hid_device);
69 static void free_hid_device (hid_device_p hid_device);
73 char const *config_file = BTHIDD_CONFFILE;
74 char const *hids_file = BTHIDD_HIDSFILE;
76 static char buffer[1024];
77 static int32_t hid_descriptor_size;
78 static hid_device_t *hid_device = NULL;
79 static LIST_HEAD(, hid_device) hid_devices;
88 %token <bdaddr> T_BDADDRSTRING
89 %token <num> T_HEXBYTE
90 %token T_DEVICE T_BDADDR T_CONTROL_PSM T_INTERRUPT_PSM T_RECONNECT_INITIATE
91 %token T_BATTERY_POWER T_NORMALLY_CONNECTABLE T_HID_DESCRIPTOR
92 %token T_TRUE T_FALSE T_ERROR
102 hid_device = (hid_device_t *) calloc(1, sizeof(*hid_device));
103 if (hid_device == NULL) {
104 SYSLOG(LOGCRIT, "Could not allocate new " \
109 hid_device->new_device = 1;
113 if (check_hid_device(hid_device))
114 LIST_INSERT_HEAD(&hid_devices,hid_device,next);
116 free_hid_device(hid_device);
131 | normally_connectable
136 bdaddr: T_BDADDR T_BDADDRSTRING
138 memcpy(&hid_device->bdaddr, &$2, sizeof(hid_device->bdaddr));
142 control_psm: T_CONTROL_PSM T_HEXBYTE
144 hid_device->control_psm = $2;
148 interrupt_psm: T_INTERRUPT_PSM T_HEXBYTE
150 hid_device->interrupt_psm = $2;
154 reconnect_initiate: T_RECONNECT_INITIATE T_TRUE
156 hid_device->reconnect_initiate = 1;
158 | T_RECONNECT_INITIATE T_FALSE
160 hid_device->reconnect_initiate = 0;
164 battery_power: T_BATTERY_POWER T_TRUE
166 hid_device->battery_power = 1;
168 | T_BATTERY_POWER T_FALSE
170 hid_device->battery_power = 0;
174 normally_connectable: T_NORMALLY_CONNECTABLE T_TRUE
176 hid_device->normally_connectable = 1;
178 | T_NORMALLY_CONNECTABLE T_FALSE
180 hid_device->normally_connectable = 0;
184 hid_descriptor: T_HID_DESCRIPTOR
186 hid_descriptor_size = 0;
188 '{' hid_descriptor_bytes '}'
190 if (hid_device->desc != NULL)
191 hid_dispose_report_desc(hid_device->desc);
193 hid_device->desc = hid_use_report_desc((unsigned char *) buffer, hid_descriptor_size);
194 if (hid_device->desc == NULL) {
195 SYSLOG(LOGCRIT, "Could not use HID descriptor" EOL);
201 hid_descriptor_bytes: hid_descriptor_byte
202 | hid_descriptor_bytes hid_descriptor_byte
205 hid_descriptor_byte: T_HEXBYTE
207 if (hid_descriptor_size >= (int32_t) sizeof(buffer)) {
208 SYSLOG(LOGCRIT, "HID descriptor is too big" EOL);
212 buffer[hid_descriptor_size ++] = $1;
216 parser_error: T_ERROR
223 /* Display parser error message */
225 yyerror(char const *message)
227 SYSLOG(LOGERR, "%s in line %d" EOL, message, yylineno);
230 /* Re-read config file */
232 read_config_file(void)
236 if (config_file == NULL) {
237 SYSLOG(LOGERR, "Unknown config file name!" EOL);
241 if ((yyin = fopen(config_file, "r")) == NULL) {
242 SYSLOG(LOGERR, "Could not open config file '%s'. %s (%d)" EOL,
243 config_file, strerror(errno), errno);
249 SYSLOG(LOGERR, "Could not parse config file '%s'" EOL,
265 while (!LIST_EMPTY(&hid_devices)) {
266 hid_device_p d = LIST_FIRST(&hid_devices);
268 LIST_REMOVE(d, next);
273 /* Lookup config entry */
275 get_hid_device(bdaddr_p bdaddr)
279 LIST_FOREACH(d, &hid_devices, next)
280 if (memcmp(&d->bdaddr, bdaddr, sizeof(bdaddr_t)) == 0)
286 /* Get next config entry */
288 get_next_hid_device(hid_device_p d)
290 return ((d == NULL)? LIST_FIRST(&hid_devices) : LIST_NEXT(d, next));
293 /* Print config entry */
295 print_hid_device(hid_device_p d, FILE *f)
297 /* XXX FIXME hack! */
300 unsigned char data[1];
302 /* XXX FIXME hack! */
304 struct report_desc *desc = (struct report_desc *) d->desc;
310 " control_psm 0x%x;\n" \
311 " interrupt_psm 0x%x;\n" \
312 " reconnect_initiate %s;\n" \
313 " battery_power %s;\n" \
314 " normally_connectable %s;\n" \
316 bt_ntoa(&d->bdaddr, NULL),
317 d->control_psm, d->interrupt_psm,
318 d->reconnect_initiate? "true" : "false",
319 d->battery_power? "true" : "false",
320 d->normally_connectable? "true" : "false");
322 for (i = 0; i < desc->size; i ++) {
326 fprintf(f, "0x%2.2x ", desc->data[i]);
335 /* Check config entry */
337 check_hid_device(hid_device_p d)
343 if (get_hid_device(&d->bdaddr) != NULL) {
344 SYSLOG(LOGERR, "Ignoring duplicated entry for bdaddr %s" EOL,
345 bt_ntoa(&d->bdaddr, NULL));
349 if (d->control_psm == 0) {
350 SYSLOG(LOGERR, "Ignoring entry with invalid control PSM" EOL);
354 if (d->interrupt_psm == 0) {
355 SYSLOG(LOGERR, "Ignoring entry with invalid interrupt PSM" EOL);
359 if (d->desc == NULL) {
360 SYSLOG(LOGERR, "Ignoring entry without HID descriptor" EOL);
364 /* XXX somehow need to make sure descriptor is valid */
365 for (hd = hid_start_parse(d->desc, ~0, -1); hid_get_item(hd, &hi) > 0; ) {
368 case hid_endcollection:
374 /* Check if the device may send keystrokes */
375 page = HID_PAGE(hi.usage);
376 if (page == HUP_KEYBOARD)
386 /* Free config entry */
388 free_hid_device(hid_device_p d)
391 hid_dispose_report_desc(d->desc);
393 memset(d, 0, sizeof(*d));
397 /* Re-read hids file */
407 if (hids_file == NULL) {
408 SYSLOG(LOGERR, "Unknown HIDs file name!" EOL);
412 if ((f = fopen(hids_file, "r")) == NULL) {
416 SYSLOG(LOGERR, "Could not open HIDs file '%s'. %s (%d)" EOL,
417 hids_file, strerror(errno), errno);
421 for (lineno = 1; fgets(buffer, sizeof(buffer), f) != NULL; lineno ++) {
422 if ((line = strtok(buffer, "\r\n\t ")) == NULL)
423 continue; /* ignore empty lines */
425 if (!bt_aton(line, &bdaddr)) {
426 SYSLOG(LOGWARNING, "Ignoring unparseable BD_ADDR in " \
427 "%s:%d" EOL, hids_file, lineno);
431 if ((d = get_hid_device(&bdaddr)) != NULL)
440 /* Write hids file */
442 write_hids_file(void)
448 if (hids_file == NULL) {
449 SYSLOG(LOGERR, "Unknown HIDs file name!" EOL);
453 snprintf(path, sizeof(path), "%s.new", hids_file);
455 if ((f = fopen(path, "w")) == NULL) {
456 SYSLOG(LOGERR, "Could not open HIDs file '%s'. %s (%d)" EOL,
457 path, strerror(errno), errno);
461 LIST_FOREACH(d, &hid_devices, next)
463 fprintf(f, "%s\n", bt_ntoa(&d->bdaddr, NULL));
467 if (rename(path, hids_file) < 0) {
468 SYSLOG(LOGERR, "Could not rename new HIDs file '%s' to '%s'. " \
469 "%s (%d)" EOL, path, hids_file, strerror(errno), errno);