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