Home | History | Annotate | Line # | Download | only in btdevctl
sdp.c revision 1.8
      1 /*	$NetBSD: sdp.c,v 1.8 2010/04/28 06:18:07 plunky Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 Itronix Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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.
     15  * 3. The name of Itronix Inc. may not be used to endorse
     16  *    or promote products derived from this software without specific
     17  *    prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     26  * ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 /*
     32  * Copyright (c) 2009 The NetBSD Foundation, Inc.
     33  * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin (at) yahoo.com>
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  *
     45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55  * SUCH DAMAGE.
     56  */
     57 
     58 #include <sys/cdefs.h>
     59 __RCSID("$NetBSD: sdp.c,v 1.8 2010/04/28 06:18:07 plunky Exp $");
     60 
     61 #include <sys/types.h>
     62 
     63 #include <dev/bluetooth/btdev.h>
     64 #include <dev/bluetooth/bthidev.h>
     65 #include <dev/bluetooth/btsco.h>
     66 #include <dev/usb/usb.h>
     67 #include <dev/usb/usbhid.h>
     68 
     69 #include <prop/proplib.h>
     70 
     71 #include <bluetooth.h>
     72 #include <err.h>
     73 #include <errno.h>
     74 #include <sdp.h>
     75 #include <stdlib.h>
     76 #include <strings.h>
     77 #include <usbhid.h>
     78 
     79 #include "btdevctl.h"
     80 
     81 static bool parse_hid_descriptor(sdp_data_t *);
     82 static int32_t parse_boolean(sdp_data_t *);
     83 static int32_t parse_pdl_param(sdp_data_t *, uint16_t);
     84 static int32_t parse_pdl(sdp_data_t *, uint16_t);
     85 static int32_t parse_apdl(sdp_data_t *, uint16_t);
     86 
     87 static int config_pnp(prop_dictionary_t, sdp_data_t *);
     88 static int config_hid(prop_dictionary_t, sdp_data_t *);
     89 static int config_hset(prop_dictionary_t, sdp_data_t *);
     90 static int config_hf(prop_dictionary_t, sdp_data_t *);
     91 
     92 uint16_t pnp_services[] = {
     93 	SDP_SERVICE_CLASS_PNP_INFORMATION,
     94 };
     95 
     96 uint16_t hid_services[] = {
     97 	SDP_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE,
     98 };
     99 
    100 uint16_t hset_services[] = {
    101 	SDP_SERVICE_CLASS_HEADSET,
    102 };
    103 
    104 uint16_t hf_services[] = {
    105 	SDP_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY,
    106 };
    107 
    108 static struct {
    109 	const char		*name;
    110 	int			(*handler)(prop_dictionary_t, sdp_data_t *);
    111 	const char		*description;
    112 	uint16_t		*services;
    113 	size_t			nservices;
    114 } cfgtype[] = {
    115     {
    116 	"HID",		config_hid,	"Human Interface Device",
    117 	hid_services,	__arraycount(hid_services),
    118     },
    119     {
    120 	"HSET",		config_hset,	"Headset",
    121 	hset_services,	__arraycount(hset_services),
    122     },
    123     {
    124 	"HF",		config_hf,	"Handsfree",
    125 	hf_services,	__arraycount(hf_services),
    126     },
    127 };
    128 
    129 #define MAX_SSP		(2 + 1 * 3)	/* largest nservices is 1 */
    130 
    131 static bool
    132 cfg_ssa(sdp_session_t ss, uint16_t *services, size_t nservices, sdp_data_t *rsp)
    133 {
    134 	uint8_t buf[MAX_SSP];
    135 	sdp_data_t ssp;
    136 	size_t i;
    137 
    138 	ssp.next = buf;
    139 	ssp.end = buf + sizeof(buf);
    140 
    141 	for (i = 0; i < nservices; i++)
    142 		sdp_put_uuid16(&ssp, services[i]);
    143 
    144 	ssp.end = ssp.next;
    145 	ssp.next = buf;
    146 
    147 	return sdp_service_search_attribute(ss, &ssp, NULL, rsp);
    148 }
    149 
    150 static bool
    151 cfg_search(sdp_session_t ss, int i, prop_dictionary_t dict)
    152 {
    153 	sdp_data_t rsp, rec;
    154 
    155 	/* check PnP Information first */
    156 	if (!cfg_ssa(ss, pnp_services, __arraycount(pnp_services), &rsp))
    157 		return false;
    158 
    159 	while (sdp_get_seq(&rsp, &rec)) {
    160 		if (config_pnp(dict, &rec) == 0)
    161 			break;
    162 	}
    163 
    164 	/* then requested service */
    165 	if (!cfg_ssa(ss, cfgtype[i].services, cfgtype[i].nservices, &rsp))
    166 		return false;
    167 
    168 	while (sdp_get_seq(&rsp, &rec)) {
    169 		errno = (*cfgtype[i].handler)(dict, &rec);
    170 		if (errno == 0)
    171 			return true;
    172 	}
    173 
    174 	return false;
    175 }
    176 
    177 prop_dictionary_t
    178 cfg_query(bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
    179 {
    180 	prop_dictionary_t dict;
    181 	sdp_session_t ss;
    182 	size_t i;
    183 	bool rv;
    184 
    185 	dict = prop_dictionary_create();
    186 	if (dict == NULL)
    187 		return NULL;
    188 
    189 	for (i = 0; i < __arraycount(cfgtype); i++) {
    190 		if (strcasecmp(service, cfgtype[i].name) == 0) {
    191 			ss = sdp_open(laddr, raddr);
    192 			if (ss != NULL) {
    193 				rv = cfg_search(ss, i, dict);
    194 
    195 				sdp_close(ss);
    196 
    197 				if (rv == true)
    198 					return dict;
    199 			}
    200 
    201 			prop_object_release(dict);
    202 			return NULL;
    203 		}
    204 	}
    205 
    206 	printf("Known config types:\n");
    207 	for (i = 0; i < __arraycount(cfgtype); i++)
    208 		printf("\t%s\t%s\n", cfgtype[i].name, cfgtype[i].description);
    209 
    210 	exit(EXIT_FAILURE);
    211 }
    212 
    213 /*
    214  * Configure PnP Information results
    215  */
    216 static int
    217 config_pnp(prop_dictionary_t dict, sdp_data_t *rec)
    218 {
    219 	sdp_data_t value;
    220 	uintmax_t v;
    221 	uint16_t attr;
    222 	int vendor, product, source;
    223 
    224 	vendor = -1;
    225 	product = -1;
    226 	source = -1;
    227 
    228 	while (sdp_get_attr(rec, &attr, &value)) {
    229 		switch (attr) {
    230 		case 0x0201:	/* Vendor ID */
    231 			if (sdp_get_uint(&value, &v)
    232 			    && v <= UINT16_MAX)
    233 				vendor = (int)v;
    234 
    235 			break;
    236 
    237 		case 0x0202:	/* Product ID */
    238 			if (sdp_get_uint(&value, &v)
    239 			    && v <= UINT16_MAX)
    240 				product = (int)v;
    241 
    242 			break;
    243 
    244 		case 0x0205:	/* Vendor ID Source */
    245 			if (sdp_get_uint(&value, &v)
    246 			    && v <= UINT16_MAX)
    247 				source = (int)v;
    248 
    249 			break;
    250 
    251 		default:
    252 			break;
    253 		}
    254 	}
    255 
    256 	if (vendor == -1 || product == -1)
    257 		return ENOATTR;
    258 
    259 	if (source != 0x0002)	/* "USB Implementers Forum" */
    260 		return ENOATTR;
    261 
    262 	if (!prop_dictionary_set_uint16(dict, BTDEVvendor, (uint16_t)vendor))
    263 		return errno;
    264 
    265 	if (!prop_dictionary_set_uint16(dict, BTDEVproduct, (uint16_t)product))
    266 		return errno;
    267 
    268 	return 0;
    269 }
    270 
    271 /*
    272  * Configure HID results
    273  */
    274 static int
    275 config_hid(prop_dictionary_t dict, sdp_data_t *rec)
    276 {
    277 	prop_object_t obj;
    278 	int32_t control_psm, interrupt_psm,
    279 		reconnect_initiate, hid_length;
    280 	uint8_t *hid_descriptor;
    281 	sdp_data_t value;
    282 	const char *mode;
    283 	uint16_t attr;
    284 
    285 	control_psm = -1;
    286 	interrupt_psm = -1;
    287 	reconnect_initiate = -1;
    288 	hid_descriptor = NULL;
    289 	hid_length = -1;
    290 
    291 	while (sdp_get_attr(rec, &attr, &value)) {
    292 		switch (attr) {
    293 		case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
    294 			control_psm = parse_pdl(&value, SDP_UUID_PROTOCOL_L2CAP);
    295 			break;
    296 
    297 		case SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS:
    298 			interrupt_psm = parse_apdl(&value, SDP_UUID_PROTOCOL_L2CAP);
    299 			break;
    300 
    301 		case 0x0205: /* HIDReconnectInitiate */
    302 			reconnect_initiate = parse_boolean(&value);
    303 			break;
    304 
    305 		case 0x0206: /* HIDDescriptorList */
    306 			if (parse_hid_descriptor(&value)) {
    307 				hid_descriptor = value.next;
    308 				hid_length = value.end - value.next;
    309 			}
    310 			break;
    311 
    312 		default:
    313 			break;
    314 		}
    315 	}
    316 
    317 	if (control_psm == -1
    318 	    || interrupt_psm == -1
    319 	    || reconnect_initiate == -1
    320 	    || hid_descriptor == NULL
    321 	    || hid_length == -1)
    322 		return ENOATTR;
    323 
    324 	obj = prop_string_create_cstring_nocopy("bthidev");
    325 	if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
    326 		return errno;
    327 
    328 	prop_object_release(obj);
    329 
    330 	obj = prop_number_create_integer(control_psm);
    331 	if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVcontrolpsm, obj))
    332 		return errno;
    333 
    334 	prop_object_release(obj);
    335 
    336 	obj = prop_number_create_integer(interrupt_psm);
    337 	if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVinterruptpsm, obj))
    338 		return errno;
    339 
    340 	prop_object_release(obj);
    341 
    342 	obj = prop_data_create_data(hid_descriptor, hid_length);
    343 	if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVdescriptor, obj))
    344 		return errno;
    345 
    346 	mode = hid_mode(obj);
    347 	prop_object_release(obj);
    348 
    349 	obj = prop_string_create_cstring_nocopy(mode);
    350 	if (obj == NULL || !prop_dictionary_set(dict, BTDEVmode, obj))
    351 		return errno;
    352 
    353 	prop_object_release(obj);
    354 
    355 	if (!reconnect_initiate) {
    356 		obj = prop_bool_create(true);
    357 		if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVreconnect, obj))
    358 			return errno;
    359 
    360 		prop_object_release(obj);
    361 	}
    362 
    363 	return 0;
    364 }
    365 
    366 /*
    367  * Configure HSET results
    368  */
    369 static int
    370 config_hset(prop_dictionary_t dict, sdp_data_t *rec)
    371 {
    372 	prop_object_t obj;
    373 	sdp_data_t value;
    374 	int32_t channel;
    375 	uint16_t attr;
    376 
    377 	channel = -1;
    378 
    379 	while (sdp_get_attr(rec, &attr, &value)) {
    380 		switch (attr) {
    381 		case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
    382 			channel = parse_pdl(&value, SDP_UUID_PROTOCOL_RFCOMM);
    383 			break;
    384 
    385 		default:
    386 			break;
    387 		}
    388 	}
    389 
    390 	if (channel == -1)
    391 		return ENOATTR;
    392 
    393 	obj = prop_string_create_cstring_nocopy("btsco");
    394 	if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
    395 		return errno;
    396 
    397 	prop_object_release(obj);
    398 
    399 	obj = prop_number_create_integer(channel);
    400 	if (obj == NULL || !prop_dictionary_set(dict, BTSCOchannel, obj))
    401 		return errno;
    402 
    403 	prop_object_release(obj);
    404 
    405 	return 0;
    406 }
    407 
    408 /*
    409  * Configure HF results
    410  */
    411 static int
    412 config_hf(prop_dictionary_t dict, sdp_data_t *rec)
    413 {
    414 	prop_object_t obj;
    415 	sdp_data_t value;
    416 	int32_t channel;
    417 	uint16_t attr;
    418 
    419 	channel = -1;
    420 
    421 	while (sdp_get_attr(rec, &attr, &value)) {
    422 		switch (attr) {
    423 		case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
    424 			channel = parse_pdl(&value, SDP_UUID_PROTOCOL_RFCOMM);
    425 			break;
    426 
    427 		default:
    428 			break;
    429 		}
    430 	}
    431 
    432 	if (channel == -1)
    433 		return ENOATTR;
    434 
    435 	obj = prop_string_create_cstring_nocopy("btsco");
    436 	if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
    437 		return errno;
    438 
    439 	prop_object_release(obj);
    440 
    441 	obj = prop_bool_create(true);
    442 	if (obj == NULL || !prop_dictionary_set(dict, BTSCOlisten, obj))
    443 		return errno;
    444 
    445 	prop_object_release(obj);
    446 
    447 	obj = prop_number_create_integer(channel);
    448 	if (obj == NULL || !prop_dictionary_set(dict, BTSCOchannel, obj))
    449 		return errno;
    450 
    451 	prop_object_release(obj);
    452 
    453 	return 0;
    454 }
    455 
    456 /*
    457  * Parse HIDDescriptorList . This is a sequence of HIDDescriptors, of which
    458  * each is a data element sequence containing, minimally, a ClassDescriptorType
    459  * and ClassDescriptorData containing a byte array of data. Any extra elements
    460  * should be ignored.
    461  *
    462  * If a ClassDescriptorType "Report" is found, set SDP data value to the
    463  * ClassDescriptorData content and return true. Note that we don't need to
    464  * extract the actual length as the SDP data is guaranteed valid.
    465  */
    466 
    467 static bool
    468 parse_hid_descriptor(sdp_data_t *value)
    469 {
    470 	sdp_data_t list, desc;
    471 	uintmax_t type;
    472 	char *str;
    473 	size_t len;
    474 
    475 	if (!sdp_get_seq(value, &list))
    476 		return false;
    477 
    478 	while (sdp_get_seq(&list, &desc)) {
    479 		if (sdp_get_uint(&desc, &type)
    480 		    && type == UDESC_REPORT
    481 		    && sdp_get_str(&desc, &str, &len)) {
    482 			value->next = (uint8_t *)str;
    483 			value->end = (uint8_t *)(str + len);
    484 			return true;
    485 		}
    486 	}
    487 
    488 	return false;
    489 }
    490 
    491 static int32_t
    492 parse_boolean(sdp_data_t *value)
    493 {
    494 	bool bv;
    495 
    496 	if (!sdp_get_bool(value, &bv))
    497 		return -1;
    498 
    499 	return bv;
    500 }
    501 
    502 /*
    503  * The ProtocolDescriptorList attribute describes one or
    504  * more protocol stacks that may be used to gain access to
    505  * the service dscribed by the service record.
    506  *
    507  * If the ProtocolDescriptorList describes a single stack,
    508  * the attribute value takes the form of a data element
    509  * sequence in which each element of the sequence is a
    510  * protocol descriptor.
    511  *
    512  *	seq
    513  *	  <list>
    514  *
    515  * If it is possible for more than one kind of protocol
    516  * stack to be used to gain access to the service, the
    517  * ProtocolDescriptorList takes the form of a data element
    518  * alternative where each member is a data element sequence
    519  * consisting of a list of sequences describing each protocol
    520  *
    521  *	alt
    522  *	  seq
    523  *	    <list>
    524  *	  seq
    525  *	    <list>
    526  *
    527  * Each ProtocolDescriptorList is a list containing a sequence for
    528  * each protocol, where each sequence contains the protocol UUUID
    529  * and any protocol specific parameters.
    530  *
    531  *	seq
    532  *	  uuid		L2CAP
    533  *	  uint16	psm
    534  *	seq
    535  *	  uuid		RFCOMM
    536  *	  uint8		channel
    537  *
    538  * We want to extract the ProtocolSpecificParameter#1 for the
    539  * given protocol, which will be an unsigned int.
    540  */
    541 static int32_t
    542 parse_pdl_param(sdp_data_t *pdl, uint16_t proto)
    543 {
    544 	sdp_data_t seq;
    545 	uintmax_t param;
    546 
    547 	while (sdp_get_seq(pdl, &seq)) {
    548 		if (!sdp_match_uuid16(&seq, proto))
    549 			continue;
    550 
    551 		if (sdp_get_uint(&seq, &param))
    552 			return param;
    553 
    554 		break;
    555 	}
    556 
    557 	return -1;
    558 }
    559 
    560 static int32_t
    561 parse_pdl(sdp_data_t *value, uint16_t proto)
    562 {
    563 	sdp_data_t seq;
    564 	int32_t param = -1;
    565 
    566 	sdp_get_alt(value, value);	/* strip any alt header */
    567 
    568 	while (param == -1 && sdp_get_seq(value, &seq))
    569 		param = parse_pdl_param(&seq, proto);
    570 
    571 	return param;
    572 }
    573 
    574 /*
    575  * Parse AdditionalProtocolDescriptorList
    576  */
    577 static int32_t
    578 parse_apdl(sdp_data_t *value, uint16_t proto)
    579 {
    580 	sdp_data_t seq;
    581 	int32_t param = -1;
    582 
    583 	sdp_get_seq(value, value);	/* strip seq header */
    584 
    585 	while (param == -1 && sdp_get_seq(value, &seq))
    586 		param = parse_pdl_param(&seq, proto);
    587 
    588 	return param;
    589 }
    590 
    591 /*
    592  * return appropriate mode for HID descriptor
    593  */
    594 const char *
    595 hid_mode(prop_data_t desc)
    596 {
    597 	report_desc_t r;
    598 	hid_data_t d;
    599 	struct hid_item h;
    600 	const char *mode;
    601 
    602 	hid_init(NULL);
    603 
    604 	mode = BTDEVauth;	/* default */
    605 
    606 	r = hid_use_report_desc(prop_data_data_nocopy(desc),
    607 				prop_data_size(desc));
    608 	if (r == NULL)
    609 		err(EXIT_FAILURE, "hid_use_report_desc");
    610 
    611 	d = hid_start_parse(r, ~0, -1);
    612 	while (hid_get_item(d, &h) > 0) {
    613 		if (h.kind == hid_collection
    614 		    && HID_PAGE(h.usage) == HUP_GENERIC_DESKTOP
    615 		    && HID_USAGE(h.usage) == HUG_KEYBOARD)
    616 			mode = BTDEVencrypt;
    617 	}
    618 
    619 	hid_end_parse(d);
    620 	hid_dispose_report_desc(r);
    621 
    622 	return mode;
    623 }
    624