sdp.c revision 1.3 1 1.3 plunky /* $NetBSD: sdp.c,v 1.3 2007/04/21 06:15:24 plunky 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.3 plunky __RCSID("$NetBSD: sdp.c,v 1.3 2007/04/21 06:15:24 plunky 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 #define NUM(v) (sizeof(v) / sizeof(v[0]))
124 1.1 plunky
125 1.1 plunky static struct {
126 1.1 plunky const char *name;
127 1.1 plunky int (*handler)(prop_dictionary_t);
128 1.1 plunky const char *description;
129 1.1 plunky uint16_t *services;
130 1.1 plunky int nservices;
131 1.1 plunky uint32_t *attrs;
132 1.1 plunky int nattrs;
133 1.1 plunky } cfgtype[] = {
134 1.1 plunky {
135 1.1 plunky "HID", config_hid, "Human Interface Device",
136 1.1 plunky hid_services, NUM(hid_services),
137 1.1 plunky hid_attrs, NUM(hid_attrs),
138 1.1 plunky },
139 1.1 plunky {
140 1.1 plunky "HSET", config_hset, "Headset",
141 1.1 plunky hset_services, NUM(hset_services),
142 1.1 plunky hset_attrs, NUM(hset_attrs),
143 1.1 plunky },
144 1.1 plunky {
145 1.1 plunky "HF", config_hf, "Handsfree",
146 1.1 plunky hf_services, NUM(hf_services),
147 1.1 plunky hf_attrs, NUM(hf_attrs),
148 1.1 plunky },
149 1.1 plunky };
150 1.1 plunky
151 1.1 plunky static sdp_attr_t values[8];
152 1.1 plunky static uint8_t buffer[NUM(values)][512];
153 1.1 plunky
154 1.1 plunky prop_dictionary_t
155 1.1 plunky cfg_query(bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
156 1.1 plunky {
157 1.1 plunky prop_dictionary_t dict;
158 1.1 plunky void *ss;
159 1.1 plunky int rv, i;
160 1.1 plunky
161 1.1 plunky dict = prop_dictionary_create();
162 1.1 plunky if (dict == NULL)
163 1.1 plunky return NULL;
164 1.1 plunky
165 1.1 plunky for (i = 0 ; i < NUM(values) ; i++) {
166 1.1 plunky values[i].flags = SDP_ATTR_INVALID;
167 1.1 plunky values[i].attr = 0;
168 1.1 plunky values[i].vlen = sizeof(buffer[i]);
169 1.1 plunky values[i].value = buffer[i];
170 1.1 plunky }
171 1.1 plunky
172 1.1 plunky for (i = 0 ; i < NUM(cfgtype) ; i++) {
173 1.1 plunky if (strcasecmp(service, cfgtype[i].name) == 0) {
174 1.1 plunky ss = sdp_open(laddr, raddr);
175 1.1 plunky
176 1.1 plunky if (ss == NULL || (errno = sdp_error(ss)) != 0)
177 1.1 plunky return NULL;
178 1.1 plunky
179 1.1 plunky rv = sdp_search(ss,
180 1.1 plunky cfgtype[i].nservices, cfgtype[i].services,
181 1.1 plunky cfgtype[i].nattrs, cfgtype[i].attrs,
182 1.1 plunky NUM(values), values);
183 1.1 plunky
184 1.1 plunky if (rv != 0) {
185 1.1 plunky errno = sdp_error(ss);
186 1.1 plunky return NULL;
187 1.1 plunky }
188 1.1 plunky sdp_close(ss);
189 1.1 plunky
190 1.1 plunky rv = (*cfgtype[i].handler)(dict);
191 1.1 plunky if (rv != 0)
192 1.1 plunky return NULL;
193 1.1 plunky
194 1.1 plunky return dict;
195 1.1 plunky }
196 1.1 plunky }
197 1.1 plunky
198 1.1 plunky printf("Known config types:\n");
199 1.1 plunky for (i = 0 ; i < NUM(cfgtype) ; i++)
200 1.1 plunky printf("\t%s\t%s\n", cfgtype[i].name, cfgtype[i].description);
201 1.1 plunky
202 1.1 plunky exit(EXIT_FAILURE);
203 1.1 plunky }
204 1.1 plunky
205 1.1 plunky /*
206 1.1 plunky * Configure HID results
207 1.1 plunky */
208 1.1 plunky static int
209 1.1 plunky config_hid(prop_dictionary_t dict)
210 1.1 plunky {
211 1.1 plunky prop_object_t obj;
212 1.1 plunky int32_t control_psm, interrupt_psm,
213 1.1 plunky reconnect_initiate, battery_power,
214 1.1 plunky normally_connectable, hid_length;
215 1.1 plunky uint8_t *hid_descriptor;
216 1.3 plunky const char *mode;
217 1.1 plunky int i;
218 1.1 plunky
219 1.1 plunky control_psm = -1;
220 1.1 plunky interrupt_psm = -1;
221 1.1 plunky reconnect_initiate = -1;
222 1.1 plunky normally_connectable = 0;
223 1.1 plunky battery_power = 0;
224 1.1 plunky hid_descriptor = NULL;
225 1.1 plunky hid_length = -1;
226 1.1 plunky
227 1.1 plunky for (i = 0; i < NUM(values) ; i++) {
228 1.1 plunky if (values[i].flags != SDP_ATTR_OK)
229 1.1 plunky continue;
230 1.1 plunky
231 1.1 plunky switch (values[i].attr) {
232 1.1 plunky case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
233 1.1 plunky control_psm = parse_l2cap_psm(&values[i]);
234 1.1 plunky break;
235 1.1 plunky
236 1.1 plunky case SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS:
237 1.1 plunky interrupt_psm = parse_l2cap_psm(&values[i]);
238 1.1 plunky break;
239 1.1 plunky
240 1.1 plunky case 0x0205: /* HIDReconnectInitiate */
241 1.1 plunky reconnect_initiate = parse_boolean(&values[i]);
242 1.1 plunky break;
243 1.1 plunky
244 1.1 plunky case 0x0206: /* HIDDescriptorList */
245 1.1 plunky if (parse_hid_descriptor(&values[i]) == 0) {
246 1.1 plunky hid_descriptor = values[i].value;
247 1.1 plunky hid_length = values[i].vlen;
248 1.1 plunky }
249 1.1 plunky break;
250 1.1 plunky
251 1.1 plunky case 0x0209: /* HIDBatteryPower */
252 1.1 plunky battery_power = parse_boolean(&values[i]);
253 1.1 plunky break;
254 1.1 plunky
255 1.1 plunky case 0x020d: /* HIDNormallyConnectable */
256 1.1 plunky normally_connectable = parse_boolean(&values[i]);
257 1.1 plunky break;
258 1.1 plunky }
259 1.1 plunky }
260 1.1 plunky
261 1.1 plunky if (control_psm == -1
262 1.1 plunky || interrupt_psm == -1
263 1.1 plunky || reconnect_initiate == -1
264 1.1 plunky || hid_descriptor == NULL
265 1.1 plunky || hid_length == -1)
266 1.1 plunky return ENOATTR;
267 1.1 plunky
268 1.1 plunky obj = prop_string_create_cstring_nocopy("bthidev");
269 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
270 1.1 plunky return errno;
271 1.1 plunky
272 1.2 plunky prop_object_release(obj);
273 1.2 plunky
274 1.1 plunky obj = prop_number_create_integer(control_psm);
275 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVcontrolpsm, obj))
276 1.1 plunky return errno;
277 1.1 plunky
278 1.2 plunky prop_object_release(obj);
279 1.2 plunky
280 1.1 plunky obj = prop_number_create_integer(interrupt_psm);
281 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVinterruptpsm, obj))
282 1.1 plunky return errno;
283 1.1 plunky
284 1.2 plunky prop_object_release(obj);
285 1.2 plunky
286 1.1 plunky obj = prop_data_create_data(hid_descriptor, hid_length);
287 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVdescriptor, obj))
288 1.1 plunky return errno;
289 1.1 plunky
290 1.3 plunky mode = hid_mode(obj);
291 1.3 plunky prop_object_release(obj);
292 1.3 plunky
293 1.3 plunky obj = prop_string_create_cstring_nocopy(mode);
294 1.3 plunky if (obj == NULL || !prop_dictionary_set(dict, BTDEVmode, obj))
295 1.3 plunky return errno;
296 1.3 plunky
297 1.2 plunky prop_object_release(obj);
298 1.2 plunky
299 1.1 plunky if (!reconnect_initiate) {
300 1.1 plunky obj = prop_bool_create(TRUE);
301 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVreconnect, obj))
302 1.1 plunky return errno;
303 1.2 plunky
304 1.2 plunky prop_object_release(obj);
305 1.1 plunky }
306 1.1 plunky
307 1.1 plunky return 0;
308 1.1 plunky }
309 1.1 plunky
310 1.1 plunky /*
311 1.1 plunky * Configure HSET results
312 1.1 plunky */
313 1.1 plunky static int
314 1.1 plunky config_hset(prop_dictionary_t dict)
315 1.1 plunky {
316 1.1 plunky prop_object_t obj;
317 1.1 plunky uint32_t channel;
318 1.1 plunky int i;
319 1.1 plunky
320 1.1 plunky channel = -1;
321 1.1 plunky
322 1.1 plunky for (i = 0; i < NUM(values) ; i++) {
323 1.1 plunky if (values[i].flags != SDP_ATTR_OK)
324 1.1 plunky continue;
325 1.1 plunky
326 1.1 plunky switch (values[i].attr) {
327 1.1 plunky case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
328 1.1 plunky channel = parse_rfcomm_channel(&values[i]);
329 1.1 plunky break;
330 1.1 plunky }
331 1.1 plunky }
332 1.1 plunky
333 1.1 plunky if (channel == -1)
334 1.1 plunky return ENOATTR;
335 1.1 plunky
336 1.1 plunky obj = prop_string_create_cstring_nocopy("btsco");
337 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
338 1.1 plunky return errno;
339 1.1 plunky
340 1.2 plunky prop_object_release(obj);
341 1.2 plunky
342 1.1 plunky obj = prop_number_create_integer(channel);
343 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTSCOchannel, obj))
344 1.1 plunky return errno;
345 1.1 plunky
346 1.2 plunky prop_object_release(obj);
347 1.2 plunky
348 1.1 plunky return 0;
349 1.1 plunky }
350 1.1 plunky
351 1.1 plunky /*
352 1.1 plunky * Configure HF results
353 1.1 plunky */
354 1.1 plunky static int
355 1.1 plunky config_hf(prop_dictionary_t dict)
356 1.1 plunky {
357 1.1 plunky prop_object_t obj;
358 1.1 plunky uint32_t channel;
359 1.1 plunky int i;
360 1.1 plunky
361 1.1 plunky channel = -1;
362 1.1 plunky
363 1.1 plunky for (i = 0 ; i < NUM(values) ; i++) {
364 1.1 plunky if (values[i].flags != SDP_ATTR_OK)
365 1.1 plunky continue;
366 1.1 plunky
367 1.1 plunky switch (values[i].attr) {
368 1.1 plunky case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
369 1.1 plunky channel = parse_rfcomm_channel(&values[i]);
370 1.1 plunky break;
371 1.1 plunky }
372 1.1 plunky }
373 1.1 plunky
374 1.1 plunky if (channel == -1)
375 1.1 plunky return ENOATTR;
376 1.1 plunky
377 1.1 plunky obj = prop_string_create_cstring_nocopy("btsco");
378 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
379 1.1 plunky return errno;
380 1.1 plunky
381 1.2 plunky prop_object_release(obj);
382 1.2 plunky
383 1.1 plunky obj = prop_bool_create(TRUE);
384 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTSCOlisten, obj))
385 1.1 plunky return errno;
386 1.1 plunky
387 1.2 plunky prop_object_release(obj);
388 1.2 plunky
389 1.1 plunky obj = prop_number_create_integer(channel);
390 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTSCOchannel, obj))
391 1.1 plunky return errno;
392 1.1 plunky
393 1.2 plunky prop_object_release(obj);
394 1.2 plunky
395 1.1 plunky return 0;
396 1.1 plunky }
397 1.1 plunky
398 1.1 plunky /*
399 1.1 plunky * Parse [additional] protocol descriptor list for L2CAP PSM
400 1.1 plunky *
401 1.1 plunky * seq8 len8 2
402 1.1 plunky * seq8 len8 2
403 1.1 plunky * uuid16 value16 3 L2CAP
404 1.1 plunky * uint16 value16 3 PSM
405 1.1 plunky * seq8 len8 2
406 1.1 plunky * uuid16 value16 3 HID Protocol
407 1.1 plunky * ===
408 1.1 plunky * 15
409 1.1 plunky */
410 1.1 plunky
411 1.1 plunky static int32_t
412 1.1 plunky parse_l2cap_psm(sdp_attr_t *a)
413 1.1 plunky {
414 1.1 plunky uint8_t *ptr = a->value;
415 1.1 plunky uint8_t *end = a->value + a->vlen;
416 1.1 plunky int32_t type, len, uuid, psm;
417 1.1 plunky
418 1.1 plunky if (end - ptr < 15)
419 1.1 plunky return (-1);
420 1.1 plunky
421 1.1 plunky if (a->attr == SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS) {
422 1.1 plunky SDP_GET8(type, ptr);
423 1.1 plunky switch (type) {
424 1.1 plunky case SDP_DATA_SEQ8:
425 1.1 plunky SDP_GET8(len, ptr);
426 1.1 plunky break;
427 1.1 plunky
428 1.1 plunky case SDP_DATA_SEQ16:
429 1.1 plunky SDP_GET16(len, ptr);
430 1.1 plunky break;
431 1.1 plunky
432 1.1 plunky case SDP_DATA_SEQ32:
433 1.1 plunky SDP_GET32(len, ptr);
434 1.1 plunky break;
435 1.1 plunky
436 1.1 plunky default:
437 1.1 plunky return (-1);
438 1.1 plunky }
439 1.1 plunky if (ptr + len > end)
440 1.1 plunky return (-1);
441 1.1 plunky }
442 1.1 plunky
443 1.1 plunky SDP_GET8(type, ptr);
444 1.1 plunky switch (type) {
445 1.1 plunky case SDP_DATA_SEQ8:
446 1.1 plunky SDP_GET8(len, ptr);
447 1.1 plunky break;
448 1.1 plunky
449 1.1 plunky case SDP_DATA_SEQ16:
450 1.1 plunky SDP_GET16(len, ptr);
451 1.1 plunky break;
452 1.1 plunky
453 1.1 plunky case SDP_DATA_SEQ32:
454 1.1 plunky SDP_GET32(len, ptr);
455 1.1 plunky break;
456 1.1 plunky
457 1.1 plunky default:
458 1.1 plunky return (-1);
459 1.1 plunky }
460 1.1 plunky if (ptr + len > end)
461 1.1 plunky return (-1);
462 1.1 plunky
463 1.1 plunky /* Protocol */
464 1.1 plunky SDP_GET8(type, ptr);
465 1.1 plunky switch (type) {
466 1.1 plunky case SDP_DATA_SEQ8:
467 1.1 plunky SDP_GET8(len, ptr);
468 1.1 plunky break;
469 1.1 plunky
470 1.1 plunky case SDP_DATA_SEQ16:
471 1.1 plunky SDP_GET16(len, ptr);
472 1.1 plunky break;
473 1.1 plunky
474 1.1 plunky case SDP_DATA_SEQ32:
475 1.1 plunky SDP_GET32(len, ptr);
476 1.1 plunky break;
477 1.1 plunky
478 1.1 plunky default:
479 1.1 plunky return (-1);
480 1.1 plunky }
481 1.1 plunky if (ptr + len > end)
482 1.1 plunky return (-1);
483 1.1 plunky
484 1.1 plunky /* UUID */
485 1.1 plunky if (ptr + 3 > end)
486 1.1 plunky return (-1);
487 1.1 plunky SDP_GET8(type, ptr);
488 1.1 plunky switch (type) {
489 1.1 plunky case SDP_DATA_UUID16:
490 1.1 plunky SDP_GET16(uuid, ptr);
491 1.1 plunky if (uuid != SDP_UUID_PROTOCOL_L2CAP)
492 1.1 plunky return (-1);
493 1.1 plunky break;
494 1.1 plunky
495 1.1 plunky case SDP_DATA_UUID32: /* XXX FIXME can we have 32-bit UUID */
496 1.1 plunky case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
497 1.1 plunky default:
498 1.1 plunky return (-1);
499 1.1 plunky }
500 1.1 plunky
501 1.1 plunky /* PSM */
502 1.1 plunky if (ptr + 3 > end)
503 1.1 plunky return (-1);
504 1.1 plunky SDP_GET8(type, ptr);
505 1.1 plunky if (type != SDP_DATA_UINT16)
506 1.1 plunky return (-1);
507 1.1 plunky SDP_GET16(psm, ptr);
508 1.1 plunky
509 1.1 plunky return (psm);
510 1.1 plunky }
511 1.1 plunky
512 1.1 plunky /*
513 1.1 plunky * Parse HID descriptor string
514 1.1 plunky *
515 1.1 plunky * seq8 len8 2
516 1.1 plunky * seq8 len8 2
517 1.1 plunky * uint8 value8 2
518 1.1 plunky * str value 3
519 1.1 plunky * ===
520 1.1 plunky * 9
521 1.1 plunky */
522 1.1 plunky
523 1.1 plunky static int32_t
524 1.1 plunky parse_hid_descriptor(sdp_attr_t *a)
525 1.1 plunky {
526 1.1 plunky uint8_t *ptr = a->value;
527 1.1 plunky uint8_t *end = a->value + a->vlen;
528 1.1 plunky int32_t type, len, descriptor_type;
529 1.1 plunky
530 1.1 plunky if (end - ptr < 9)
531 1.1 plunky return (-1);
532 1.1 plunky
533 1.1 plunky SDP_GET8(type, ptr);
534 1.1 plunky switch (type) {
535 1.1 plunky case SDP_DATA_SEQ8:
536 1.1 plunky SDP_GET8(len, ptr);
537 1.1 plunky break;
538 1.1 plunky
539 1.1 plunky case SDP_DATA_SEQ16:
540 1.1 plunky SDP_GET16(len, ptr);
541 1.1 plunky break;
542 1.1 plunky
543 1.1 plunky case SDP_DATA_SEQ32:
544 1.1 plunky SDP_GET32(len, ptr);
545 1.1 plunky break;
546 1.1 plunky
547 1.1 plunky default:
548 1.1 plunky return (-1);
549 1.1 plunky }
550 1.1 plunky if (ptr + len > end)
551 1.1 plunky return (-1);
552 1.1 plunky
553 1.1 plunky while (ptr < end) {
554 1.1 plunky /* Descriptor */
555 1.1 plunky SDP_GET8(type, ptr);
556 1.1 plunky switch (type) {
557 1.1 plunky case SDP_DATA_SEQ8:
558 1.1 plunky if (ptr + 1 > end)
559 1.1 plunky return (-1);
560 1.1 plunky SDP_GET8(len, ptr);
561 1.1 plunky break;
562 1.1 plunky
563 1.1 plunky case SDP_DATA_SEQ16:
564 1.1 plunky if (ptr + 2 > end)
565 1.1 plunky return (-1);
566 1.1 plunky SDP_GET16(len, ptr);
567 1.1 plunky break;
568 1.1 plunky
569 1.1 plunky case SDP_DATA_SEQ32:
570 1.1 plunky if (ptr + 4 > end)
571 1.1 plunky return (-1);
572 1.1 plunky SDP_GET32(len, ptr);
573 1.1 plunky break;
574 1.1 plunky
575 1.1 plunky default:
576 1.1 plunky return (-1);
577 1.1 plunky }
578 1.1 plunky
579 1.1 plunky /* Descripor type */
580 1.1 plunky if (ptr + 1 > end)
581 1.1 plunky return (-1);
582 1.1 plunky SDP_GET8(type, ptr);
583 1.1 plunky if (type != SDP_DATA_UINT8 || ptr + 1 > end)
584 1.1 plunky return (-1);
585 1.1 plunky SDP_GET8(descriptor_type, ptr);
586 1.1 plunky
587 1.1 plunky /* Descriptor value */
588 1.1 plunky if (ptr + 1 > end)
589 1.1 plunky return (-1);
590 1.1 plunky SDP_GET8(type, ptr);
591 1.1 plunky switch (type) {
592 1.1 plunky case SDP_DATA_STR8:
593 1.1 plunky if (ptr + 1 > end)
594 1.1 plunky return (-1);
595 1.1 plunky SDP_GET8(len, ptr);
596 1.1 plunky break;
597 1.1 plunky
598 1.1 plunky case SDP_DATA_STR16:
599 1.1 plunky if (ptr + 2 > end)
600 1.1 plunky return (-1);
601 1.1 plunky SDP_GET16(len, ptr);
602 1.1 plunky break;
603 1.1 plunky
604 1.1 plunky case SDP_DATA_STR32:
605 1.1 plunky if (ptr + 4 > end)
606 1.1 plunky return (-1);
607 1.1 plunky SDP_GET32(len, ptr);
608 1.1 plunky break;
609 1.1 plunky
610 1.1 plunky default:
611 1.1 plunky return (-1);
612 1.1 plunky }
613 1.1 plunky if (ptr + len > end)
614 1.1 plunky return (-1);
615 1.1 plunky
616 1.1 plunky if (descriptor_type == UDESC_REPORT && len > 0) {
617 1.1 plunky a->value = ptr;
618 1.1 plunky a->vlen = len;
619 1.1 plunky
620 1.1 plunky return (0);
621 1.1 plunky }
622 1.1 plunky
623 1.1 plunky ptr += len;
624 1.1 plunky }
625 1.1 plunky
626 1.1 plunky return (-1);
627 1.1 plunky }
628 1.1 plunky
629 1.1 plunky /*
630 1.1 plunky * Parse boolean value
631 1.1 plunky *
632 1.1 plunky * bool8 int8
633 1.1 plunky */
634 1.1 plunky
635 1.1 plunky static int32_t
636 1.1 plunky parse_boolean(sdp_attr_t *a)
637 1.1 plunky {
638 1.1 plunky if (a->vlen != 2 || a->value[0] != SDP_DATA_BOOL)
639 1.1 plunky return (-1);
640 1.1 plunky
641 1.1 plunky return (a->value[1]);
642 1.1 plunky }
643 1.1 plunky
644 1.1 plunky /*
645 1.1 plunky * Parse protocol descriptor list for the RFCOMM channel
646 1.1 plunky *
647 1.1 plunky * seq8 len8 2
648 1.1 plunky * seq8 len8 2
649 1.1 plunky * uuid16 value16 3 L2CAP
650 1.1 plunky * seq8 len8 2
651 1.1 plunky * uuid16 value16 3 RFCOMM
652 1.1 plunky * uint8 value8 2 channel
653 1.1 plunky * ===
654 1.1 plunky * 14
655 1.1 plunky */
656 1.1 plunky
657 1.1 plunky static int32_t
658 1.1 plunky parse_rfcomm_channel(sdp_attr_t *a)
659 1.1 plunky {
660 1.1 plunky uint8_t *ptr = a->value;
661 1.1 plunky uint8_t *end = a->value + a->vlen;
662 1.1 plunky int32_t type, len, uuid, channel;
663 1.1 plunky
664 1.1 plunky if (end - ptr < 14)
665 1.1 plunky return (-1);
666 1.1 plunky
667 1.1 plunky SDP_GET8(type, ptr);
668 1.1 plunky switch (type) {
669 1.1 plunky case SDP_DATA_SEQ8:
670 1.1 plunky SDP_GET8(len, ptr);
671 1.1 plunky break;
672 1.1 plunky
673 1.1 plunky case SDP_DATA_SEQ16:
674 1.1 plunky SDP_GET16(len, ptr);
675 1.1 plunky break;
676 1.1 plunky
677 1.1 plunky case SDP_DATA_SEQ32:
678 1.1 plunky SDP_GET32(len, ptr);
679 1.1 plunky break;
680 1.1 plunky
681 1.1 plunky default:
682 1.1 plunky return (-1);
683 1.1 plunky }
684 1.1 plunky if (ptr + len > end)
685 1.1 plunky return (-1);
686 1.1 plunky
687 1.1 plunky /* Protocol */
688 1.1 plunky SDP_GET8(type, ptr);
689 1.1 plunky switch (type) {
690 1.1 plunky case SDP_DATA_SEQ8:
691 1.1 plunky SDP_GET8(len, ptr);
692 1.1 plunky break;
693 1.1 plunky
694 1.1 plunky case SDP_DATA_SEQ16:
695 1.1 plunky SDP_GET16(len, ptr);
696 1.1 plunky break;
697 1.1 plunky
698 1.1 plunky case SDP_DATA_SEQ32:
699 1.1 plunky SDP_GET32(len, ptr);
700 1.1 plunky break;
701 1.1 plunky
702 1.1 plunky default:
703 1.1 plunky return (-1);
704 1.1 plunky }
705 1.1 plunky if (ptr + len > end)
706 1.1 plunky return (-1);
707 1.1 plunky
708 1.1 plunky /* UUID */
709 1.1 plunky if (ptr + 3 > end)
710 1.1 plunky return (-1);
711 1.1 plunky SDP_GET8(type, ptr);
712 1.1 plunky switch (type) {
713 1.1 plunky case SDP_DATA_UUID16:
714 1.1 plunky SDP_GET16(uuid, ptr);
715 1.1 plunky if (uuid != SDP_UUID_PROTOCOL_L2CAP)
716 1.1 plunky return (-1);
717 1.1 plunky break;
718 1.1 plunky
719 1.1 plunky case SDP_DATA_UUID32: /* XXX FIXME can we have 32-bit UUID */
720 1.1 plunky case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
721 1.1 plunky default:
722 1.1 plunky return (-1);
723 1.1 plunky }
724 1.1 plunky
725 1.1 plunky /* Protocol */
726 1.1 plunky SDP_GET8(type, ptr);
727 1.1 plunky switch (type) {
728 1.1 plunky case SDP_DATA_SEQ8:
729 1.1 plunky SDP_GET8(len, ptr);
730 1.1 plunky break;
731 1.1 plunky
732 1.1 plunky case SDP_DATA_SEQ16:
733 1.1 plunky SDP_GET16(len, ptr);
734 1.1 plunky break;
735 1.1 plunky
736 1.1 plunky case SDP_DATA_SEQ32:
737 1.1 plunky SDP_GET32(len, ptr);
738 1.1 plunky break;
739 1.1 plunky
740 1.1 plunky default:
741 1.1 plunky return (-1);
742 1.1 plunky }
743 1.1 plunky if (ptr + len > end)
744 1.1 plunky return (-1);
745 1.1 plunky
746 1.1 plunky /* UUID */
747 1.1 plunky if (ptr + 3 > end)
748 1.1 plunky return (-1);
749 1.1 plunky SDP_GET8(type, ptr);
750 1.1 plunky switch (type) {
751 1.1 plunky case SDP_DATA_UUID16:
752 1.1 plunky SDP_GET16(uuid, ptr);
753 1.1 plunky if (uuid != SDP_UUID_PROTOCOL_RFCOMM)
754 1.1 plunky return (-1);
755 1.1 plunky break;
756 1.1 plunky
757 1.1 plunky case SDP_DATA_UUID32: /* XXX FIXME can we have 32-bit UUID */
758 1.1 plunky case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
759 1.1 plunky default:
760 1.1 plunky return (-1);
761 1.1 plunky }
762 1.1 plunky
763 1.1 plunky /* channel */
764 1.1 plunky if (ptr + 2 > end)
765 1.1 plunky return (-1);
766 1.1 plunky
767 1.1 plunky SDP_GET8(type, ptr);
768 1.1 plunky if (type != SDP_DATA_UINT8)
769 1.1 plunky return (-1);
770 1.1 plunky
771 1.1 plunky SDP_GET8(channel, ptr);
772 1.1 plunky
773 1.1 plunky return (channel);
774 1.1 plunky }
775 1.3 plunky
776 1.3 plunky /*
777 1.3 plunky * return appropriate mode for HID descriptor
778 1.3 plunky */
779 1.3 plunky const char *
780 1.3 plunky hid_mode(prop_data_t desc)
781 1.3 plunky {
782 1.3 plunky report_desc_t r;
783 1.3 plunky hid_data_t d;
784 1.3 plunky struct hid_item h;
785 1.3 plunky const char *mode;
786 1.3 plunky
787 1.3 plunky hid_init(NULL);
788 1.3 plunky
789 1.3 plunky mode = BTDEVauth; /* default */
790 1.3 plunky
791 1.3 plunky r = hid_use_report_desc(prop_data_data_nocopy(desc),
792 1.3 plunky prop_data_size(desc));
793 1.3 plunky if (r == NULL)
794 1.3 plunky err(EXIT_FAILURE, "hid_use_report_desc");
795 1.3 plunky
796 1.3 plunky d = hid_start_parse(r, ~0, -1);
797 1.3 plunky while (hid_get_item(d, &h) > 0) {
798 1.3 plunky if (h.kind == hid_collection
799 1.3 plunky && HID_PAGE(h.usage) == HUP_GENERIC_DESKTOP
800 1.3 plunky && HID_USAGE(h.usage) == HUG_KEYBOARD)
801 1.3 plunky mode = BTDEVencrypt;
802 1.3 plunky }
803 1.3 plunky
804 1.3 plunky hid_end_parse(d);
805 1.3 plunky hid_dispose_report_desc(r);
806 1.3 plunky
807 1.3 plunky return mode;
808 1.3 plunky }
809