4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $Id: sdp.c,v 1.3 2004/02/17 22:14:57 max Exp $
32 #include <sys/queue.h>
33 #define L2CAP_SOCKET_CHECKED
34 #include <bluetooth.h>
35 #include <dev/usb/usb.h>
36 #include <dev/usb/usbhid.h>
42 #include "bthid_config.h"
43 #include "bthidcontrol.h"
45 static int32_t hid_sdp_query (bdaddr_t const *local, struct hid_device *hd, int32_t *error);
46 static int32_t hid_sdp_parse_protocol_descriptor_list (sdp_attr_p a);
47 static int32_t hid_sdp_parse_hid_descriptor (sdp_attr_p a);
48 static int32_t hid_sdp_parse_boolean (sdp_attr_p a);
51 * Hard coded attibute IDs taken from the
52 * DEVICE IDENTIFICATION PROFILE SPECIFICATION V13 p.12
55 #define SDP_ATTR_DEVICE_ID_SERVICE_VENDORID 0x0201
56 #define SDP_ATTR_DEVICE_ID_SERVICE_PRODUCTID 0x0202
57 #define SDP_ATTR_DEVICE_ID_SERVICE_VERSION 0x0203
58 #define SDP_ATTR_DEVICE_ID_RANGE SDP_ATTR_RANGE( \
59 SDP_ATTR_DEVICE_ID_SERVICE_VENDORID, SDP_ATTR_DEVICE_ID_SERVICE_VERSION )
61 static uint16_t service = SDP_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE;
62 static uint16_t service_devid = SDP_SERVICE_CLASS_PNP_INFORMATION;
63 static uint32_t attrs_devid = SDP_ATTR_DEVICE_ID_RANGE;
65 static uint32_t attrs[] = {
66 SDP_ATTR_RANGE( SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
67 SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST),
68 SDP_ATTR_RANGE (SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS,
69 SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS),
70 SDP_ATTR_RANGE( 0x0205, /* HIDReconnectInitiate */
72 SDP_ATTR_RANGE( 0x0206, /* HIDDescriptorList */
74 SDP_ATTR_RANGE( 0x0209, /* HIDBatteryPower */
76 SDP_ATTR_RANGE( 0x020d, /* HIDNormallyConnectable */
79 #define nattrs (sizeof(attrs)/sizeof(attrs[0]))
81 static sdp_attr_t values[8];
82 #define nvalues (sizeof(values)/sizeof(values[0]))
84 static uint8_t buffer[nvalues][512];
90 #undef hid_sdp_query_exit
91 #define hid_sdp_query_exit(e) { \
98 return (((e) == 0)? 0 : -1); \
102 hid_init_return_values() {
104 for (i = 0; i < nvalues; i ++) {
105 values[i].flags = SDP_ATTR_INVALID;
107 values[i].vlen = sizeof(buffer[i]);
108 values[i].value = buffer[i];
113 hid_sdp_query(bdaddr_t const *local, struct hid_device *hd, int32_t *error)
116 uint8_t *hid_descriptor = NULL, *v;
117 int32_t i, control_psm = -1, interrupt_psm = -1,
118 reconnect_initiate = -1,
119 normally_connectable = 0, battery_power = 0,
120 hid_descriptor_length = -1, type;
121 int16_t vendor_id = 0, product_id = 0, version = 0;
124 local = NG_HCI_BDADDR_ANY;
126 hid_sdp_query_exit(EINVAL);
128 hid_init_return_values();
130 if ((ss = sdp_open(local, &hd->bdaddr)) == NULL)
131 hid_sdp_query_exit(ENOMEM);
132 if (sdp_error(ss) != 0)
133 hid_sdp_query_exit(sdp_error(ss));
134 if (sdp_search(ss, 1, &service, nattrs, attrs, nvalues, values) != 0)
135 hid_sdp_query_exit(sdp_error(ss));
137 for (i = 0; i < nvalues; i ++) {
138 if (values[i].flags != SDP_ATTR_OK)
141 switch (values[i].attr) {
142 case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
143 control_psm = hid_sdp_parse_protocol_descriptor_list(&values[i]);
146 case SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS:
147 interrupt_psm = hid_sdp_parse_protocol_descriptor_list(&values[i]);
150 case 0x0205: /* HIDReconnectInitiate */
151 reconnect_initiate = hid_sdp_parse_boolean(&values[i]);
154 case 0x0206: /* HIDDescriptorList */
155 if (hid_sdp_parse_hid_descriptor(&values[i]) == 0) {
156 hid_descriptor = values[i].value;
157 hid_descriptor_length = values[i].vlen;
161 case 0x0209: /* HIDBatteryPower */
162 battery_power = hid_sdp_parse_boolean(&values[i]);
165 case 0x020d: /* HIDNormallyConnectable */
166 normally_connectable = hid_sdp_parse_boolean(&values[i]);
171 hid_init_return_values();
173 if (sdp_search(ss, 1, &service_devid, 1, &attrs_devid, nvalues, values) != 0)
174 hid_sdp_query_exit(sdp_error(ss));
179 /* If search is successful, scan through return vals */
180 for (i = 0; i < 3; i ++ ) {
181 if (values[i].flags == SDP_ATTR_INVALID )
184 /* Expecting tag + uint16_t on all 3 attributes */
185 if (values[i].vlen != 3)
188 /* Make sure, we're reading a uint16_t */
191 if (type != SDP_DATA_UINT16 )
194 switch (values[i].attr) {
195 case SDP_ATTR_DEVICE_ID_SERVICE_VENDORID:
196 SDP_GET16(vendor_id, v);
198 case SDP_ATTR_DEVICE_ID_SERVICE_PRODUCTID:
199 SDP_GET16(product_id, v);
201 case SDP_ATTR_DEVICE_ID_SERVICE_VERSION:
202 SDP_GET16(version, v);
209 if (control_psm == -1 || interrupt_psm == -1 ||
210 reconnect_initiate == -1 ||
211 hid_descriptor == NULL || hid_descriptor_length == -1)
212 hid_sdp_query_exit(ENOATTR);
213 hd->vendor_id = vendor_id;
214 hd->product_id = product_id;
215 hd->version = version;
216 hd->control_psm = control_psm;
217 hd->interrupt_psm = interrupt_psm;
218 hd->reconnect_initiate = reconnect_initiate? 1 : 0;
219 hd->battery_power = battery_power? 1 : 0;
220 hd->normally_connectable = normally_connectable? 1 : 0;
221 hd->desc = hid_use_report_desc(hid_descriptor, hid_descriptor_length);
222 if (hd->desc == NULL)
223 hid_sdp_query_exit(ENOMEM);
238 hid_sdp_parse_protocol_descriptor_list(sdp_attr_p a)
240 uint8_t *ptr = a->value;
241 uint8_t *end = a->value + a->vlen;
242 int32_t type, len, uuid, psm;
247 if (a->attr == SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS) {
315 case SDP_DATA_UUID16:
316 SDP_GET16(uuid, ptr);
317 if (uuid != SDP_UUID_PROTOCOL_L2CAP)
321 case SDP_DATA_UUID32: /* XXX FIXME can we have 32-bit UUID */
322 case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
331 if (type != SDP_DATA_UINT16)
346 hid_sdp_parse_hid_descriptor(sdp_attr_p a)
348 uint8_t *ptr = a->value;
349 uint8_t *end = a->value + a->vlen;
350 int32_t type, len, descriptor_type;
405 if (type != SDP_DATA_UINT8 || ptr + 1 > end)
407 SDP_GET8(descriptor_type, ptr);
409 /* Descriptor value */
438 if (descriptor_type == UDESC_REPORT && len > 0) {
453 hid_sdp_parse_boolean(sdp_attr_p a)
455 if (a->vlen != 2 || a->value[0] != SDP_DATA_BOOL)
458 return (a->value[1]);
461 /* Perform SDP query */
463 hid_query(bdaddr_t *bdaddr, int argc, char **argv)
465 struct hid_device hd;
468 memcpy(&hd.bdaddr, bdaddr, sizeof(hd.bdaddr));
469 if (hid_sdp_query(NULL, &hd, &e) < 0) {
470 fprintf(stderr, "Could not perform SDP query on the " \
471 "device %s. %s (%d)\n", bt_ntoa(bdaddr, NULL),
476 print_hid_device(&hd, stdout);
481 struct bthid_command sdp_commands[] =
485 "Perform SDP query to the specified device and print HID configuration entry\n"\
486 "for the device. The configuration entry should be appended to the Bluetooth\n"\
487 "HID daemon configuration file and the daemon should be restarted.\n",