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