4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: sdp.c,v 1.3 2004/02/17 22:14:57 max Exp $
34 #include <sys/queue.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
37 #include <dev/usb/usb.h>
38 #include <dev/usb/usbhid.h>
44 #include "bthid_config.h"
45 #include "bthidcontrol.h"
47 static int32_t hid_sdp_query (bdaddr_t const *local, struct hid_device *hd, int32_t *error);
48 static int32_t hid_sdp_parse_protocol_descriptor_list (sdp_attr_p a);
49 static int32_t hid_sdp_parse_hid_descriptor (sdp_attr_p a);
50 static int32_t hid_sdp_parse_boolean (sdp_attr_p a);
52 static uint16_t service = SDP_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE;
54 static uint32_t attrs[] = {
55 SDP_ATTR_RANGE( SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
56 SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST),
57 SDP_ATTR_RANGE (SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS,
58 SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS),
59 SDP_ATTR_RANGE( 0x0205, /* HIDReconnectInitiate */
61 SDP_ATTR_RANGE( 0x0206, /* HIDDescriptorList */
63 SDP_ATTR_RANGE( 0x0209, /* HIDBatteryPower */
65 SDP_ATTR_RANGE( 0x020d, /* HIDNormallyConnectable */
68 #define nattrs (sizeof(attrs)/sizeof(attrs[0]))
70 static sdp_attr_t values[8];
71 #define nvalues (sizeof(values)/sizeof(values[0]))
73 static uint8_t buffer[nvalues][512];
79 #undef hid_sdp_query_exit
80 #define hid_sdp_query_exit(e) { \
87 return (((e) == 0)? 0 : -1); \
91 hid_sdp_query(bdaddr_t const *local, struct hid_device *hd, int32_t *error)
94 uint8_t *hid_descriptor = NULL;
95 int32_t i, control_psm = -1, interrupt_psm = -1,
96 reconnect_initiate = -1,
97 normally_connectable = 0, battery_power = 0,
98 hid_descriptor_length = -1;
101 local = NG_HCI_BDADDR_ANY;
103 hid_sdp_query_exit(EINVAL);
105 for (i = 0; i < nvalues; i ++) {
106 values[i].flags = SDP_ATTR_INVALID;
108 values[i].vlen = sizeof(buffer[i]);
109 values[i].value = buffer[i];
112 if ((ss = sdp_open(local, &hd->bdaddr)) == NULL)
113 hid_sdp_query_exit(ENOMEM);
114 if (sdp_error(ss) != 0)
115 hid_sdp_query_exit(sdp_error(ss));
116 if (sdp_search(ss, 1, &service, nattrs, attrs, nvalues, values) != 0)
117 hid_sdp_query_exit(sdp_error(ss));
122 for (i = 0; i < nvalues; i ++) {
123 if (values[i].flags != SDP_ATTR_OK)
126 switch (values[i].attr) {
127 case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
128 control_psm = hid_sdp_parse_protocol_descriptor_list(&values[i]);
131 case SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS:
132 interrupt_psm = hid_sdp_parse_protocol_descriptor_list(&values[i]);
135 case 0x0205: /* HIDReconnectInitiate */
136 reconnect_initiate = hid_sdp_parse_boolean(&values[i]);
139 case 0x0206: /* HIDDescriptorList */
140 if (hid_sdp_parse_hid_descriptor(&values[i]) == 0) {
141 hid_descriptor = values[i].value;
142 hid_descriptor_length = values[i].vlen;
146 case 0x0209: /* HIDBatteryPower */
147 battery_power = hid_sdp_parse_boolean(&values[i]);
150 case 0x020d: /* HIDNormallyConnectable */
151 normally_connectable = hid_sdp_parse_boolean(&values[i]);
156 if (control_psm == -1 || interrupt_psm == -1 ||
157 reconnect_initiate == -1 ||
158 hid_descriptor == NULL || hid_descriptor_length == -1)
159 hid_sdp_query_exit(ENOATTR);
161 hd->control_psm = control_psm;
162 hd->interrupt_psm = interrupt_psm;
163 hd->reconnect_initiate = reconnect_initiate? 1 : 0;
164 hd->battery_power = battery_power? 1 : 0;
165 hd->normally_connectable = normally_connectable? 1 : 0;
166 hd->desc = hid_use_report_desc(hid_descriptor, hid_descriptor_length);
167 if (hd->desc == NULL)
168 hid_sdp_query_exit(ENOMEM);
183 hid_sdp_parse_protocol_descriptor_list(sdp_attr_p a)
185 uint8_t *ptr = a->value;
186 uint8_t *end = a->value + a->vlen;
187 int32_t type, len, uuid, psm;
192 if (a->attr == SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS) {
260 case SDP_DATA_UUID16:
261 SDP_GET16(uuid, ptr);
262 if (uuid != SDP_UUID_PROTOCOL_L2CAP)
266 case SDP_DATA_UUID32: /* XXX FIXME can we have 32-bit UUID */
267 case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
276 if (type != SDP_DATA_UINT16)
291 hid_sdp_parse_hid_descriptor(sdp_attr_p a)
293 uint8_t *ptr = a->value;
294 uint8_t *end = a->value + a->vlen;
295 int32_t type, len, descriptor_type;
350 if (type != SDP_DATA_UINT8 || ptr + 1 > end)
352 SDP_GET8(descriptor_type, ptr);
354 /* Descriptor value */
383 if (descriptor_type == UDESC_REPORT && len > 0) {
398 hid_sdp_parse_boolean(sdp_attr_p a)
400 if (a->vlen != 2 || a->value[0] != SDP_DATA_BOOL)
403 return (a->value[1]);
406 /* Perform SDP query */
408 hid_query(bdaddr_t *bdaddr, int argc, char **argv)
410 struct hid_device hd;
413 memcpy(&hd.bdaddr, bdaddr, sizeof(hd.bdaddr));
414 if (hid_sdp_query(NULL, &hd, &e) < 0) {
415 fprintf(stderr, "Could not perform SDP query on the " \
416 "device %s. %s (%d)\n", bt_ntoa(bdaddr, NULL),
421 print_hid_device(&hd, stdout);
426 struct bthid_command sdp_commands[] =
430 "Perform SDP query to the specified device and print HID configuration entry\n"\
431 "for the device. The configuration entry should be appended to the Bluetooth\n"\
432 "HID daemon configuration file and the daemon should be restarted.\n",