Home | History | Annotate | Line # | Download | only in btdevctl
sdp.c revision 1.4.8.1
      1  1.4.8.1    yamt /*	$NetBSD: sdp.c,v 1.4.8.1 2008/05/18 12:36:14 yamt 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.1  plunky  * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin (at) yahoo.com>
     33      1.1  plunky  * All rights reserved.
     34      1.1  plunky  *
     35      1.1  plunky  * Redistribution and use in source and binary forms, with or without
     36      1.1  plunky  * modification, are permitted provided that the following conditions
     37      1.1  plunky  * are met:
     38      1.1  plunky  * 1. Redistributions of source code must retain the above copyright
     39      1.1  plunky  *    notice, this list of conditions and the following disclaimer.
     40      1.1  plunky  * 2. Redistributions in binary form must reproduce the above copyright
     41      1.1  plunky  *    notice, this list of conditions and the following disclaimer in the
     42      1.1  plunky  *    documentation and/or other materials provided with the distribution.
     43      1.1  plunky  *
     44      1.1  plunky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     45      1.1  plunky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     46      1.1  plunky  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     47      1.1  plunky  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     48      1.1  plunky  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     49      1.1  plunky  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     50      1.1  plunky  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     51      1.1  plunky  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     52      1.1  plunky  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     53      1.1  plunky  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     54      1.1  plunky  * SUCH DAMAGE.
     55      1.1  plunky  */
     56      1.1  plunky 
     57      1.1  plunky #include <sys/cdefs.h>
     58  1.4.8.1    yamt __RCSID("$NetBSD: sdp.c,v 1.4.8.1 2008/05/18 12:36:14 yamt Exp $");
     59      1.1  plunky 
     60      1.1  plunky #include <sys/types.h>
     61      1.1  plunky 
     62      1.1  plunky #include <dev/bluetooth/btdev.h>
     63      1.1  plunky #include <dev/bluetooth/bthidev.h>
     64      1.1  plunky #include <dev/bluetooth/btsco.h>
     65      1.1  plunky #include <dev/usb/usb.h>
     66      1.1  plunky #include <dev/usb/usbhid.h>
     67      1.1  plunky 
     68      1.1  plunky #include <prop/proplib.h>
     69      1.1  plunky 
     70      1.1  plunky #include <bluetooth.h>
     71      1.1  plunky #include <err.h>
     72      1.1  plunky #include <errno.h>
     73      1.1  plunky #include <sdp.h>
     74      1.1  plunky #include <stdlib.h>
     75      1.1  plunky #include <usbhid.h>
     76      1.1  plunky 
     77      1.1  plunky #include "btdevctl.h"
     78      1.1  plunky 
     79      1.1  plunky static int32_t parse_l2cap_psm(sdp_attr_t *);
     80      1.1  plunky static int32_t parse_rfcomm_channel(sdp_attr_t *);
     81      1.1  plunky static int32_t parse_hid_descriptor(sdp_attr_t *);
     82      1.1  plunky static int32_t parse_boolean(sdp_attr_t *);
     83      1.1  plunky 
     84      1.1  plunky static int config_hid(prop_dictionary_t);
     85      1.1  plunky static int config_hset(prop_dictionary_t);
     86      1.1  plunky static int config_hf(prop_dictionary_t);
     87      1.1  plunky 
     88      1.1  plunky uint16_t hid_services[] = {
     89      1.1  plunky 	SDP_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE
     90      1.1  plunky };
     91      1.1  plunky 
     92      1.1  plunky uint32_t hid_attrs[] = {
     93      1.1  plunky 	SDP_ATTR_RANGE(	SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
     94      1.1  plunky 			SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST),
     95      1.1  plunky 	SDP_ATTR_RANGE( SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS,
     96      1.1  plunky 			SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS),
     97      1.1  plunky 	SDP_ATTR_RANGE(	0x0205,		/* HIDReconnectInitiate */
     98      1.1  plunky 			0x0206),	/* HIDDescriptorList */
     99      1.1  plunky 	SDP_ATTR_RANGE(	0x0209,		/* HIDBatteryPower */
    100      1.1  plunky 			0x0209),
    101      1.1  plunky 	SDP_ATTR_RANGE(	0x020d,		/* HIDNormallyConnectable */
    102      1.1  plunky 			0x020d)
    103      1.1  plunky };
    104      1.1  plunky 
    105      1.1  plunky uint16_t hset_services[] = {
    106      1.1  plunky 	SDP_SERVICE_CLASS_HEADSET
    107      1.1  plunky };
    108      1.1  plunky 
    109      1.1  plunky uint32_t hset_attrs[] = {
    110      1.1  plunky 	SDP_ATTR_RANGE(	SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
    111      1.1  plunky 			SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST),
    112      1.1  plunky };
    113      1.1  plunky 
    114      1.1  plunky uint16_t hf_services[] = {
    115      1.1  plunky 	SDP_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY
    116      1.1  plunky };
    117      1.1  plunky 
    118      1.1  plunky uint32_t hf_attrs[] = {
    119      1.1  plunky 	SDP_ATTR_RANGE(	SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
    120      1.1  plunky 			SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST),
    121      1.1  plunky };
    122      1.1  plunky 
    123      1.1  plunky static struct {
    124      1.1  plunky 	const char		*name;
    125      1.1  plunky 	int			(*handler)(prop_dictionary_t);
    126      1.1  plunky 	const char		*description;
    127      1.1  plunky 	uint16_t		*services;
    128      1.1  plunky 	int			nservices;
    129      1.1  plunky 	uint32_t		*attrs;
    130      1.1  plunky 	int			nattrs;
    131      1.1  plunky } cfgtype[] = {
    132      1.1  plunky     {
    133      1.1  plunky 	"HID",		config_hid,	"Human Interface Device",
    134  1.4.8.1    yamt 	hid_services,	__arraycount(hid_services),
    135  1.4.8.1    yamt 	hid_attrs,	__arraycount(hid_attrs),
    136      1.1  plunky     },
    137      1.1  plunky     {
    138      1.1  plunky 	"HSET",		config_hset,	"Headset",
    139  1.4.8.1    yamt 	hset_services,	__arraycount(hset_services),
    140  1.4.8.1    yamt 	hset_attrs,	__arraycount(hset_attrs),
    141      1.1  plunky     },
    142      1.1  plunky     {
    143      1.1  plunky 	"HF",		config_hf,	"Handsfree",
    144  1.4.8.1    yamt 	hf_services,	__arraycount(hf_services),
    145  1.4.8.1    yamt 	hf_attrs,	__arraycount(hf_attrs),
    146      1.1  plunky     },
    147      1.1  plunky };
    148      1.1  plunky 
    149      1.1  plunky static sdp_attr_t	values[8];
    150  1.4.8.1    yamt static uint8_t		buffer[__arraycount(values)][512];
    151      1.1  plunky 
    152      1.1  plunky prop_dictionary_t
    153      1.1  plunky cfg_query(bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
    154      1.1  plunky {
    155      1.1  plunky 	prop_dictionary_t dict;
    156      1.1  plunky 	void *ss;
    157      1.1  plunky 	int rv, i;
    158      1.1  plunky 
    159      1.1  plunky 	dict = prop_dictionary_create();
    160      1.1  plunky 	if (dict == NULL)
    161      1.1  plunky 		return NULL;
    162      1.1  plunky 
    163  1.4.8.1    yamt 	for (i = 0; i < __arraycount(values); i++) {
    164      1.1  plunky 		values[i].flags = SDP_ATTR_INVALID;
    165      1.1  plunky 		values[i].attr = 0;
    166      1.1  plunky 		values[i].vlen = sizeof(buffer[i]);
    167      1.1  plunky 		values[i].value = buffer[i];
    168      1.1  plunky 	}
    169      1.1  plunky 
    170  1.4.8.1    yamt 	for (i = 0; i < __arraycount(cfgtype); i++) {
    171      1.1  plunky 		if (strcasecmp(service, cfgtype[i].name) == 0) {
    172      1.1  plunky 			ss = sdp_open(laddr, raddr);
    173      1.1  plunky 
    174      1.1  plunky 			if (ss == NULL || (errno = sdp_error(ss)) != 0)
    175      1.1  plunky 				return NULL;
    176      1.1  plunky 
    177      1.1  plunky 			rv = sdp_search(ss,
    178      1.1  plunky 				cfgtype[i].nservices, cfgtype[i].services,
    179      1.1  plunky 				cfgtype[i].nattrs, cfgtype[i].attrs,
    180  1.4.8.1    yamt 				__arraycount(values), values);
    181      1.1  plunky 
    182      1.1  plunky 			if (rv != 0) {
    183      1.1  plunky 				errno = sdp_error(ss);
    184      1.1  plunky 				return NULL;
    185      1.1  plunky 			}
    186      1.1  plunky 			sdp_close(ss);
    187      1.1  plunky 
    188      1.1  plunky 			rv = (*cfgtype[i].handler)(dict);
    189      1.1  plunky 			if (rv != 0)
    190      1.1  plunky 				return NULL;
    191      1.1  plunky 
    192      1.1  plunky 			return dict;
    193      1.1  plunky 		}
    194      1.1  plunky 	}
    195      1.1  plunky 
    196      1.1  plunky 	printf("Known config types:\n");
    197  1.4.8.1    yamt 	for (i = 0; i < __arraycount(cfgtype); i++)
    198      1.1  plunky 		printf("\t%s\t%s\n", cfgtype[i].name, cfgtype[i].description);
    199      1.1  plunky 
    200      1.1  plunky 	exit(EXIT_FAILURE);
    201      1.1  plunky }
    202      1.1  plunky 
    203      1.1  plunky /*
    204      1.1  plunky  * Configure HID results
    205      1.1  plunky  */
    206      1.1  plunky static int
    207      1.1  plunky config_hid(prop_dictionary_t dict)
    208      1.1  plunky {
    209      1.1  plunky 	prop_object_t obj;
    210      1.1  plunky 	int32_t control_psm, interrupt_psm,
    211      1.1  plunky 		reconnect_initiate, battery_power,
    212      1.1  plunky 		normally_connectable, hid_length;
    213      1.1  plunky 	uint8_t *hid_descriptor;
    214      1.3  plunky 	const char *mode;
    215      1.1  plunky 	int i;
    216      1.1  plunky 
    217      1.1  plunky 	control_psm = -1;
    218      1.1  plunky 	interrupt_psm = -1;
    219      1.1  plunky 	reconnect_initiate = -1;
    220      1.1  plunky 	normally_connectable = 0;
    221      1.1  plunky 	battery_power = 0;
    222      1.1  plunky 	hid_descriptor = NULL;
    223      1.1  plunky 	hid_length = -1;
    224      1.1  plunky 
    225  1.4.8.1    yamt 	for (i = 0; i < __arraycount(values); i++) {
    226      1.1  plunky 		if (values[i].flags != SDP_ATTR_OK)
    227      1.1  plunky 			continue;
    228      1.1  plunky 
    229      1.1  plunky 		switch (values[i].attr) {
    230      1.1  plunky 		case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
    231      1.1  plunky 			control_psm = parse_l2cap_psm(&values[i]);
    232      1.1  plunky 			break;
    233      1.1  plunky 
    234      1.1  plunky 		case SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS:
    235      1.1  plunky 			interrupt_psm = parse_l2cap_psm(&values[i]);
    236      1.1  plunky 			break;
    237      1.1  plunky 
    238      1.1  plunky 		case 0x0205: /* HIDReconnectInitiate */
    239      1.1  plunky 			reconnect_initiate = parse_boolean(&values[i]);
    240      1.1  plunky 			break;
    241      1.1  plunky 
    242      1.1  plunky 		case 0x0206: /* HIDDescriptorList */
    243      1.1  plunky 			if (parse_hid_descriptor(&values[i]) == 0) {
    244      1.1  plunky 				hid_descriptor = values[i].value;
    245      1.1  plunky 				hid_length = values[i].vlen;
    246      1.1  plunky 			}
    247      1.1  plunky 			break;
    248      1.1  plunky 
    249      1.1  plunky 		case 0x0209: /* HIDBatteryPower */
    250      1.1  plunky 			battery_power = parse_boolean(&values[i]);
    251      1.1  plunky 			break;
    252      1.1  plunky 
    253      1.1  plunky 		case 0x020d: /* HIDNormallyConnectable */
    254      1.1  plunky 			normally_connectable = parse_boolean(&values[i]);
    255      1.1  plunky 			break;
    256      1.1  plunky 		}
    257      1.1  plunky 	}
    258      1.1  plunky 
    259      1.1  plunky 	if (control_psm == -1
    260      1.1  plunky 	    || interrupt_psm == -1
    261      1.1  plunky 	    || reconnect_initiate == -1
    262      1.1  plunky 	    || hid_descriptor == NULL
    263      1.1  plunky 	    || hid_length == -1)
    264      1.1  plunky 		return ENOATTR;
    265      1.1  plunky 
    266      1.1  plunky 	obj = prop_string_create_cstring_nocopy("bthidev");
    267      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
    268      1.1  plunky 		return errno;
    269      1.1  plunky 
    270      1.2  plunky 	prop_object_release(obj);
    271      1.2  plunky 
    272      1.1  plunky 	obj = prop_number_create_integer(control_psm);
    273      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVcontrolpsm, obj))
    274      1.1  plunky 		return errno;
    275      1.1  plunky 
    276      1.2  plunky 	prop_object_release(obj);
    277      1.2  plunky 
    278      1.1  plunky 	obj = prop_number_create_integer(interrupt_psm);
    279      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVinterruptpsm, obj))
    280      1.1  plunky 		return errno;
    281      1.1  plunky 
    282      1.2  plunky 	prop_object_release(obj);
    283      1.2  plunky 
    284      1.1  plunky 	obj = prop_data_create_data(hid_descriptor, hid_length);
    285      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVdescriptor, obj))
    286      1.1  plunky 		return errno;
    287      1.1  plunky 
    288      1.3  plunky 	mode = hid_mode(obj);
    289      1.3  plunky 	prop_object_release(obj);
    290      1.3  plunky 
    291      1.3  plunky 	obj = prop_string_create_cstring_nocopy(mode);
    292      1.3  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTDEVmode, obj))
    293      1.3  plunky 		return errno;
    294      1.3  plunky 
    295      1.2  plunky 	prop_object_release(obj);
    296      1.2  plunky 
    297      1.1  plunky 	if (!reconnect_initiate) {
    298      1.4   pavel 		obj = prop_bool_create(true);
    299      1.1  plunky 		if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVreconnect, obj))
    300      1.1  plunky 			return errno;
    301      1.2  plunky 
    302      1.2  plunky 		prop_object_release(obj);
    303      1.1  plunky 	}
    304      1.1  plunky 
    305      1.1  plunky 	return 0;
    306      1.1  plunky }
    307      1.1  plunky 
    308      1.1  plunky /*
    309      1.1  plunky  * Configure HSET results
    310      1.1  plunky  */
    311      1.1  plunky static int
    312      1.1  plunky config_hset(prop_dictionary_t dict)
    313      1.1  plunky {
    314      1.1  plunky 	prop_object_t obj;
    315      1.1  plunky 	uint32_t channel;
    316      1.1  plunky 	int i;
    317      1.1  plunky 
    318      1.1  plunky 	channel = -1;
    319      1.1  plunky 
    320  1.4.8.1    yamt 	for (i = 0; i < __arraycount(values); i++) {
    321      1.1  plunky 		if (values[i].flags != SDP_ATTR_OK)
    322      1.1  plunky 			continue;
    323      1.1  plunky 
    324      1.1  plunky 		switch (values[i].attr) {
    325      1.1  plunky 		case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
    326      1.1  plunky 			channel = parse_rfcomm_channel(&values[i]);
    327      1.1  plunky 			break;
    328      1.1  plunky 		}
    329      1.1  plunky 	}
    330      1.1  plunky 
    331      1.1  plunky 	if (channel == -1)
    332      1.1  plunky 		return ENOATTR;
    333      1.1  plunky 
    334      1.1  plunky 	obj = prop_string_create_cstring_nocopy("btsco");
    335      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
    336      1.1  plunky 		return errno;
    337      1.1  plunky 
    338      1.2  plunky 	prop_object_release(obj);
    339      1.2  plunky 
    340      1.1  plunky 	obj = prop_number_create_integer(channel);
    341      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTSCOchannel, obj))
    342      1.1  plunky 		return errno;
    343      1.1  plunky 
    344      1.2  plunky 	prop_object_release(obj);
    345      1.2  plunky 
    346      1.1  plunky 	return 0;
    347      1.1  plunky }
    348      1.1  plunky 
    349      1.1  plunky /*
    350      1.1  plunky  * Configure HF results
    351      1.1  plunky  */
    352      1.1  plunky static int
    353      1.1  plunky config_hf(prop_dictionary_t dict)
    354      1.1  plunky {
    355      1.1  plunky 	prop_object_t obj;
    356      1.1  plunky 	uint32_t channel;
    357      1.1  plunky 	int i;
    358      1.1  plunky 
    359      1.1  plunky 	channel = -1;
    360      1.1  plunky 
    361  1.4.8.1    yamt 	for (i = 0; i < __arraycount(values); i++) {
    362      1.1  plunky 		if (values[i].flags != SDP_ATTR_OK)
    363      1.1  plunky 			continue;
    364      1.1  plunky 
    365      1.1  plunky 		switch (values[i].attr) {
    366      1.1  plunky 		case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
    367      1.1  plunky 			channel = parse_rfcomm_channel(&values[i]);
    368      1.1  plunky 			break;
    369      1.1  plunky 		}
    370      1.1  plunky 	}
    371      1.1  plunky 
    372      1.1  plunky 	if (channel == -1)
    373      1.1  plunky 		return ENOATTR;
    374      1.1  plunky 
    375      1.1  plunky 	obj = prop_string_create_cstring_nocopy("btsco");
    376      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
    377      1.1  plunky 		return errno;
    378      1.1  plunky 
    379      1.2  plunky 	prop_object_release(obj);
    380      1.2  plunky 
    381      1.4   pavel 	obj = prop_bool_create(true);
    382      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTSCOlisten, obj))
    383      1.1  plunky 		return errno;
    384      1.1  plunky 
    385      1.2  plunky 	prop_object_release(obj);
    386      1.2  plunky 
    387      1.1  plunky 	obj = prop_number_create_integer(channel);
    388      1.1  plunky 	if (obj == NULL || !prop_dictionary_set(dict, BTSCOchannel, obj))
    389      1.1  plunky 		return errno;
    390      1.1  plunky 
    391      1.2  plunky 	prop_object_release(obj);
    392      1.2  plunky 
    393      1.1  plunky 	return 0;
    394      1.1  plunky }
    395      1.1  plunky 
    396      1.1  plunky /*
    397      1.1  plunky  * Parse [additional] protocol descriptor list for L2CAP PSM
    398      1.1  plunky  *
    399      1.1  plunky  * seq8 len8				2
    400      1.1  plunky  *	seq8 len8			2
    401      1.1  plunky  *		uuid16 value16		3	L2CAP
    402      1.1  plunky  *		uint16 value16		3	PSM
    403      1.1  plunky  *	seq8 len8			2
    404      1.1  plunky  *		uuid16 value16		3	HID Protocol
    405      1.1  plunky  *				      ===
    406      1.1  plunky  *				       15
    407      1.1  plunky  */
    408      1.1  plunky 
    409      1.1  plunky static int32_t
    410      1.1  plunky parse_l2cap_psm(sdp_attr_t *a)
    411      1.1  plunky {
    412      1.1  plunky 	uint8_t	*ptr = a->value;
    413      1.1  plunky 	uint8_t	*end = a->value + a->vlen;
    414      1.1  plunky 	int32_t	 type, len, uuid, psm;
    415      1.1  plunky 
    416      1.1  plunky 	if (end - ptr < 15)
    417      1.1  plunky 		return (-1);
    418      1.1  plunky 
    419      1.1  plunky 	if (a->attr == SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS) {
    420      1.1  plunky 		SDP_GET8(type, ptr);
    421      1.1  plunky 		switch (type) {
    422      1.1  plunky 		case SDP_DATA_SEQ8:
    423      1.1  plunky 			SDP_GET8(len, ptr);
    424      1.1  plunky 			break;
    425      1.1  plunky 
    426      1.1  plunky 		case SDP_DATA_SEQ16:
    427      1.1  plunky 			SDP_GET16(len, ptr);
    428      1.1  plunky 			break;
    429      1.1  plunky 
    430      1.1  plunky 		case SDP_DATA_SEQ32:
    431      1.1  plunky 			SDP_GET32(len, ptr);
    432      1.1  plunky 			break;
    433      1.1  plunky 
    434      1.1  plunky 		default:
    435      1.1  plunky 			return (-1);
    436      1.1  plunky 		}
    437      1.1  plunky 		if (ptr + len > end)
    438      1.1  plunky 			return (-1);
    439      1.1  plunky 	}
    440      1.1  plunky 
    441      1.1  plunky 	SDP_GET8(type, ptr);
    442      1.1  plunky 	switch (type) {
    443      1.1  plunky 	case SDP_DATA_SEQ8:
    444      1.1  plunky 		SDP_GET8(len, ptr);
    445      1.1  plunky 		break;
    446      1.1  plunky 
    447      1.1  plunky 	case SDP_DATA_SEQ16:
    448      1.1  plunky 		SDP_GET16(len, ptr);
    449      1.1  plunky 		break;
    450      1.1  plunky 
    451      1.1  plunky 	case SDP_DATA_SEQ32:
    452      1.1  plunky 		SDP_GET32(len, ptr);
    453      1.1  plunky 		break;
    454      1.1  plunky 
    455      1.1  plunky 	default:
    456      1.1  plunky 		return (-1);
    457      1.1  plunky 	}
    458      1.1  plunky 	if (ptr + len > end)
    459      1.1  plunky 		return (-1);
    460      1.1  plunky 
    461      1.1  plunky 	/* Protocol */
    462      1.1  plunky 	SDP_GET8(type, ptr);
    463      1.1  plunky 	switch (type) {
    464      1.1  plunky 	case SDP_DATA_SEQ8:
    465      1.1  plunky 		SDP_GET8(len, ptr);
    466      1.1  plunky 		break;
    467      1.1  plunky 
    468      1.1  plunky 	case SDP_DATA_SEQ16:
    469      1.1  plunky 		SDP_GET16(len, ptr);
    470      1.1  plunky 		break;
    471      1.1  plunky 
    472      1.1  plunky 	case SDP_DATA_SEQ32:
    473      1.1  plunky 		SDP_GET32(len, ptr);
    474      1.1  plunky 		break;
    475      1.1  plunky 
    476      1.1  plunky 	default:
    477      1.1  plunky 		return (-1);
    478      1.1  plunky 	}
    479      1.1  plunky 	if (ptr + len > end)
    480      1.1  plunky 		return (-1);
    481      1.1  plunky 
    482      1.1  plunky 	/* UUID */
    483      1.1  plunky 	if (ptr + 3 > end)
    484      1.1  plunky 		return (-1);
    485      1.1  plunky 	SDP_GET8(type, ptr);
    486      1.1  plunky 	switch (type) {
    487      1.1  plunky 	case SDP_DATA_UUID16:
    488      1.1  plunky 		SDP_GET16(uuid, ptr);
    489      1.1  plunky 		if (uuid != SDP_UUID_PROTOCOL_L2CAP)
    490      1.1  plunky 			return (-1);
    491      1.1  plunky 		break;
    492      1.1  plunky 
    493      1.1  plunky 	case SDP_DATA_UUID32:  /* XXX FIXME can we have 32-bit UUID */
    494      1.1  plunky 	case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
    495      1.1  plunky 	default:
    496      1.1  plunky 		return (-1);
    497      1.1  plunky 	}
    498      1.1  plunky 
    499      1.1  plunky 	/* PSM */
    500      1.1  plunky 	if (ptr + 3 > end)
    501      1.1  plunky 		return (-1);
    502      1.1  plunky 	SDP_GET8(type, ptr);
    503      1.1  plunky 	if (type != SDP_DATA_UINT16)
    504      1.1  plunky 		return (-1);
    505      1.1  plunky 	SDP_GET16(psm, ptr);
    506      1.1  plunky 
    507      1.1  plunky 	return (psm);
    508      1.1  plunky }
    509      1.1  plunky 
    510      1.1  plunky /*
    511      1.1  plunky  * Parse HID descriptor string
    512      1.1  plunky  *
    513      1.1  plunky  * seq8 len8			2
    514      1.1  plunky  *	seq8 len8		2
    515      1.1  plunky  *		uint8 value8	2
    516      1.1  plunky  *		str value	3
    517      1.1  plunky  *			      ===
    518      1.1  plunky  *			        9
    519      1.1  plunky  */
    520      1.1  plunky 
    521      1.1  plunky static int32_t
    522      1.1  plunky parse_hid_descriptor(sdp_attr_t *a)
    523      1.1  plunky {
    524      1.1  plunky 	uint8_t	*ptr = a->value;
    525      1.1  plunky 	uint8_t	*end = a->value + a->vlen;
    526      1.1  plunky 	int32_t	 type, len, descriptor_type;
    527      1.1  plunky 
    528      1.1  plunky 	if (end - ptr < 9)
    529      1.1  plunky 		return (-1);
    530      1.1  plunky 
    531      1.1  plunky 	SDP_GET8(type, ptr);
    532      1.1  plunky 	switch (type) {
    533      1.1  plunky 	case SDP_DATA_SEQ8:
    534      1.1  plunky 		SDP_GET8(len, ptr);
    535      1.1  plunky 		break;
    536      1.1  plunky 
    537      1.1  plunky 	case SDP_DATA_SEQ16:
    538      1.1  plunky 		SDP_GET16(len, ptr);
    539      1.1  plunky 		break;
    540      1.1  plunky 
    541      1.1  plunky 	case SDP_DATA_SEQ32:
    542      1.1  plunky 		SDP_GET32(len, ptr);
    543      1.1  plunky 		break;
    544      1.1  plunky 
    545      1.1  plunky 	default:
    546      1.1  plunky 		return (-1);
    547      1.1  plunky 	}
    548      1.1  plunky 	if (ptr + len > end)
    549      1.1  plunky 		return (-1);
    550      1.1  plunky 
    551      1.1  plunky 	while (ptr < end) {
    552      1.1  plunky 		/* Descriptor */
    553      1.1  plunky 		SDP_GET8(type, ptr);
    554      1.1  plunky 		switch (type) {
    555      1.1  plunky 		case SDP_DATA_SEQ8:
    556      1.1  plunky 			if (ptr + 1 > end)
    557      1.1  plunky 				return (-1);
    558      1.1  plunky 			SDP_GET8(len, ptr);
    559      1.1  plunky 			break;
    560      1.1  plunky 
    561      1.1  plunky 		case SDP_DATA_SEQ16:
    562      1.1  plunky 			if (ptr + 2 > end)
    563      1.1  plunky 				return (-1);
    564      1.1  plunky 			SDP_GET16(len, ptr);
    565      1.1  plunky 			break;
    566      1.1  plunky 
    567      1.1  plunky 		case SDP_DATA_SEQ32:
    568      1.1  plunky 			if (ptr + 4 > end)
    569      1.1  plunky 				return (-1);
    570      1.1  plunky 			SDP_GET32(len, ptr);
    571      1.1  plunky 			break;
    572      1.1  plunky 
    573      1.1  plunky 		default:
    574      1.1  plunky 			return (-1);
    575      1.1  plunky 		}
    576      1.1  plunky 
    577      1.1  plunky 		/* Descripor type */
    578      1.1  plunky 		if (ptr + 1 > end)
    579      1.1  plunky 			return (-1);
    580      1.1  plunky 		SDP_GET8(type, ptr);
    581      1.1  plunky 		if (type != SDP_DATA_UINT8 || ptr + 1 > end)
    582      1.1  plunky 			return (-1);
    583      1.1  plunky 		SDP_GET8(descriptor_type, ptr);
    584      1.1  plunky 
    585      1.1  plunky 		/* Descriptor value */
    586      1.1  plunky 		if (ptr + 1 > end)
    587      1.1  plunky 			return (-1);
    588      1.1  plunky 		SDP_GET8(type, ptr);
    589      1.1  plunky 		switch (type) {
    590      1.1  plunky 		case SDP_DATA_STR8:
    591      1.1  plunky 			if (ptr + 1 > end)
    592      1.1  plunky 				return (-1);
    593      1.1  plunky 			SDP_GET8(len, ptr);
    594      1.1  plunky 			break;
    595      1.1  plunky 
    596      1.1  plunky 		case SDP_DATA_STR16:
    597      1.1  plunky 			if (ptr + 2 > end)
    598      1.1  plunky 				return (-1);
    599      1.1  plunky 			SDP_GET16(len, ptr);
    600      1.1  plunky 			break;
    601      1.1  plunky 
    602      1.1  plunky 		case SDP_DATA_STR32:
    603      1.1  plunky 			if (ptr + 4 > end)
    604      1.1  plunky 				return (-1);
    605      1.1  plunky 			SDP_GET32(len, ptr);
    606      1.1  plunky 			break;
    607      1.1  plunky 
    608      1.1  plunky 		default:
    609      1.1  plunky 			return (-1);
    610      1.1  plunky 		}
    611      1.1  plunky 		if (ptr + len > end)
    612      1.1  plunky 			return (-1);
    613      1.1  plunky 
    614      1.1  plunky 		if (descriptor_type == UDESC_REPORT && len > 0) {
    615      1.1  plunky 			a->value = ptr;
    616      1.1  plunky 			a->vlen = len;
    617      1.1  plunky 
    618      1.1  plunky 			return (0);
    619      1.1  plunky 		}
    620      1.1  plunky 
    621      1.1  plunky 		ptr += len;
    622      1.1  plunky 	}
    623      1.1  plunky 
    624      1.1  plunky 	return (-1);
    625      1.1  plunky }
    626      1.1  plunky 
    627      1.1  plunky /*
    628      1.1  plunky  * Parse boolean value
    629      1.1  plunky  *
    630      1.1  plunky  * bool8 int8
    631      1.1  plunky  */
    632      1.1  plunky 
    633      1.1  plunky static int32_t
    634      1.1  plunky parse_boolean(sdp_attr_t *a)
    635      1.1  plunky {
    636      1.1  plunky 	if (a->vlen != 2 || a->value[0] != SDP_DATA_BOOL)
    637      1.1  plunky 		return (-1);
    638      1.1  plunky 
    639      1.1  plunky 	return (a->value[1]);
    640      1.1  plunky }
    641      1.1  plunky 
    642      1.1  plunky /*
    643      1.1  plunky  * Parse protocol descriptor list for the RFCOMM channel
    644      1.1  plunky  *
    645      1.1  plunky  * seq8 len8				2
    646      1.1  plunky  *	seq8 len8			2
    647      1.1  plunky  *		uuid16 value16		3	L2CAP
    648      1.1  plunky  *	seq8 len8			2
    649      1.1  plunky  *		uuid16 value16		3	RFCOMM
    650      1.1  plunky  *		uint8 value8		2	channel
    651      1.1  plunky  *				      ===
    652      1.1  plunky  *				       14
    653      1.1  plunky  */
    654      1.1  plunky 
    655      1.1  plunky static int32_t
    656      1.1  plunky parse_rfcomm_channel(sdp_attr_t *a)
    657      1.1  plunky {
    658      1.1  plunky 	uint8_t	*ptr = a->value;
    659      1.1  plunky 	uint8_t	*end = a->value + a->vlen;
    660      1.1  plunky 	int32_t	 type, len, uuid, channel;
    661      1.1  plunky 
    662      1.1  plunky 	if (end - ptr < 14)
    663      1.1  plunky 		return (-1);
    664      1.1  plunky 
    665      1.1  plunky 	SDP_GET8(type, ptr);
    666      1.1  plunky 	switch (type) {
    667      1.1  plunky 	case SDP_DATA_SEQ8:
    668      1.1  plunky 		SDP_GET8(len, ptr);
    669      1.1  plunky 		break;
    670      1.1  plunky 
    671      1.1  plunky 	case SDP_DATA_SEQ16:
    672      1.1  plunky 		SDP_GET16(len, ptr);
    673      1.1  plunky 		break;
    674      1.1  plunky 
    675      1.1  plunky 	case SDP_DATA_SEQ32:
    676      1.1  plunky 		SDP_GET32(len, ptr);
    677      1.1  plunky 		break;
    678      1.1  plunky 
    679      1.1  plunky 	default:
    680      1.1  plunky 		return (-1);
    681      1.1  plunky 	}
    682      1.1  plunky 	if (ptr + len > end)
    683      1.1  plunky 		return (-1);
    684      1.1  plunky 
    685      1.1  plunky 	/* Protocol */
    686      1.1  plunky 	SDP_GET8(type, ptr);
    687      1.1  plunky 	switch (type) {
    688      1.1  plunky 	case SDP_DATA_SEQ8:
    689      1.1  plunky 		SDP_GET8(len, ptr);
    690      1.1  plunky 		break;
    691      1.1  plunky 
    692      1.1  plunky 	case SDP_DATA_SEQ16:
    693      1.1  plunky 		SDP_GET16(len, ptr);
    694      1.1  plunky 		break;
    695      1.1  plunky 
    696      1.1  plunky 	case SDP_DATA_SEQ32:
    697      1.1  plunky 		SDP_GET32(len, ptr);
    698      1.1  plunky 		break;
    699      1.1  plunky 
    700      1.1  plunky 	default:
    701      1.1  plunky 		return (-1);
    702      1.1  plunky 	}
    703      1.1  plunky 	if (ptr + len > end)
    704      1.1  plunky 		return (-1);
    705      1.1  plunky 
    706      1.1  plunky 	/* UUID */
    707      1.1  plunky 	if (ptr + 3 > end)
    708      1.1  plunky 		return (-1);
    709      1.1  plunky 	SDP_GET8(type, ptr);
    710      1.1  plunky 	switch (type) {
    711      1.1  plunky 	case SDP_DATA_UUID16:
    712      1.1  plunky 		SDP_GET16(uuid, ptr);
    713      1.1  plunky 		if (uuid != SDP_UUID_PROTOCOL_L2CAP)
    714      1.1  plunky 			return (-1);
    715      1.1  plunky 		break;
    716      1.1  plunky 
    717      1.1  plunky 	case SDP_DATA_UUID32:  /* XXX FIXME can we have 32-bit UUID */
    718      1.1  plunky 	case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
    719      1.1  plunky 	default:
    720      1.1  plunky 		return (-1);
    721      1.1  plunky 	}
    722      1.1  plunky 
    723      1.1  plunky 	/* Protocol */
    724      1.1  plunky 	SDP_GET8(type, ptr);
    725      1.1  plunky 	switch (type) {
    726      1.1  plunky 	case SDP_DATA_SEQ8:
    727      1.1  plunky 		SDP_GET8(len, ptr);
    728      1.1  plunky 		break;
    729      1.1  plunky 
    730      1.1  plunky 	case SDP_DATA_SEQ16:
    731      1.1  plunky 		SDP_GET16(len, ptr);
    732      1.1  plunky 		break;
    733      1.1  plunky 
    734      1.1  plunky 	case SDP_DATA_SEQ32:
    735      1.1  plunky 		SDP_GET32(len, ptr);
    736      1.1  plunky 		break;
    737      1.1  plunky 
    738      1.1  plunky 	default:
    739      1.1  plunky 		return (-1);
    740      1.1  plunky 	}
    741      1.1  plunky 	if (ptr + len > end)
    742      1.1  plunky 		return (-1);
    743      1.1  plunky 
    744      1.1  plunky 	/* UUID */
    745      1.1  plunky 	if (ptr + 3 > end)
    746      1.1  plunky 		return (-1);
    747      1.1  plunky 	SDP_GET8(type, ptr);
    748      1.1  plunky 	switch (type) {
    749      1.1  plunky 	case SDP_DATA_UUID16:
    750      1.1  plunky 		SDP_GET16(uuid, ptr);
    751      1.1  plunky 		if (uuid != SDP_UUID_PROTOCOL_RFCOMM)
    752      1.1  plunky 			return (-1);
    753      1.1  plunky 		break;
    754      1.1  plunky 
    755      1.1  plunky 	case SDP_DATA_UUID32:  /* XXX FIXME can we have 32-bit UUID */
    756      1.1  plunky 	case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
    757      1.1  plunky 	default:
    758      1.1  plunky 		return (-1);
    759      1.1  plunky 	}
    760      1.1  plunky 
    761      1.1  plunky 	/* channel */
    762      1.1  plunky 	if (ptr + 2 > end)
    763      1.1  plunky 		return (-1);
    764      1.1  plunky 
    765      1.1  plunky 	SDP_GET8(type, ptr);
    766      1.1  plunky 	if (type != SDP_DATA_UINT8)
    767      1.1  plunky 		return (-1);
    768      1.1  plunky 
    769      1.1  plunky 	SDP_GET8(channel, ptr);
    770      1.1  plunky 
    771      1.1  plunky 	return (channel);
    772      1.1  plunky }
    773      1.3  plunky 
    774      1.3  plunky /*
    775      1.3  plunky  * return appropriate mode for HID descriptor
    776      1.3  plunky  */
    777      1.3  plunky const char *
    778      1.3  plunky hid_mode(prop_data_t desc)
    779      1.3  plunky {
    780      1.3  plunky 	report_desc_t r;
    781      1.3  plunky 	hid_data_t d;
    782      1.3  plunky 	struct hid_item h;
    783      1.3  plunky 	const char *mode;
    784      1.3  plunky 
    785      1.3  plunky 	hid_init(NULL);
    786      1.3  plunky 
    787      1.3  plunky 	mode = BTDEVauth;	/* default */
    788      1.3  plunky 
    789      1.3  plunky 	r = hid_use_report_desc(prop_data_data_nocopy(desc),
    790      1.3  plunky 				prop_data_size(desc));
    791      1.3  plunky 	if (r == NULL)
    792      1.3  plunky 		err(EXIT_FAILURE, "hid_use_report_desc");
    793      1.3  plunky 
    794      1.3  plunky 	d = hid_start_parse(r, ~0, -1);
    795      1.3  plunky 	while (hid_get_item(d, &h) > 0) {
    796      1.3  plunky 		if (h.kind == hid_collection
    797      1.3  plunky 		    && HID_PAGE(h.usage) == HUP_GENERIC_DESKTOP
    798      1.3  plunky 		    && HID_USAGE(h.usage) == HUG_KEYBOARD)
    799      1.3  plunky 			mode = BTDEVencrypt;
    800      1.3  plunky 	}
    801      1.3  plunky 
    802      1.3  plunky 	hid_end_parse(d);
    803      1.3  plunky 	hid_dispose_report_desc(r);
    804      1.3  plunky 
    805      1.3  plunky 	return mode;
    806      1.3  plunky }
    807