Compat linux/linux32 nice(2) fix. The syscall argument is an increment
[netbsd-mini2440.git] / lib / libsdp / search.c
blob0393ea173dbbca4949ee6bf0987c0f30ad6fd50a
1 /* $NetBSD: search.c,v 1.2 2007/11/16 19:35:08 plunky Exp $ */
3 /*
4 * search.c
6 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
30 * $Id: search.c,v 1.3 2007/12/15 16:03:29 perry Exp $
31 * $FreeBSD: src/lib/libsdp/search.c,v 1.8 2007/11/16 15:13:12 emax Exp $
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: search.c,v 1.2 2007/11/16 19:35:08 plunky Exp $");
37 #include <sys/uio.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <bluetooth.h>
41 #include <errno.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
47 #include <sdp-int.h>
48 #include <sdp.h>
50 int32_t
51 sdp_search(void *xss,
52 uint32_t plen, uint16_t const *pp,
53 uint32_t alen, uint32_t const *ap,
54 uint32_t vlen, sdp_attr_t *vp)
56 struct sdp_xpdu {
57 sdp_pdu_t pdu;
58 uint16_t len;
59 } __packed xpdu;
61 sdp_session_p ss = (sdp_session_p) xss;
62 uint8_t *req = NULL, *rsp = NULL, *rsp_tmp = NULL;
63 int32_t t, len;
64 uint16_t lo, hi;
66 if (ss == NULL)
67 return (-1);
69 if (ss->req == NULL || ss->rsp == NULL ||
70 plen == 0 || pp == NULL || alen == 0 || ap == NULL) {
71 ss->error = EINVAL;
72 return (-1);
75 req = ss->req;
77 /* Calculate ServiceSearchPattern length */
78 plen = plen * (sizeof(pp[0]) + 1);
80 /* Calculate AttributeIDList length */
81 for (len = 0, t = 0; t < alen; t ++) {
82 lo = (uint16_t) (ap[t] >> 16);
83 hi = (uint16_t) (ap[t]);
85 if (lo > hi) {
86 ss->error = EINVAL;
87 return (-1);
90 if (lo != hi)
91 len += (sizeof(ap[t]) + 1);
92 else
93 len += (sizeof(lo) + 1);
95 alen = len;
97 /* Calculate length of the request */
98 len = plen + sizeof(uint8_t) + sizeof(uint16_t) +
99 /* ServiceSearchPattern */
100 sizeof(uint16_t) +
101 /* MaximumAttributeByteCount */
102 alen + sizeof(uint8_t) + sizeof(uint16_t);
103 /* AttributeIDList */
105 if (ss->req_e - req < len) {
106 ss->error = ENOBUFS;
107 return (-1);
110 /* Put ServiceSearchPattern */
111 SDP_PUT8(SDP_DATA_SEQ16, req);
112 SDP_PUT16(plen, req);
113 for (; plen > 0; pp ++, plen -= (sizeof(pp[0]) + 1)) {
114 SDP_PUT8(SDP_DATA_UUID16, req);
115 SDP_PUT16(*pp, req);
118 /* Put MaximumAttributeByteCount */
119 SDP_PUT16(0xffff, req);
121 /* Put AttributeIDList */
122 SDP_PUT8(SDP_DATA_SEQ16, req);
123 SDP_PUT16(alen, req);
124 for (; alen > 0; ap ++) {
125 lo = (uint16_t) (*ap >> 16);
126 hi = (uint16_t) (*ap);
128 if (lo != hi) {
129 /* Put attribute range */
130 SDP_PUT8(SDP_DATA_UINT32, req);
131 SDP_PUT32(*ap, req);
132 alen -= (sizeof(ap[0]) + 1);
133 } else {
134 /* Put attribute */
135 SDP_PUT8(SDP_DATA_UINT16, req);
136 SDP_PUT16(lo, req);
137 alen -= (sizeof(lo) + 1);
141 /* Submit ServiceSearchAttributeRequest and wait for response */
142 ss->cslen = 0;
143 rsp = ss->rsp;
145 do {
146 struct iovec iov[2];
147 uint8_t *req_cs = req;
149 /* Add continuation state (if any) */
150 if (ss->req_e - req_cs < ss->cslen + 1) {
151 ss->error = ENOBUFS;
152 return (-1);
155 SDP_PUT8(ss->cslen, req_cs);
156 if (ss->cslen > 0) {
157 memcpy(req_cs, ss->cs, ss->cslen);
158 req_cs += ss->cslen;
161 /* Prepare SDP PDU header */
162 xpdu.pdu.pid = SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_REQUEST;
163 xpdu.pdu.tid = htons(ss->tid);
164 xpdu.pdu.len = htons(req_cs - ss->req);
166 /* Submit request */
167 iov[0].iov_base = (void *) &xpdu;
168 iov[0].iov_len = sizeof(xpdu.pdu);
169 iov[1].iov_base = (void *) ss->req;
170 iov[1].iov_len = req_cs - ss->req;
172 do {
173 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
174 } while (len < 0 && errno == EINTR);
176 if (len < 0) {
177 ss->error = errno;
178 return (-1);
181 /* Read response */
182 iov[0].iov_base = (void *) &xpdu;
183 iov[0].iov_len = sizeof(xpdu);
184 iov[1].iov_base = (void *) rsp;
185 iov[1].iov_len = ss->imtu;
187 do {
188 len = readv(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
189 } while (len < 0 && errno == EINTR);
191 if (len < 0) {
192 ss->error = errno;
193 return (-1);
195 if (len < sizeof(xpdu)) {
196 ss->error = ENOMSG;
197 return (-1);
200 xpdu.pdu.tid = ntohs(xpdu.pdu.tid);
201 xpdu.pdu.len = ntohs(xpdu.pdu.len);
202 xpdu.len = ntohs(xpdu.len);
204 if (xpdu.pdu.pid == SDP_PDU_ERROR_RESPONSE ||
205 xpdu.pdu.tid != ss->tid ||
206 xpdu.pdu.len > len ||
207 xpdu.len > xpdu.pdu.len) {
208 ss->error = EIO;
209 return (-1);
212 rsp += xpdu.len;
213 ss->tid ++;
215 /* Save continuation state (if any) */
216 ss->cslen = rsp[0];
217 if (ss->cslen > 0) {
218 if (ss->cslen > sizeof(ss->cs)) {
219 ss->error = ENOBUFS;
220 return (-1);
223 memcpy(ss->cs, rsp + 1, ss->cslen);
226 * Ensure that we always have ss->imtu bytes
227 * available in the ss->rsp buffer
230 if (ss->rsp_e - rsp <= ss->imtu) {
231 uint32_t size, offset;
233 size = ss->rsp_e - ss->rsp + ss->imtu;
234 offset = rsp - ss->rsp;
236 rsp_tmp = realloc(ss->rsp, size);
237 if (rsp_tmp == NULL) {
238 ss->error = ENOMEM;
239 return (-1);
242 ss->rsp = rsp_tmp;
243 ss->rsp_e = ss->rsp + size;
244 rsp = ss->rsp + offset;
247 } while (ss->cslen > 0);
250 * If we got here then we have completed SDP transaction and now
251 * we must populate attribute values into vp array. At this point
252 * ss->rsp points to the beginning of the response and rsp points
253 * to the end of the response.
255 * From Bluetooth v1.1 spec page 364
257 * The AttributeLists is a data element sequence where each element
258 * in turn is a data element sequence representing an attribute list.
259 * Each attribute list contains attribute IDs and attribute values
260 * from one service record. The first element in each attribute list
261 * contains the attribute ID of the first attribute to be returned for
262 * that service record. The second element in each attribute list
263 * contains the corresponding attribute value. Successive pairs of
264 * elements in each attribute list contain additional attribute ID
265 * and value pairs. Only attributes that have non-null values within
266 * the service record and whose attribute IDs were specified in the
267 * SDP_ServiceSearchAttributeRequest are contained in the AttributeLists
268 * Neither an attribute ID nor attribute value is placed in
269 * AttributeLists for attributes in the service record that have no
270 * value. Within each attribute list, the attributes are listed in
271 * ascending order of attribute ID value.
274 if (vp == NULL)
275 goto done;
277 rsp_tmp = ss->rsp;
279 /* Skip the first SEQ */
280 SDP_GET8(t, rsp_tmp);
281 switch (t) {
282 case SDP_DATA_SEQ8:
283 SDP_GET8(len, rsp_tmp);
284 break;
286 case SDP_DATA_SEQ16:
287 SDP_GET16(len, rsp_tmp);
288 break;
290 case SDP_DATA_SEQ32:
291 SDP_GET32(len, rsp_tmp);
292 break;
294 default:
295 ss->error = ENOATTR;
296 return (-1);
297 /* NOT REACHED */
300 for (; rsp_tmp < rsp && vlen > 0; ) {
301 /* Get set of attributes for the next record */
302 SDP_GET8(t, rsp_tmp);
303 switch (t) {
304 case SDP_DATA_SEQ8:
305 SDP_GET8(len, rsp_tmp);
306 break;
308 case SDP_DATA_SEQ16:
309 SDP_GET16(len, rsp_tmp);
310 break;
312 case SDP_DATA_SEQ32:
313 SDP_GET32(len, rsp_tmp);
314 break;
316 default:
317 ss->error = ENOATTR;
318 return (-1);
319 /* NOT REACHED */
322 /* Now rsp_tmp points to list of (attr,value) pairs */
323 for (; len > 0 && vlen > 0; vp ++, vlen --) {
324 /* Attribute */
325 SDP_GET8(t, rsp_tmp);
326 if (t != SDP_DATA_UINT16) {
327 ss->error = ENOATTR;
328 return (-1);
330 SDP_GET16(vp->attr, rsp_tmp);
332 /* Attribute value */
333 switch (rsp_tmp[0]) {
334 case SDP_DATA_NIL:
335 alen = 0;
336 break;
338 case SDP_DATA_UINT8:
339 case SDP_DATA_INT8:
340 case SDP_DATA_BOOL:
341 alen = sizeof(uint8_t);
342 break;
344 case SDP_DATA_UINT16:
345 case SDP_DATA_INT16:
346 case SDP_DATA_UUID16:
347 alen = sizeof(uint16_t);
348 break;
350 case SDP_DATA_UINT32:
351 case SDP_DATA_INT32:
352 case SDP_DATA_UUID32:
353 alen = sizeof(uint32_t);
354 break;
356 case SDP_DATA_UINT64:
357 case SDP_DATA_INT64:
358 alen = sizeof(uint64_t);
359 break;
361 case SDP_DATA_UINT128:
362 case SDP_DATA_INT128:
363 case SDP_DATA_UUID128:
364 alen = sizeof(uint128_t);
365 break;
367 case SDP_DATA_STR8:
368 case SDP_DATA_URL8:
369 case SDP_DATA_SEQ8:
370 case SDP_DATA_ALT8:
371 alen = rsp_tmp[1] + sizeof(uint8_t);
372 break;
374 case SDP_DATA_STR16:
375 case SDP_DATA_URL16:
376 case SDP_DATA_SEQ16:
377 case SDP_DATA_ALT16:
378 alen = ((uint16_t)rsp_tmp[1] << 8)
379 | ((uint16_t)rsp_tmp[2]);
380 alen += sizeof(uint16_t);
381 break;
383 case SDP_DATA_STR32:
384 case SDP_DATA_URL32:
385 case SDP_DATA_SEQ32:
386 case SDP_DATA_ALT32:
387 alen = ((uint32_t)rsp_tmp[1] << 24)
388 | ((uint32_t)rsp_tmp[2] << 16)
389 | ((uint32_t)rsp_tmp[3] << 8)
390 | ((uint32_t)rsp_tmp[4]);
391 alen += sizeof(uint32_t);
392 break;
394 default:
395 ss->error = ENOATTR;
396 return (-1);
397 /* NOT REACHED */
400 alen += sizeof(uint8_t);
402 if (vp->value != NULL) {
403 if (alen <= vp->vlen) {
404 vp->flags = SDP_ATTR_OK;
405 vp->vlen = alen;
406 } else
407 vp->flags = SDP_ATTR_TRUNCATED;
409 memcpy(vp->value, rsp_tmp, vp->vlen);
410 } else
411 vp->flags = SDP_ATTR_INVALID;
413 len -= (
414 sizeof(uint8_t) + sizeof(uint16_t) +
415 alen
418 rsp_tmp += alen;
421 done:
422 ss->error = 0;
424 return (0);