Home | History | Annotate | Line # | Download | only in btdevctl
sdp.c revision 1.5.8.1
      1  1.5.8.1     jym /*	$NetBSD: sdp.c,v 1.5.8.1 2009/05/13 19:20:19 jym 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.5.8.1     jym  * 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.5.8.1     jym __RCSID("$NetBSD: sdp.c,v 1.5.8.1 2009/05/13 19:20:19 jym 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.1  plunky 
     69      1.1  plunky #include <prop/proplib.h>
     70      1.1  plunky 
     71      1.1  plunky #include <bluetooth.h>
     72      1.1  plunky #include <err.h>
     73      1.1  plunky #include <errno.h>
     74      1.1  plunky #include <sdp.h>
     75      1.1  plunky #include <stdlib.h>
     76  1.5.8.1     jym #include <strings.h>
     77      1.1  plunky #include <usbhid.h>
     78      1.1  plunky 
     79      1.1  plunky #include "btdevctl.h"
     80      1.1  plunky 
     81  1.5.8.1     jym static bool parse_hid_descriptor(sdp_data_t *);
     82  1.5.8.1     jym static int32_t parse_boolean(sdp_data_t *);
     83  1.5.8.1     jym static int32_t parse_pdl_param(sdp_data_t *, uint16_t);
     84  1.5.8.1     jym static int32_t parse_pdl(sdp_data_t *, uint16_t);
     85  1.5.8.1     jym static int32_t parse_apdl(sdp_data_t *, uint16_t);
     86  1.5.8.1     jym 
     87  1.5.8.1     jym static int config_hid(prop_dictionary_t, sdp_data_t *);
     88  1.5.8.1     jym static int config_hset(prop_dictionary_t, sdp_data_t *);
     89  1.5.8.1     jym static int config_hf(prop_dictionary_t, sdp_data_t *);
     90      1.1  plunky 
     91      1.1  plunky uint16_t hid_services[] = {
     92  1.5.8.1     jym 	SDP_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE,
     93      1.1  plunky };
     94      1.1  plunky 
     95      1.1  plunky uint16_t hset_services[] = {
     96  1.5.8.1     jym 	SDP_SERVICE_CLASS_HEADSET,
     97      1.1  plunky };
     98      1.1  plunky 
     99      1.1  plunky uint16_t hf_services[] = {
    100  1.5.8.1     jym 	SDP_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY,
    101      1.1  plunky };
    102      1.1  plunky 
    103      1.1  plunky static struct {
    104      1.1  plunky 	const char		*name;
    105  1.5.8.1     jym 	int			(*handler)(prop_dictionary_t, sdp_data_t *);
    106      1.1  plunky 	const char		*description;
    107      1.1  plunky 	uint16_t		*services;
    108  1.5.8.1     jym 	size_t			nservices;
    109      1.1  plunky } cfgtype[] = {
    110      1.1  plunky     {
    111      1.1  plunky 	"HID",		config_hid,	"Human Interface Device",
    112      1.5  plunky 	hid_services,	__arraycount(hid_services),
    113      1.1  plunky     },
    114      1.1  plunky     {
    115      1.1  plunky 	"HSET",		config_hset,	"Headset",
    116      1.5  plunky 	hset_services,	__arraycount(hset_services),
    117      1.1  plunky     },
    118      1.1  plunky     {
    119      1.1  plunky 	"HF",		config_hf,	"Handsfree",
    120      1.5  plunky 	hf_services,	__arraycount(hf_services),
    121      1.1  plunky     },
    122      1.1  plunky };
    123      1.1  plunky 
    124  1.5.8.1     jym #define MAX_SSP		(2 + 1 * 3)	/* largest nservices is 1 */
    125      1.1  plunky 
    126      1.1  plunky prop_dictionary_t
    127      1.1  plunky cfg_query(bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
    128      1.1  plunky {
    129      1.1  plunky 	prop_dictionary_t dict;
    130  1.5.8.1     jym 	sdp_session_t ss;
    131  1.5.8.1     jym 	uint8_t buf[MAX_SSP];
    132  1.5.8.1     jym 	sdp_data_t ssp, rsp, rec;
    133  1.5.8.1     jym 	size_t i, n;
    134  1.5.8.1     jym 	bool rv;
    135      1.1  plunky 
    136      1.1  plunky 	dict = prop_dictionary_create();
    137      1.1  plunky 	if (dict == NULL)
    138      1.1  plunky 		return NULL;
    139      1.1  plunky 
    140      1.5  plunky 	for (i = 0; i < __arraycount(cfgtype); i++) {
    141      1.1  plunky 		if (strcasecmp(service, cfgtype[i].name) == 0) {
    142      1.1  plunky 			ss = sdp_open(laddr, raddr);
    143  1.5.8.1     jym 			if (ss == NULL)
    144      1.1  plunky 				return NULL;
    145      1.1  plunky 
    146  1.5.8.1     jym 			/* build ServiceSearchPattern */
    147  1.5.8.1     jym 			ssp.next = buf;
    148  1.5.8.1     jym 			ssp.end = buf + sizeof(buf);
    149  1.5.8.1     jym 
    150  1.5.8.1     jym 			for (n = 0; n < cfgtype[i].nservices; n++)
    151  1.5.8.1     jym 				sdp_put_uuid16(&ssp, cfgtype[i].services[n]);
    152  1.5.8.1     jym 
    153  1.5.8.1     jym 			ssp.end = ssp.next;
    154  1.5.8.1     jym 			ssp.next = buf;
    155  1.5.8.1     jym 
    156  1.5.8.1     jym 			rv = sdp_service_search_attribute(ss, &ssp, NULL, &rsp);
    157  1.5.8.1     jym 			if (!rv) {
    158  1.5.8.1     jym 				prop_object_release(dict);
    159  1.5.8.1     jym 				sdp_close(ss);
    160      1.1  plunky 				return NULL;
    161      1.1  plunky 			}
    162      1.1  plunky 
    163  1.5.8.1     jym 			while (sdp_get_seq(&rsp, &rec)) {
    164  1.5.8.1     jym 				errno = (*cfgtype[i].handler)(dict, &rec);
    165  1.5.8.1     jym 				if (errno == 0) {
    166  1.5.8.1     jym 					sdp_close(ss);
    167  1.5.8.1     jym 					return dict;
    168  1.5.8.1     jym 				}
    169  1.5.8.1     jym 			}
    170      1.1  plunky 
    171  1.5.8.1     jym 			sdp_close(ss);
    172  1.5.8.1     jym 			prop_object_release(dict);
    173  1.5.8.1     jym 			return NULL;
    174      1.1  plunky 		}
    175      1.1  plunky 	}
    176      1.1  plunky 
    177      1.1  plunky 	printf("Known config types:\n");
    178      1.5  plunky 	for (i = 0; i < __arraycount(cfgtype); i++)
    179      1.1  plunky 		printf("\t%s\t%s\n", cfgtype[i].name, cfgtype[i].description);
    180      1.1  plunky 
    181      1.1  plunky 	exit(EXIT_FAILURE);
    182      1.1  plunky }
    183      1.1  plunky 
    184      1.1  plunky /*
    185      1.1  plunky  * Configure HID results
    186      1.1  plunky  */
    187      1.1  plunky static int
    188  1.5.8.1     jym config_hid(prop_dictionary_t dict, sdp_data_t *rec)
    189      1.1  plunky {
    190      1.1  plunky 	prop_object_t obj;
    191      1.1  plunky 	int32_t control_psm, interrupt_psm,
    192  1.5.8.1     jym 		reconnect_initiate, hid_length;
    193      1.1  plunky 	uint8_t *hid_descriptor;
    194  1.5.8.1     jym 	sdp_data_t value;
    195      1.3  plunky 	const char *mode;
    196  1.5.8.1     jym 	uint16_t attr;
    197      1.1  plunky 
    198      1.1  plunky 	control_psm = -1;
    199      1.1  plunky 	interrupt_psm = -1;
    200      1.1  plunky 	reconnect_initiate = -1;
    201      1.1  plunky 	hid_descriptor = NULL;
    202      1.1  plunky 	hid_length = -1;
    203      1.1  plunky 
    204  1.5.8.1     jym 	while (sdp_get_attr(rec, &attr, &value)) {
    205  1.5.8.1     jym 		switch (attr) {
    206      1.1  plunky 		case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
    207  1.5.8.1     jym 			control_psm = parse_pdl(&value, SDP_UUID_PROTOCOL_L2CAP);
    208      1.1  plunky 			break;
    209      1.1  plunky 
    210      1.1  plunky 		case SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS:
    211  1.5.8.1     jym 			interrupt_psm = parse_apdl(&value, SDP_UUID_PROTOCOL_L2CAP);
    212      1.1  plunky 			break;
    213      1.1  plunky 
    214      1.1  plunky 		case 0x0205: /* HIDReconnectInitiate */
    215  1.5.8.1     jym 			reconnect_initiate = parse_boolean(&value);
    216      1.1  plunky 			break;
    217      1.1  plunky 
    218      1.1  plunky 		case 0x0206: /* HIDDescriptorList */
    219  1.5.8.1     jym 			if (parse_hid_descriptor(&value)) {
    220  1.5.8.1     jym 				hid_descriptor = value.next;
    221  1.5.8.1     jym 				hid_length = value.end - value.next;
    222      1.1  plunky 			}
    223      1.1  plunky 			break;
    224      1.1  plunky 
    225  1.5.8.1     jym 		default:
    226      1.1  plunky 			break;
    227      1.1  plunky 		}
    228      1.1  plunky 	}
    229      1.1  plunky 
    230      1.1  plunky 	if (control_psm == -1
    231      1.1  plunky 	    || interrupt_psm == -1
    232      1.1  plunky 	    || reconnect_initiate == -1
    233      1.1  plunky 	    || hid_descriptor == NULL
    234      1.1  plunky 	    || hid_length == -1)
    235      1.1  plunky 		return ENOATTR;
    236      1.1  plunky 
    237      1.1  plunky 	obj = prop_string_create_cstring_nocopy("bthidev");
    238      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
    239      1.1  plunky 		return errno;
    240      1.1  plunky 
    241      1.2  plunky 	prop_object_release(obj);
    242      1.2  plunky 
    243      1.1  plunky 	obj = prop_number_create_integer(control_psm);
    244      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVcontrolpsm, obj))
    245      1.1  plunky 		return errno;
    246      1.1  plunky 
    247      1.2  plunky 	prop_object_release(obj);
    248      1.2  plunky 
    249      1.1  plunky 	obj = prop_number_create_integer(interrupt_psm);
    250      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVinterruptpsm, obj))
    251      1.1  plunky 		return errno;
    252      1.1  plunky 
    253      1.2  plunky 	prop_object_release(obj);
    254      1.2  plunky 
    255      1.1  plunky 	obj = prop_data_create_data(hid_descriptor, hid_length);
    256      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVdescriptor, obj))
    257      1.1  plunky 		return errno;
    258      1.1  plunky 
    259      1.3  plunky 	mode = hid_mode(obj);
    260      1.3  plunky 	prop_object_release(obj);
    261      1.3  plunky 
    262      1.3  plunky 	obj = prop_string_create_cstring_nocopy(mode);
    263      1.3  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTDEVmode, obj))
    264      1.3  plunky 		return errno;
    265      1.3  plunky 
    266      1.2  plunky 	prop_object_release(obj);
    267      1.2  plunky 
    268      1.1  plunky 	if (!reconnect_initiate) {
    269      1.4   pavel 		obj = prop_bool_create(true);
    270      1.1  plunky 		if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVreconnect, obj))
    271      1.1  plunky 			return errno;
    272      1.2  plunky 
    273      1.2  plunky 		prop_object_release(obj);
    274      1.1  plunky 	}
    275      1.1  plunky 
    276      1.1  plunky 	return 0;
    277      1.1  plunky }
    278      1.1  plunky 
    279      1.1  plunky /*
    280      1.1  plunky  * Configure HSET results
    281      1.1  plunky  */
    282      1.1  plunky static int
    283  1.5.8.1     jym config_hset(prop_dictionary_t dict, sdp_data_t *rec)
    284      1.1  plunky {
    285      1.1  plunky 	prop_object_t obj;
    286  1.5.8.1     jym 	sdp_data_t value;
    287  1.5.8.1     jym 	int32_t channel;
    288  1.5.8.1     jym 	uint16_t attr;
    289      1.1  plunky 
    290      1.1  plunky 	channel = -1;
    291      1.1  plunky 
    292  1.5.8.1     jym 	while (sdp_get_attr(rec, &attr, &value)) {
    293  1.5.8.1     jym 		switch (attr) {
    294      1.1  plunky 		case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
    295  1.5.8.1     jym 			channel = parse_pdl(&value, SDP_UUID_PROTOCOL_RFCOMM);
    296  1.5.8.1     jym 			break;
    297  1.5.8.1     jym 
    298  1.5.8.1     jym 		default:
    299      1.1  plunky 			break;
    300      1.1  plunky 		}
    301      1.1  plunky 	}
    302      1.1  plunky 
    303      1.1  plunky 	if (channel == -1)
    304      1.1  plunky 		return ENOATTR;
    305      1.1  plunky 
    306      1.1  plunky 	obj = prop_string_create_cstring_nocopy("btsco");
    307      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
    308      1.1  plunky 		return errno;
    309      1.1  plunky 
    310      1.2  plunky 	prop_object_release(obj);
    311      1.2  plunky 
    312      1.1  plunky 	obj = prop_number_create_integer(channel);
    313      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTSCOchannel, obj))
    314      1.1  plunky 		return errno;
    315      1.1  plunky 
    316      1.2  plunky 	prop_object_release(obj);
    317      1.2  plunky 
    318      1.1  plunky 	return 0;
    319      1.1  plunky }
    320      1.1  plunky 
    321      1.1  plunky /*
    322      1.1  plunky  * Configure HF results
    323      1.1  plunky  */
    324      1.1  plunky static int
    325  1.5.8.1     jym config_hf(prop_dictionary_t dict, sdp_data_t *rec)
    326      1.1  plunky {
    327      1.1  plunky 	prop_object_t obj;
    328  1.5.8.1     jym 	sdp_data_t value;
    329  1.5.8.1     jym 	int32_t channel;
    330  1.5.8.1     jym 	uint16_t attr;
    331      1.1  plunky 
    332      1.1  plunky 	channel = -1;
    333      1.1  plunky 
    334  1.5.8.1     jym 	while (sdp_get_attr(rec, &attr, &value)) {
    335  1.5.8.1     jym 		switch (attr) {
    336      1.1  plunky 		case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
    337  1.5.8.1     jym 			channel = parse_pdl(&value, SDP_UUID_PROTOCOL_RFCOMM);
    338  1.5.8.1     jym 			break;
    339  1.5.8.1     jym 
    340  1.5.8.1     jym 		default:
    341      1.1  plunky 			break;
    342      1.1  plunky 		}
    343      1.1  plunky 	}
    344      1.1  plunky 
    345      1.1  plunky 	if (channel == -1)
    346      1.1  plunky 		return ENOATTR;
    347      1.1  plunky 
    348      1.1  plunky 	obj = prop_string_create_cstring_nocopy("btsco");
    349      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
    350      1.1  plunky 		return errno;
    351      1.1  plunky 
    352      1.2  plunky 	prop_object_release(obj);
    353      1.2  plunky 
    354      1.4   pavel 	obj = prop_bool_create(true);
    355      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTSCOlisten, obj))
    356      1.1  plunky 		return errno;
    357      1.1  plunky 
    358      1.2  plunky 	prop_object_release(obj);
    359      1.2  plunky 
    360      1.1  plunky 	obj = prop_number_create_integer(channel);
    361      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTSCOchannel, obj))
    362      1.1  plunky 		return errno;
    363      1.1  plunky 
    364      1.2  plunky 	prop_object_release(obj);
    365      1.2  plunky 
    366      1.1  plunky 	return 0;
    367      1.1  plunky }
    368      1.1  plunky 
    369      1.1  plunky /*
    370  1.5.8.1     jym  * Parse HIDDescriptorList . This is a sequence of HIDDescriptors, of which
    371  1.5.8.1     jym  * each is a data element sequence containing, minimally, a ClassDescriptorType
    372  1.5.8.1     jym  * and ClassDescriptorData containing a byte array of data. Any extra elements
    373  1.5.8.1     jym  * should be ignored.
    374      1.1  plunky  *
    375  1.5.8.1     jym  * If a ClassDescriptorType "Report" is found, set SDP data value to the
    376  1.5.8.1     jym  * ClassDescriptorData content and return true. Note that we don't need to
    377  1.5.8.1     jym  * extract the actual length as the SDP data is guaranteed valid.
    378      1.1  plunky  */
    379      1.1  plunky 
    380  1.5.8.1     jym static bool
    381  1.5.8.1     jym parse_hid_descriptor(sdp_data_t *value)
    382      1.1  plunky {
    383  1.5.8.1     jym 	sdp_data_t list, desc;
    384  1.5.8.1     jym 	uintmax_t type;
    385  1.5.8.1     jym 	char *str;
    386  1.5.8.1     jym 	size_t len;
    387  1.5.8.1     jym 
    388  1.5.8.1     jym 	if (!sdp_get_seq(value, &list))
    389  1.5.8.1     jym 		return false;
    390  1.5.8.1     jym 
    391  1.5.8.1     jym 	while (sdp_get_seq(&list, &desc)) {
    392  1.5.8.1     jym 		if (sdp_get_uint(&desc, &type)
    393  1.5.8.1     jym 		    && type == UDESC_REPORT
    394  1.5.8.1     jym 		    && sdp_get_str(&desc, &str, &len)) {
    395  1.5.8.1     jym 			value->next = (uint8_t *)str;
    396  1.5.8.1     jym 			value->end = (uint8_t *)(str + len);
    397  1.5.8.1     jym 			return true;
    398      1.1  plunky 		}
    399      1.1  plunky 	}
    400      1.1  plunky 
    401  1.5.8.1     jym 	return false;
    402  1.5.8.1     jym }
    403      1.1  plunky 
    404  1.5.8.1     jym static int32_t
    405  1.5.8.1     jym parse_boolean(sdp_data_t *value)
    406  1.5.8.1     jym {
    407  1.5.8.1     jym 	bool bv;
    408      1.1  plunky 
    409  1.5.8.1     jym 	if (!sdp_get_bool(value, &bv))
    410  1.5.8.1     jym 		return -1;
    411      1.1  plunky 
    412  1.5.8.1     jym 	return bv;
    413      1.1  plunky }
    414      1.1  plunky 
    415      1.1  plunky /*
    416  1.5.8.1     jym  * The ProtocolDescriptorList attribute describes one or
    417  1.5.8.1     jym  * more protocol stacks that may be used to gain access to
    418  1.5.8.1     jym  * the service dscribed by the service record.
    419  1.5.8.1     jym  *
    420  1.5.8.1     jym  * If the ProtocolDescriptorList describes a single stack,
    421  1.5.8.1     jym  * the attribute value takes the form of a data element
    422  1.5.8.1     jym  * sequence in which each element of the sequence is a
    423  1.5.8.1     jym  * protocol descriptor.
    424      1.1  plunky  *
    425  1.5.8.1     jym  *	seq
    426  1.5.8.1     jym  *	  <list>
    427  1.5.8.1     jym  *
    428  1.5.8.1     jym  * If it is possible for more than one kind of protocol
    429  1.5.8.1     jym  * stack to be used to gain access to the service, the
    430  1.5.8.1     jym  * ProtocolDescriptorList takes the form of a data element
    431  1.5.8.1     jym  * alternative where each member is a data element sequence
    432  1.5.8.1     jym  * consisting of a list of sequences describing each protocol
    433  1.5.8.1     jym  *
    434  1.5.8.1     jym  *	alt
    435  1.5.8.1     jym  *	  seq
    436  1.5.8.1     jym  *	    <list>
    437  1.5.8.1     jym  *	  seq
    438  1.5.8.1     jym  *	    <list>
    439  1.5.8.1     jym  *
    440  1.5.8.1     jym  * Each ProtocolDescriptorList is a list containing a sequence for
    441  1.5.8.1     jym  * each protocol, where each sequence contains the protocol UUUID
    442  1.5.8.1     jym  * and any protocol specific parameters.
    443  1.5.8.1     jym  *
    444  1.5.8.1     jym  *	seq
    445  1.5.8.1     jym  *	  uuid		L2CAP
    446  1.5.8.1     jym  *	  uint16	psm
    447  1.5.8.1     jym  *	seq
    448  1.5.8.1     jym  *	  uuid		RFCOMM
    449  1.5.8.1     jym  *	  uint8		channel
    450  1.5.8.1     jym  *
    451  1.5.8.1     jym  * We want to extract the ProtocolSpecificParameter#1 for the
    452  1.5.8.1     jym  * given protocol, which will be an unsigned int.
    453      1.1  plunky  */
    454      1.1  plunky static int32_t
    455  1.5.8.1     jym parse_pdl_param(sdp_data_t *pdl, uint16_t proto)
    456      1.1  plunky {
    457  1.5.8.1     jym 	sdp_data_t seq;
    458  1.5.8.1     jym 	uintmax_t param;
    459      1.1  plunky 
    460  1.5.8.1     jym 	while (sdp_get_seq(pdl, &seq)) {
    461  1.5.8.1     jym 		if (!sdp_match_uuid16(&seq, proto))
    462  1.5.8.1     jym 			continue;
    463      1.1  plunky 
    464  1.5.8.1     jym 		if (sdp_get_uint(&seq, &param))
    465  1.5.8.1     jym 			return param;
    466      1.1  plunky 
    467  1.5.8.1     jym 		break;
    468      1.1  plunky 	}
    469      1.1  plunky 
    470  1.5.8.1     jym 	return -1;
    471      1.1  plunky }
    472      1.1  plunky 
    473      1.1  plunky static int32_t
    474  1.5.8.1     jym parse_pdl(sdp_data_t *value, uint16_t proto)
    475      1.1  plunky {
    476  1.5.8.1     jym 	sdp_data_t seq;
    477  1.5.8.1     jym 	int32_t param = -1;
    478  1.5.8.1     jym 
    479  1.5.8.1     jym 	sdp_get_alt(value, value);	/* strip any alt header */
    480      1.1  plunky 
    481  1.5.8.1     jym 	while (param == -1 && sdp_get_seq(value, &seq))
    482  1.5.8.1     jym 		param = parse_pdl_param(&seq, proto);
    483  1.5.8.1     jym 
    484  1.5.8.1     jym 	return param;
    485      1.1  plunky }
    486      1.1  plunky 
    487      1.1  plunky /*
    488  1.5.8.1     jym  * Parse AdditionalProtocolDescriptorList
    489      1.1  plunky  */
    490      1.1  plunky static int32_t
    491  1.5.8.1     jym parse_apdl(sdp_data_t *value, uint16_t proto)
    492      1.1  plunky {
    493  1.5.8.1     jym 	sdp_data_t seq;
    494  1.5.8.1     jym 	int32_t param = -1;
    495      1.1  plunky 
    496  1.5.8.1     jym 	sdp_get_seq(value, value);	/* strip seq header */
    497      1.1  plunky 
    498  1.5.8.1     jym 	while (param == -1 && sdp_get_seq(value, &seq))
    499  1.5.8.1     jym 		param = parse_pdl_param(&seq, proto);
    500      1.1  plunky 
    501  1.5.8.1     jym 	return param;
    502      1.1  plunky }
    503      1.3  plunky 
    504      1.3  plunky /*
    505      1.3  plunky  * return appropriate mode for HID descriptor
    506      1.3  plunky  */
    507      1.3  plunky const char *
    508      1.3  plunky hid_mode(prop_data_t desc)
    509      1.3  plunky {
    510      1.3  plunky 	report_desc_t r;
    511      1.3  plunky 	hid_data_t d;
    512      1.3  plunky 	struct hid_item h;
    513      1.3  plunky 	const char *mode;
    514      1.3  plunky 
    515      1.3  plunky 	hid_init(NULL);
    516      1.3  plunky 
    517      1.3  plunky 	mode = BTDEVauth;	/* default */
    518      1.3  plunky 
    519      1.3  plunky 	r = hid_use_report_desc(prop_data_data_nocopy(desc),
    520      1.3  plunky 				prop_data_size(desc));
    521      1.3  plunky 	if (r == NULL)
    522      1.3  plunky 		err(EXIT_FAILURE, "hid_use_report_desc");
    523      1.3  plunky 
    524      1.3  plunky 	d = hid_start_parse(r, ~0, -1);
    525      1.3  plunky 	while (hid_get_item(d, &h) > 0) {
    526      1.3  plunky 		if (h.kind == hid_collection
    527      1.3  plunky 		    && HID_PAGE(h.usage) == HUP_GENERIC_DESKTOP
    528      1.3  plunky 		    && HID_USAGE(h.usage) == HUG_KEYBOARD)
    529      1.3  plunky 			mode = BTDEVencrypt;
    530      1.3  plunky 	}
    531      1.3  plunky 
    532      1.3  plunky 	hid_end_parse(d);
    533      1.3  plunky 	hid_dispose_report_desc(r);
    534      1.3  plunky 
    535      1.3  plunky 	return mode;
    536      1.3  plunky }
    537