4 * Copyright (c) 2004 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: ssar.c,v 1.4 2004/01/12 22:54:31 max Exp $
32 #include <sys/queue.h>
33 #include <bluetooth.h>
39 #include "uuid-private.h"
42 int32_t server_prepare_attr_list(provider_p const provider,
43 uint8_t const *req, uint8_t const * const req_end,
44 uint8_t *rsp, uint8_t const * const rsp_end);
47 * Scan an attribute for matching UUID.
50 server_search_uuid_sub(uint8_t *buf, uint8_t const * const eob, const uint128_t *uuid)
64 SDP_GET16(value, buf);
66 memcpy(&duuid, &uuid_base, sizeof(duuid));
67 duuid.b[2] = value >> 8 & 0xff;
68 duuid.b[3] = value & 0xff;
70 if (memcmp(&duuid, uuid, sizeof(duuid)) == 0)
76 SDP_GET32(value, buf);
77 memcpy(&duuid, &uuid_base, sizeof(duuid));
78 duuid.b[0] = value >> 24 & 0xff;
79 duuid.b[1] = value >> 16 & 0xff;
80 duuid.b[2] = value >> 8 & 0xff;
81 duuid.b[3] = value & 0xff;
83 if (memcmp(&duuid, uuid, sizeof(duuid)) == 0)
86 case SDP_DATA_UUID128:
89 SDP_GET_UUID128(&duuid, buf);
91 if (memcmp(&duuid, uuid, sizeof(duuid)) == 0)
104 case SDP_DATA_UINT32:
109 case SDP_DATA_UINT64:
113 case SDP_DATA_UINT128:
114 case SDP_DATA_INT128:
120 SDP_GET8(value, buf);
126 SDP_GET16(value, buf);
127 if (value > (eob - buf))
134 SDP_GET32(value, buf);
135 if (value > (eob - buf))
150 * Search a provider for matching UUID in its attributes.
153 server_search_uuid(provider_p const provider, const uint128_t *uuid)
159 for (attr = provider->profile->attrs; attr->create != NULL; attr++) {
161 len = attr->create(buffer, buffer + sizeof(buffer),
162 (const uint8_t *)provider->profile, sizeof(*provider->profile));
165 if (server_search_uuid_sub(buffer, buffer + len, uuid) == 0)
172 * Prepare SDP Service Search Attribute Response
176 server_prepare_service_search_attribute_response(server_p srv, int32_t fd)
178 uint8_t const *req = srv->req + sizeof(sdp_pdu_t);
179 uint8_t const *req_end = req + ((sdp_pdu_p)(srv->req))->len;
180 uint8_t *rsp = srv->fdidx[fd].rsp;
181 uint8_t const *rsp_end = rsp + NG_L2CAP_MTU_MAXIMUM;
183 uint8_t const *sspptr = NULL, *aidptr = NULL;
186 provider_t *provider = NULL;
187 int32_t type, rsp_limit, ssplen, aidlen, cslen, cs;
188 uint128_t uuid, puuid;
191 * Minimal Service Search Attribute Request request
193 * seq8 len8 - 2 bytes
194 * uuid16 value16 - 3 bytes ServiceSearchPattern
195 * value16 - 2 bytes MaximumAttributeByteCount
196 * seq8 len8 - 2 bytes
197 * uint16 value16 - 3 bytes AttributeIDList
198 * value8 - 1 byte ContinuationState
201 if (req_end - req < 13)
202 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
204 /* Get size of ServiceSearchPattern */
209 SDP_GET8(ssplen, req);
213 SDP_GET16(ssplen, req);
217 SDP_GET32(ssplen, req);
221 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
226 /* Get MaximumAttributeByteCount */
227 if (req + 2 > req_end)
228 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
230 SDP_GET16(rsp_limit, req);
232 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
234 /* Get size of AttributeIDList */
235 if (req + 1 > req_end)
236 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
242 if (req + 1 > req_end)
243 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
245 SDP_GET8(aidlen, req);
249 if (req + 2 > req_end)
250 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
252 SDP_GET16(aidlen, req);
256 if (req + 4 > req_end)
257 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
259 SDP_GET32(aidlen, req);
263 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
268 /* Get ContinuationState */
269 if (req + 1 > req_end)
270 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
272 SDP_GET8(cslen, req);
274 if (cslen != 2 || req_end - req != 2)
275 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
281 /* Process the request. First, check continuation state */
282 if (srv->fdidx[fd].rsp_cs != cs)
283 return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
284 if (srv->fdidx[fd].rsp_size > 0)
288 * Service Search Attribute Response format
290 * value16 - 2 bytes AttributeListByteCount (not incl.)
291 * seq8 len16 - 3 bytes
292 * attr list - 3+ bytes AttributeLists
299 SDP_GET8(type, sspptr);
303 case SDP_DATA_UUID16:
305 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
307 memcpy(&uuid, &uuid_base, sizeof(uuid));
308 uuid.b[2] = *sspptr ++;
309 uuid.b[3] = *sspptr ++;
313 case SDP_DATA_UUID32:
315 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
317 memcpy(&uuid, &uuid_base, sizeof(uuid));
318 uuid.b[0] = *sspptr ++;
319 uuid.b[1] = *sspptr ++;
320 uuid.b[2] = *sspptr ++;
321 uuid.b[3] = *sspptr ++;
325 case SDP_DATA_UUID128:
327 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
329 memcpy(uuid.b, sspptr, 16);
335 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
339 for (provider = provider_get_first();
341 provider = provider_get_next(provider)) {
342 if (!provider_match_bdaddr(provider, &srv->req_sa.l2cap_bdaddr))
345 memcpy(&puuid, &uuid_base, sizeof(puuid));
346 puuid.b[2] = provider->profile->uuid >> 8;
347 puuid.b[3] = provider->profile->uuid;
349 if (memcmp(&uuid, &puuid, sizeof(uuid)) != 0 &&
350 memcmp(&uuid, &uuid_public_browse_group, sizeof(uuid)) != 0 &&
351 server_search_uuid(provider, &uuid) != 0)
354 cs = server_prepare_attr_list(provider,
355 aidptr, aidptr + aidlen, ptr, rsp_end);
357 return (SDP_ERROR_CODE_INSUFFICIENT_RESOURCES);
363 /* Set reply size (not counting PDU header and continuation state) */
364 srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 2;
365 if (srv->fdidx[fd].rsp_limit > rsp_limit)
366 srv->fdidx[fd].rsp_limit = rsp_limit;
368 srv->fdidx[fd].rsp_size = ptr - rsp;
369 srv->fdidx[fd].rsp_cs = 0;
371 /* Fix AttributeLists sequence header */
373 SDP_PUT8(SDP_DATA_SEQ16, ptr);
374 SDP_PUT16(srv->fdidx[fd].rsp_size - 3, ptr);