4 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $Id: search.c,v 1.2 2003/09/04 22:12:13 max Exp $
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <bluetooth.h>
47 uint32_t plen, uint16_t const *pp,
48 uint32_t alen, uint32_t const *ap,
49 uint32_t vlen, sdp_attr_t *vp)
54 } __attribute__ ((packed)) xpdu;
56 sdp_session_p ss = (sdp_session_p) xss;
57 uint8_t *req = NULL, *rsp = NULL, *rsp_tmp = NULL;
64 if (ss->req == NULL || ss->rsp == NULL ||
65 plen == 0 || pp == NULL || alen == 0 || ap == NULL) {
72 /* Calculate ServiceSearchPattern length */
73 plen = plen * (sizeof(pp[0]) + 1);
75 /* Calculate AttributeIDList length */
76 for (len = 0, t = 0; t < alen; t ++) {
77 lo = (uint16_t) (ap[t] >> 16);
78 hi = (uint16_t) (ap[t]);
86 len += (sizeof(ap[t]) + 1);
88 len += (sizeof(lo) + 1);
92 /* Calculate length of the request */
93 len = plen + sizeof(uint8_t) + sizeof(uint16_t) +
94 /* ServiceSearchPattern */
96 /* MaximumAttributeByteCount */
97 alen + sizeof(uint8_t) + sizeof(uint16_t);
100 if (ss->req_e - req < len) {
105 /* Put ServiceSearchPattern */
106 SDP_PUT8(SDP_DATA_SEQ16, req);
107 SDP_PUT16(plen, req);
108 for (; plen > 0; pp ++, plen -= (sizeof(pp[0]) + 1)) {
109 SDP_PUT8(SDP_DATA_UUID16, req);
113 /* Put MaximumAttributeByteCount */
114 SDP_PUT16(0xffff, req);
116 /* Put AttributeIDList */
117 SDP_PUT8(SDP_DATA_SEQ16, req);
118 SDP_PUT16(alen, req);
119 for (; alen > 0; ap ++) {
120 lo = (uint16_t) (*ap >> 16);
121 hi = (uint16_t) (*ap);
124 /* Put attribute range */
125 SDP_PUT8(SDP_DATA_UINT32, req);
127 alen -= (sizeof(ap[0]) + 1);
130 SDP_PUT8(SDP_DATA_UINT16, req);
132 alen -= (sizeof(lo) + 1);
136 /* Submit ServiceSearchAttributeRequest and wait for response */
142 uint8_t *req_cs = req;
144 /* Add continuation state (if any) */
145 if (ss->req_e - req_cs < ss->cslen + 1) {
150 SDP_PUT8(ss->cslen, req_cs);
152 memcpy(req_cs, ss->cs, ss->cslen);
156 /* Prepare SDP PDU header */
157 xpdu.pdu.pid = SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_REQUEST;
158 xpdu.pdu.tid = htons(ss->tid);
159 xpdu.pdu.len = htons(req_cs - ss->req);
162 iov[0].iov_base = (void *) &xpdu;
163 iov[0].iov_len = sizeof(xpdu.pdu);
164 iov[1].iov_base = (void *) ss->req;
165 iov[1].iov_len = req_cs - ss->req;
168 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
169 } while (len < 0 && errno == EINTR);
177 iov[0].iov_base = (void *) &xpdu;
178 iov[0].iov_len = sizeof(xpdu);
179 iov[1].iov_base = (void *) rsp;
180 iov[1].iov_len = ss->imtu;
183 len = readv(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
184 } while (len < 0 && errno == EINTR);
190 if (len < sizeof(xpdu)) {
195 xpdu.pdu.tid = ntohs(xpdu.pdu.tid);
196 xpdu.pdu.len = ntohs(xpdu.pdu.len);
197 xpdu.len = ntohs(xpdu.len);
199 if (xpdu.pdu.pid == SDP_PDU_ERROR_RESPONSE ||
200 xpdu.pdu.tid != ss->tid ||
201 xpdu.pdu.len > len ||
202 xpdu.len > xpdu.pdu.len) {
210 /* Save continuation state (if any) */
213 if (ss->cslen > sizeof(ss->cs)) {
218 memcpy(ss->cs, rsp + 1, ss->cslen);
221 * Ensure that we always have ss->imtu bytes
222 * available in the ss->rsp buffer
225 if (ss->rsp_e - rsp <= ss->imtu) {
226 uint32_t size, offset;
228 size = ss->rsp_e - ss->rsp + ss->imtu;
229 offset = rsp - ss->rsp;
231 rsp_tmp = realloc(ss->rsp, size);
232 if (rsp_tmp == NULL) {
238 ss->rsp_e = ss->rsp + size;
239 rsp = ss->rsp + offset;
242 } while (ss->cslen > 0);
245 * If we got here then we have completed SDP transaction and now
246 * we must populate attribute values into vp array. At this point
247 * ss->rsp points to the beginning of the response and rsp points
248 * to the end of the response.
250 * From Bluetooth v1.1 spec page 364
252 * The AttributeLists is a data element sequence where each element
253 * in turn is a data element sequence representing an attribute list.
254 * Each attribute list contains attribute IDs and attribute values
255 * from one service record. The first element in each attribute list
256 * contains the attribute ID of the first attribute to be returned for
257 * that service record. The second element in each attribute list
258 * contains the corresponding attribute value. Successive pairs of
259 * elements in each attribute list contain additional attribute ID
260 * and value pairs. Only attributes that have non-null values within
261 * the service record and whose attribute IDs were specified in the
262 * SDP_ServiceSearchAttributeRequest are contained in the AttributeLists
263 * Neither an attribute ID nor attribute value is placed in
264 * AttributeLists for attributes in the service record that have no
265 * value. Within each attribute list, the attributes are listed in
266 * ascending order of attribute ID value.
274 /* Skip the first SEQ */
275 SDP_GET8(t, rsp_tmp);
278 SDP_GET8(len, rsp_tmp);
282 SDP_GET16(len, rsp_tmp);
286 SDP_GET32(len, rsp_tmp);
295 for (; rsp_tmp < rsp && vlen > 0; ) {
296 /* Get set of attributes for the next record */
297 SDP_GET8(t, rsp_tmp);
300 SDP_GET8(len, rsp_tmp);
304 SDP_GET16(len, rsp_tmp);
308 SDP_GET32(len, rsp_tmp);
317 /* Now rsp_tmp points to list of (attr,value) pairs */
318 for (; len > 0 && vlen > 0; vp ++, vlen --) {
320 SDP_GET8(t, rsp_tmp);
321 if (t != SDP_DATA_UINT16) {
325 SDP_GET16(vp->attr, rsp_tmp);
327 /* Attribute value */
328 switch (rsp_tmp[0]) {
336 alen = sizeof(uint8_t);
339 case SDP_DATA_UINT16:
341 case SDP_DATA_UUID16:
342 alen = sizeof(uint16_t);
345 case SDP_DATA_UINT32:
347 case SDP_DATA_UUID32:
348 alen = sizeof(uint32_t);
351 case SDP_DATA_UINT64:
353 alen = sizeof(uint64_t);
356 case SDP_DATA_UINT128:
357 case SDP_DATA_INT128:
358 case SDP_DATA_UUID128:
359 alen = sizeof(uint128_t);
366 alen = rsp_tmp[1] + sizeof(uint8_t);
373 alen = ((uint16_t)rsp_tmp[1] << 8)
374 | ((uint16_t)rsp_tmp[2]);
375 alen += sizeof(uint16_t);
382 alen = ((uint32_t)rsp_tmp[1] << 24)
383 | ((uint32_t)rsp_tmp[2] << 16)
384 | ((uint32_t)rsp_tmp[3] << 8)
385 | ((uint32_t)rsp_tmp[4]);
386 alen += sizeof(uint32_t);
395 alen += sizeof(uint8_t);
397 if (vp->value != NULL) {
398 if (alen <= vp->vlen) {
399 vp->flags = SDP_ATTR_OK;
402 vp->flags = SDP_ATTR_TRUNCATED;
404 memcpy(vp->value, rsp_tmp, vp->vlen);
406 vp->flags = SDP_ATTR_INVALID;
409 sizeof(uint8_t) + sizeof(uint16_t) +