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);
53 * Hard coded attibute IDs taken from the
54 * DEVICE IDENTIFICATION PROFILE SPECIFICATION V13 p.12
57 #define SDP_ATTR_DEVICE_ID_SERVICE_VENDORID 0x0201
58 #define SDP_ATTR_DEVICE_ID_SERVICE_PRODUCTID 0x0202
59 #define SDP_ATTR_DEVICE_ID_SERVICE_VERSION 0x0203
60 #define SDP_ATTR_DEVICE_ID_RANGE SDP_ATTR_RANGE( \
61 SDP_ATTR_DEVICE_ID_SERVICE_VENDORID, SDP_ATTR_DEVICE_ID_SERVICE_VERSION )
63 static uint16_t service = SDP_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE;
64 static uint16_t service_devid = SDP_SERVICE_CLASS_PNP_INFORMATION;
65 static uint32_t attrs_devid = SDP_ATTR_DEVICE_ID_RANGE;
67 static uint32_t attrs[] = {
68 SDP_ATTR_RANGE( SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
69 SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST),
70 SDP_ATTR_RANGE (SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS,
71 SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS),
72 SDP_ATTR_RANGE( 0x0205, /* HIDReconnectInitiate */
74 SDP_ATTR_RANGE( 0x0206, /* HIDDescriptorList */
76 SDP_ATTR_RANGE( 0x0209, /* HIDBatteryPower */
78 SDP_ATTR_RANGE( 0x020d, /* HIDNormallyConnectable */
81 #define nattrs (sizeof(attrs)/sizeof(attrs[0]))
83 static sdp_attr_t values[8];
84 #define nvalues (sizeof(values)/sizeof(values[0]))
86 static uint8_t buffer[nvalues][512];
92 #undef hid_sdp_query_exit
93 #define hid_sdp_query_exit(e) { \
100 return (((e) == 0)? 0 : -1); \
104 hid_init_return_values() {
106 for (i = 0; i < nvalues; i ++) {
107 values[i].flags = SDP_ATTR_INVALID;
109 values[i].vlen = sizeof(buffer[i]);
110 values[i].value = buffer[i];
115 hid_sdp_query(bdaddr_t const *local, struct hid_device *hd, int32_t *error)
118 uint8_t *hid_descriptor = NULL, *v;
119 int32_t i, control_psm = -1, interrupt_psm = -1,
120 reconnect_initiate = -1,
121 normally_connectable = 0, battery_power = 0,
122 hid_descriptor_length = -1, type;
123 int16_t vendor_id = 0, product_id = 0, version = 0;
126 local = NG_HCI_BDADDR_ANY;
128 hid_sdp_query_exit(EINVAL);
130 hid_init_return_values();
132 if ((ss = sdp_open(local, &hd->bdaddr)) == NULL)
133 hid_sdp_query_exit(ENOMEM);
134 if (sdp_error(ss) != 0)
135 hid_sdp_query_exit(sdp_error(ss));
136 if (sdp_search(ss, 1, &service, nattrs, attrs, nvalues, values) != 0)
137 hid_sdp_query_exit(sdp_error(ss));
139 for (i = 0; i < nvalues; i ++) {
140 if (values[i].flags != SDP_ATTR_OK)
143 switch (values[i].attr) {
144 case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
145 control_psm = hid_sdp_parse_protocol_descriptor_list(&values[i]);
148 case SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS:
149 interrupt_psm = hid_sdp_parse_protocol_descriptor_list(&values[i]);
152 case 0x0205: /* HIDReconnectInitiate */
153 reconnect_initiate = hid_sdp_parse_boolean(&values[i]);
156 case 0x0206: /* HIDDescriptorList */
157 if (hid_sdp_parse_hid_descriptor(&values[i]) == 0) {
158 hid_descriptor = values[i].value;
159 hid_descriptor_length = values[i].vlen;
163 case 0x0209: /* HIDBatteryPower */
164 battery_power = hid_sdp_parse_boolean(&values[i]);
167 case 0x020d: /* HIDNormallyConnectable */
168 normally_connectable = hid_sdp_parse_boolean(&values[i]);
173 hid_init_return_values();
175 if (sdp_search(ss, 1, &service_devid, 1, &attrs_devid, nvalues, values) != 0)
176 hid_sdp_query_exit(sdp_error(ss));
181 /* If search is successful, scan through return vals */
182 for (i = 0; i < 3; i ++ ) {
183 if (values[i].flags == SDP_ATTR_INVALID )
186 /* Expecting tag + uint16_t on all 3 attributes */
187 if (values[i].vlen != 3)
190 /* Make sure, we're reading a uint16_t */
193 if (type != SDP_DATA_UINT16 )
196 switch (values[i].attr) {
197 case SDP_ATTR_DEVICE_ID_SERVICE_VENDORID:
198 SDP_GET16(vendor_id, v);
200 case SDP_ATTR_DEVICE_ID_SERVICE_PRODUCTID:
201 SDP_GET16(product_id, v);
203 case SDP_ATTR_DEVICE_ID_SERVICE_VERSION:
204 SDP_GET16(version, v);
211 if (control_psm == -1 || interrupt_psm == -1 ||
212 reconnect_initiate == -1 ||
213 hid_descriptor == NULL || hid_descriptor_length == -1)
214 hid_sdp_query_exit(ENOATTR);
215 hd->vendor_id = vendor_id;
216 hd->product_id = product_id;
217 hd->version = version;
218 hd->control_psm = control_psm;
219 hd->interrupt_psm = interrupt_psm;
220 hd->reconnect_initiate = reconnect_initiate? 1 : 0;
221 hd->battery_power = battery_power? 1 : 0;
222 hd->normally_connectable = normally_connectable? 1 : 0;
223 hd->desc = hid_use_report_desc(hid_descriptor, hid_descriptor_length);
224 if (hd->desc == NULL)
225 hid_sdp_query_exit(ENOMEM);
240 hid_sdp_parse_protocol_descriptor_list(sdp_attr_p a)
242 uint8_t *ptr = a->value;
243 uint8_t *end = a->value + a->vlen;
244 int32_t type, len, uuid, psm;
249 if (a->attr == SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS) {
317 case SDP_DATA_UUID16:
318 SDP_GET16(uuid, ptr);
319 if (uuid != SDP_UUID_PROTOCOL_L2CAP)
323 case SDP_DATA_UUID32: /* XXX FIXME can we have 32-bit UUID */
324 case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
333 if (type != SDP_DATA_UINT16)
348 hid_sdp_parse_hid_descriptor(sdp_attr_p a)
350 uint8_t *ptr = a->value;
351 uint8_t *end = a->value + a->vlen;
352 int32_t type, len, descriptor_type;
407 if (type != SDP_DATA_UINT8 || ptr + 1 > end)
409 SDP_GET8(descriptor_type, ptr);
411 /* Descriptor value */
440 if (descriptor_type == UDESC_REPORT && len > 0) {
455 hid_sdp_parse_boolean(sdp_attr_p a)
457 if (a->vlen != 2 || a->value[0] != SDP_DATA_BOOL)
460 return (a->value[1]);
463 /* Perform SDP query */
465 hid_query(bdaddr_t *bdaddr, int argc, char **argv)
467 struct hid_device hd;
470 memcpy(&hd.bdaddr, bdaddr, sizeof(hd.bdaddr));
471 if (hid_sdp_query(NULL, &hd, &e) < 0) {
472 fprintf(stderr, "Could not perform SDP query on the " \
473 "device %s. %s (%d)\n", bt_ntoa(bdaddr, NULL),
478 print_hid_device(&hd, stdout);
483 struct bthid_command sdp_commands[] =
487 "Perform SDP query to the specified device and print HID configuration entry\n"\
488 "for the device. The configuration entry should be appended to the Bluetooth\n"\
489 "HID daemon configuration file and the daemon should be restarted.\n",