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