sdp.c revision 1.2 1 1.2 plunky /* $NetBSD: sdp.c,v 1.2 2007/04/11 20:01:01 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.2 plunky __RCSID("$NetBSD: sdp.c,v 1.2 2007/04/11 20:01:01 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.1 plunky int i;
217 1.1 plunky
218 1.1 plunky control_psm = -1;
219 1.1 plunky interrupt_psm = -1;
220 1.1 plunky reconnect_initiate = -1;
221 1.1 plunky normally_connectable = 0;
222 1.1 plunky battery_power = 0;
223 1.1 plunky hid_descriptor = NULL;
224 1.1 plunky hid_length = -1;
225 1.1 plunky
226 1.1 plunky for (i = 0; i < NUM(values) ; i++) {
227 1.1 plunky if (values[i].flags != SDP_ATTR_OK)
228 1.1 plunky continue;
229 1.1 plunky
230 1.1 plunky switch (values[i].attr) {
231 1.1 plunky case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
232 1.1 plunky control_psm = parse_l2cap_psm(&values[i]);
233 1.1 plunky break;
234 1.1 plunky
235 1.1 plunky case SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS:
236 1.1 plunky interrupt_psm = parse_l2cap_psm(&values[i]);
237 1.1 plunky break;
238 1.1 plunky
239 1.1 plunky case 0x0205: /* HIDReconnectInitiate */
240 1.1 plunky reconnect_initiate = parse_boolean(&values[i]);
241 1.1 plunky break;
242 1.1 plunky
243 1.1 plunky case 0x0206: /* HIDDescriptorList */
244 1.1 plunky if (parse_hid_descriptor(&values[i]) == 0) {
245 1.1 plunky hid_descriptor = values[i].value;
246 1.1 plunky hid_length = values[i].vlen;
247 1.1 plunky }
248 1.1 plunky break;
249 1.1 plunky
250 1.1 plunky case 0x0209: /* HIDBatteryPower */
251 1.1 plunky battery_power = parse_boolean(&values[i]);
252 1.1 plunky break;
253 1.1 plunky
254 1.1 plunky case 0x020d: /* HIDNormallyConnectable */
255 1.1 plunky normally_connectable = parse_boolean(&values[i]);
256 1.1 plunky break;
257 1.1 plunky }
258 1.1 plunky }
259 1.1 plunky
260 1.1 plunky if (control_psm == -1
261 1.1 plunky || interrupt_psm == -1
262 1.1 plunky || reconnect_initiate == -1
263 1.1 plunky || hid_descriptor == NULL
264 1.1 plunky || hid_length == -1)
265 1.1 plunky return ENOATTR;
266 1.1 plunky
267 1.1 plunky obj = prop_string_create_cstring_nocopy("bthidev");
268 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
269 1.1 plunky return errno;
270 1.1 plunky
271 1.2 plunky prop_object_release(obj);
272 1.2 plunky
273 1.1 plunky obj = prop_number_create_integer(control_psm);
274 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVcontrolpsm, obj))
275 1.1 plunky return errno;
276 1.1 plunky
277 1.2 plunky prop_object_release(obj);
278 1.2 plunky
279 1.1 plunky obj = prop_number_create_integer(interrupt_psm);
280 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVinterruptpsm, obj))
281 1.1 plunky return errno;
282 1.1 plunky
283 1.2 plunky prop_object_release(obj);
284 1.2 plunky
285 1.1 plunky obj = prop_data_create_data(hid_descriptor, hid_length);
286 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVdescriptor, obj))
287 1.1 plunky return errno;
288 1.1 plunky
289 1.2 plunky prop_object_release(obj);
290 1.2 plunky
291 1.1 plunky if (!reconnect_initiate) {
292 1.1 plunky obj = prop_bool_create(TRUE);
293 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTHIDEVreconnect, obj))
294 1.1 plunky return errno;
295 1.2 plunky
296 1.2 plunky prop_object_release(obj);
297 1.1 plunky }
298 1.1 plunky
299 1.1 plunky return 0;
300 1.1 plunky }
301 1.1 plunky
302 1.1 plunky /*
303 1.1 plunky * Configure HSET results
304 1.1 plunky */
305 1.1 plunky static int
306 1.1 plunky config_hset(prop_dictionary_t dict)
307 1.1 plunky {
308 1.1 plunky prop_object_t obj;
309 1.1 plunky uint32_t channel;
310 1.1 plunky int i;
311 1.1 plunky
312 1.1 plunky channel = -1;
313 1.1 plunky
314 1.1 plunky for (i = 0; i < NUM(values) ; i++) {
315 1.1 plunky if (values[i].flags != SDP_ATTR_OK)
316 1.1 plunky continue;
317 1.1 plunky
318 1.1 plunky switch (values[i].attr) {
319 1.1 plunky case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
320 1.1 plunky channel = parse_rfcomm_channel(&values[i]);
321 1.1 plunky break;
322 1.1 plunky }
323 1.1 plunky }
324 1.1 plunky
325 1.1 plunky if (channel == -1)
326 1.1 plunky return ENOATTR;
327 1.1 plunky
328 1.1 plunky obj = prop_string_create_cstring_nocopy("btsco");
329 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
330 1.1 plunky return errno;
331 1.1 plunky
332 1.2 plunky prop_object_release(obj);
333 1.2 plunky
334 1.1 plunky obj = prop_number_create_integer(channel);
335 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTSCOchannel, 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 return 0;
341 1.1 plunky }
342 1.1 plunky
343 1.1 plunky /*
344 1.1 plunky * Configure HF results
345 1.1 plunky */
346 1.1 plunky static int
347 1.1 plunky config_hf(prop_dictionary_t dict)
348 1.1 plunky {
349 1.1 plunky prop_object_t obj;
350 1.1 plunky uint32_t channel;
351 1.1 plunky int i;
352 1.1 plunky
353 1.1 plunky channel = -1;
354 1.1 plunky
355 1.1 plunky for (i = 0 ; i < NUM(values) ; i++) {
356 1.1 plunky if (values[i].flags != SDP_ATTR_OK)
357 1.1 plunky continue;
358 1.1 plunky
359 1.1 plunky switch (values[i].attr) {
360 1.1 plunky case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
361 1.1 plunky channel = parse_rfcomm_channel(&values[i]);
362 1.1 plunky break;
363 1.1 plunky }
364 1.1 plunky }
365 1.1 plunky
366 1.1 plunky if (channel == -1)
367 1.1 plunky return ENOATTR;
368 1.1 plunky
369 1.1 plunky obj = prop_string_create_cstring_nocopy("btsco");
370 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTDEVtype, obj))
371 1.1 plunky return errno;
372 1.1 plunky
373 1.2 plunky prop_object_release(obj);
374 1.2 plunky
375 1.1 plunky obj = prop_bool_create(TRUE);
376 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTSCOlisten, 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.1 plunky obj = prop_number_create_integer(channel);
382 1.1 plunky if (obj == NULL || !prop_dictionary_set(dict, BTSCOchannel, 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 return 0;
388 1.1 plunky }
389 1.1 plunky
390 1.1 plunky /*
391 1.1 plunky * Parse [additional] protocol descriptor list for L2CAP PSM
392 1.1 plunky *
393 1.1 plunky * seq8 len8 2
394 1.1 plunky * seq8 len8 2
395 1.1 plunky * uuid16 value16 3 L2CAP
396 1.1 plunky * uint16 value16 3 PSM
397 1.1 plunky * seq8 len8 2
398 1.1 plunky * uuid16 value16 3 HID Protocol
399 1.1 plunky * ===
400 1.1 plunky * 15
401 1.1 plunky */
402 1.1 plunky
403 1.1 plunky static int32_t
404 1.1 plunky parse_l2cap_psm(sdp_attr_t *a)
405 1.1 plunky {
406 1.1 plunky uint8_t *ptr = a->value;
407 1.1 plunky uint8_t *end = a->value + a->vlen;
408 1.1 plunky int32_t type, len, uuid, psm;
409 1.1 plunky
410 1.1 plunky if (end - ptr < 15)
411 1.1 plunky return (-1);
412 1.1 plunky
413 1.1 plunky if (a->attr == SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS) {
414 1.1 plunky SDP_GET8(type, ptr);
415 1.1 plunky switch (type) {
416 1.1 plunky case SDP_DATA_SEQ8:
417 1.1 plunky SDP_GET8(len, ptr);
418 1.1 plunky break;
419 1.1 plunky
420 1.1 plunky case SDP_DATA_SEQ16:
421 1.1 plunky SDP_GET16(len, ptr);
422 1.1 plunky break;
423 1.1 plunky
424 1.1 plunky case SDP_DATA_SEQ32:
425 1.1 plunky SDP_GET32(len, ptr);
426 1.1 plunky break;
427 1.1 plunky
428 1.1 plunky default:
429 1.1 plunky return (-1);
430 1.1 plunky }
431 1.1 plunky if (ptr + len > end)
432 1.1 plunky return (-1);
433 1.1 plunky }
434 1.1 plunky
435 1.1 plunky SDP_GET8(type, ptr);
436 1.1 plunky switch (type) {
437 1.1 plunky case SDP_DATA_SEQ8:
438 1.1 plunky SDP_GET8(len, ptr);
439 1.1 plunky break;
440 1.1 plunky
441 1.1 plunky case SDP_DATA_SEQ16:
442 1.1 plunky SDP_GET16(len, ptr);
443 1.1 plunky break;
444 1.1 plunky
445 1.1 plunky case SDP_DATA_SEQ32:
446 1.1 plunky SDP_GET32(len, ptr);
447 1.1 plunky break;
448 1.1 plunky
449 1.1 plunky default:
450 1.1 plunky return (-1);
451 1.1 plunky }
452 1.1 plunky if (ptr + len > end)
453 1.1 plunky return (-1);
454 1.1 plunky
455 1.1 plunky /* Protocol */
456 1.1 plunky SDP_GET8(type, ptr);
457 1.1 plunky switch (type) {
458 1.1 plunky case SDP_DATA_SEQ8:
459 1.1 plunky SDP_GET8(len, ptr);
460 1.1 plunky break;
461 1.1 plunky
462 1.1 plunky case SDP_DATA_SEQ16:
463 1.1 plunky SDP_GET16(len, ptr);
464 1.1 plunky break;
465 1.1 plunky
466 1.1 plunky case SDP_DATA_SEQ32:
467 1.1 plunky SDP_GET32(len, ptr);
468 1.1 plunky break;
469 1.1 plunky
470 1.1 plunky default:
471 1.1 plunky return (-1);
472 1.1 plunky }
473 1.1 plunky if (ptr + len > end)
474 1.1 plunky return (-1);
475 1.1 plunky
476 1.1 plunky /* UUID */
477 1.1 plunky if (ptr + 3 > end)
478 1.1 plunky return (-1);
479 1.1 plunky SDP_GET8(type, ptr);
480 1.1 plunky switch (type) {
481 1.1 plunky case SDP_DATA_UUID16:
482 1.1 plunky SDP_GET16(uuid, ptr);
483 1.1 plunky if (uuid != SDP_UUID_PROTOCOL_L2CAP)
484 1.1 plunky return (-1);
485 1.1 plunky break;
486 1.1 plunky
487 1.1 plunky case SDP_DATA_UUID32: /* XXX FIXME can we have 32-bit UUID */
488 1.1 plunky case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
489 1.1 plunky default:
490 1.1 plunky return (-1);
491 1.1 plunky }
492 1.1 plunky
493 1.1 plunky /* PSM */
494 1.1 plunky if (ptr + 3 > end)
495 1.1 plunky return (-1);
496 1.1 plunky SDP_GET8(type, ptr);
497 1.1 plunky if (type != SDP_DATA_UINT16)
498 1.1 plunky return (-1);
499 1.1 plunky SDP_GET16(psm, ptr);
500 1.1 plunky
501 1.1 plunky return (psm);
502 1.1 plunky }
503 1.1 plunky
504 1.1 plunky /*
505 1.1 plunky * Parse HID descriptor string
506 1.1 plunky *
507 1.1 plunky * seq8 len8 2
508 1.1 plunky * seq8 len8 2
509 1.1 plunky * uint8 value8 2
510 1.1 plunky * str value 3
511 1.1 plunky * ===
512 1.1 plunky * 9
513 1.1 plunky */
514 1.1 plunky
515 1.1 plunky static int32_t
516 1.1 plunky parse_hid_descriptor(sdp_attr_t *a)
517 1.1 plunky {
518 1.1 plunky uint8_t *ptr = a->value;
519 1.1 plunky uint8_t *end = a->value + a->vlen;
520 1.1 plunky int32_t type, len, descriptor_type;
521 1.1 plunky
522 1.1 plunky if (end - ptr < 9)
523 1.1 plunky return (-1);
524 1.1 plunky
525 1.1 plunky SDP_GET8(type, ptr);
526 1.1 plunky switch (type) {
527 1.1 plunky case SDP_DATA_SEQ8:
528 1.1 plunky SDP_GET8(len, ptr);
529 1.1 plunky break;
530 1.1 plunky
531 1.1 plunky case SDP_DATA_SEQ16:
532 1.1 plunky SDP_GET16(len, ptr);
533 1.1 plunky break;
534 1.1 plunky
535 1.1 plunky case SDP_DATA_SEQ32:
536 1.1 plunky SDP_GET32(len, ptr);
537 1.1 plunky break;
538 1.1 plunky
539 1.1 plunky default:
540 1.1 plunky return (-1);
541 1.1 plunky }
542 1.1 plunky if (ptr + len > end)
543 1.1 plunky return (-1);
544 1.1 plunky
545 1.1 plunky while (ptr < end) {
546 1.1 plunky /* Descriptor */
547 1.1 plunky SDP_GET8(type, ptr);
548 1.1 plunky switch (type) {
549 1.1 plunky case SDP_DATA_SEQ8:
550 1.1 plunky if (ptr + 1 > end)
551 1.1 plunky return (-1);
552 1.1 plunky SDP_GET8(len, ptr);
553 1.1 plunky break;
554 1.1 plunky
555 1.1 plunky case SDP_DATA_SEQ16:
556 1.1 plunky if (ptr + 2 > end)
557 1.1 plunky return (-1);
558 1.1 plunky SDP_GET16(len, ptr);
559 1.1 plunky break;
560 1.1 plunky
561 1.1 plunky case SDP_DATA_SEQ32:
562 1.1 plunky if (ptr + 4 > end)
563 1.1 plunky return (-1);
564 1.1 plunky SDP_GET32(len, ptr);
565 1.1 plunky break;
566 1.1 plunky
567 1.1 plunky default:
568 1.1 plunky return (-1);
569 1.1 plunky }
570 1.1 plunky
571 1.1 plunky /* Descripor type */
572 1.1 plunky if (ptr + 1 > end)
573 1.1 plunky return (-1);
574 1.1 plunky SDP_GET8(type, ptr);
575 1.1 plunky if (type != SDP_DATA_UINT8 || ptr + 1 > end)
576 1.1 plunky return (-1);
577 1.1 plunky SDP_GET8(descriptor_type, ptr);
578 1.1 plunky
579 1.1 plunky /* Descriptor value */
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 switch (type) {
584 1.1 plunky case SDP_DATA_STR8:
585 1.1 plunky if (ptr + 1 > end)
586 1.1 plunky return (-1);
587 1.1 plunky SDP_GET8(len, ptr);
588 1.1 plunky break;
589 1.1 plunky
590 1.1 plunky case SDP_DATA_STR16:
591 1.1 plunky if (ptr + 2 > end)
592 1.1 plunky return (-1);
593 1.1 plunky SDP_GET16(len, ptr);
594 1.1 plunky break;
595 1.1 plunky
596 1.1 plunky case SDP_DATA_STR32:
597 1.1 plunky if (ptr + 4 > end)
598 1.1 plunky return (-1);
599 1.1 plunky SDP_GET32(len, ptr);
600 1.1 plunky break;
601 1.1 plunky
602 1.1 plunky default:
603 1.1 plunky return (-1);
604 1.1 plunky }
605 1.1 plunky if (ptr + len > end)
606 1.1 plunky return (-1);
607 1.1 plunky
608 1.1 plunky if (descriptor_type == UDESC_REPORT && len > 0) {
609 1.1 plunky a->value = ptr;
610 1.1 plunky a->vlen = len;
611 1.1 plunky
612 1.1 plunky return (0);
613 1.1 plunky }
614 1.1 plunky
615 1.1 plunky ptr += len;
616 1.1 plunky }
617 1.1 plunky
618 1.1 plunky return (-1);
619 1.1 plunky }
620 1.1 plunky
621 1.1 plunky /*
622 1.1 plunky * Parse boolean value
623 1.1 plunky *
624 1.1 plunky * bool8 int8
625 1.1 plunky */
626 1.1 plunky
627 1.1 plunky static int32_t
628 1.1 plunky parse_boolean(sdp_attr_t *a)
629 1.1 plunky {
630 1.1 plunky if (a->vlen != 2 || a->value[0] != SDP_DATA_BOOL)
631 1.1 plunky return (-1);
632 1.1 plunky
633 1.1 plunky return (a->value[1]);
634 1.1 plunky }
635 1.1 plunky
636 1.1 plunky /*
637 1.1 plunky * Parse protocol descriptor list for the RFCOMM channel
638 1.1 plunky *
639 1.1 plunky * seq8 len8 2
640 1.1 plunky * seq8 len8 2
641 1.1 plunky * uuid16 value16 3 L2CAP
642 1.1 plunky * seq8 len8 2
643 1.1 plunky * uuid16 value16 3 RFCOMM
644 1.1 plunky * uint8 value8 2 channel
645 1.1 plunky * ===
646 1.1 plunky * 14
647 1.1 plunky */
648 1.1 plunky
649 1.1 plunky static int32_t
650 1.1 plunky parse_rfcomm_channel(sdp_attr_t *a)
651 1.1 plunky {
652 1.1 plunky uint8_t *ptr = a->value;
653 1.1 plunky uint8_t *end = a->value + a->vlen;
654 1.1 plunky int32_t type, len, uuid, channel;
655 1.1 plunky
656 1.1 plunky if (end - ptr < 14)
657 1.1 plunky return (-1);
658 1.1 plunky
659 1.1 plunky SDP_GET8(type, ptr);
660 1.1 plunky switch (type) {
661 1.1 plunky case SDP_DATA_SEQ8:
662 1.1 plunky SDP_GET8(len, ptr);
663 1.1 plunky break;
664 1.1 plunky
665 1.1 plunky case SDP_DATA_SEQ16:
666 1.1 plunky SDP_GET16(len, ptr);
667 1.1 plunky break;
668 1.1 plunky
669 1.1 plunky case SDP_DATA_SEQ32:
670 1.1 plunky SDP_GET32(len, ptr);
671 1.1 plunky break;
672 1.1 plunky
673 1.1 plunky default:
674 1.1 plunky return (-1);
675 1.1 plunky }
676 1.1 plunky if (ptr + len > end)
677 1.1 plunky return (-1);
678 1.1 plunky
679 1.1 plunky /* Protocol */
680 1.1 plunky SDP_GET8(type, ptr);
681 1.1 plunky switch (type) {
682 1.1 plunky case SDP_DATA_SEQ8:
683 1.1 plunky SDP_GET8(len, ptr);
684 1.1 plunky break;
685 1.1 plunky
686 1.1 plunky case SDP_DATA_SEQ16:
687 1.1 plunky SDP_GET16(len, ptr);
688 1.1 plunky break;
689 1.1 plunky
690 1.1 plunky case SDP_DATA_SEQ32:
691 1.1 plunky SDP_GET32(len, ptr);
692 1.1 plunky break;
693 1.1 plunky
694 1.1 plunky default:
695 1.1 plunky return (-1);
696 1.1 plunky }
697 1.1 plunky if (ptr + len > end)
698 1.1 plunky return (-1);
699 1.1 plunky
700 1.1 plunky /* UUID */
701 1.1 plunky if (ptr + 3 > end)
702 1.1 plunky return (-1);
703 1.1 plunky SDP_GET8(type, ptr);
704 1.1 plunky switch (type) {
705 1.1 plunky case SDP_DATA_UUID16:
706 1.1 plunky SDP_GET16(uuid, ptr);
707 1.1 plunky if (uuid != SDP_UUID_PROTOCOL_L2CAP)
708 1.1 plunky return (-1);
709 1.1 plunky break;
710 1.1 plunky
711 1.1 plunky case SDP_DATA_UUID32: /* XXX FIXME can we have 32-bit UUID */
712 1.1 plunky case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
713 1.1 plunky default:
714 1.1 plunky return (-1);
715 1.1 plunky }
716 1.1 plunky
717 1.1 plunky /* Protocol */
718 1.1 plunky SDP_GET8(type, ptr);
719 1.1 plunky switch (type) {
720 1.1 plunky case SDP_DATA_SEQ8:
721 1.1 plunky SDP_GET8(len, ptr);
722 1.1 plunky break;
723 1.1 plunky
724 1.1 plunky case SDP_DATA_SEQ16:
725 1.1 plunky SDP_GET16(len, ptr);
726 1.1 plunky break;
727 1.1 plunky
728 1.1 plunky case SDP_DATA_SEQ32:
729 1.1 plunky SDP_GET32(len, ptr);
730 1.1 plunky break;
731 1.1 plunky
732 1.1 plunky default:
733 1.1 plunky return (-1);
734 1.1 plunky }
735 1.1 plunky if (ptr + len > end)
736 1.1 plunky return (-1);
737 1.1 plunky
738 1.1 plunky /* UUID */
739 1.1 plunky if (ptr + 3 > end)
740 1.1 plunky return (-1);
741 1.1 plunky SDP_GET8(type, ptr);
742 1.1 plunky switch (type) {
743 1.1 plunky case SDP_DATA_UUID16:
744 1.1 plunky SDP_GET16(uuid, ptr);
745 1.1 plunky if (uuid != SDP_UUID_PROTOCOL_RFCOMM)
746 1.1 plunky return (-1);
747 1.1 plunky break;
748 1.1 plunky
749 1.1 plunky case SDP_DATA_UUID32: /* XXX FIXME can we have 32-bit UUID */
750 1.1 plunky case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
751 1.1 plunky default:
752 1.1 plunky return (-1);
753 1.1 plunky }
754 1.1 plunky
755 1.1 plunky /* channel */
756 1.1 plunky if (ptr + 2 > end)
757 1.1 plunky return (-1);
758 1.1 plunky
759 1.1 plunky SDP_GET8(type, ptr);
760 1.1 plunky if (type != SDP_DATA_UINT8)
761 1.1 plunky return (-1);
762 1.1 plunky
763 1.1 plunky SDP_GET8(channel, ptr);
764 1.1 plunky
765 1.1 plunky return (channel);
766 1.1 plunky }
767