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: sar.c,v 1.2 2004/01/08 23:46:51 max Exp $
32 #include <sys/queue.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
37 #include <bluetooth.h>
40 #include <stdio.h> /* for NULL */
46 * Prepare SDP attr/value pair. Check if profile implements the attribute
47 * and if so call the attribute value function.
49 * uint16 value16 - 3 bytes (attribute)
50 * value - N bytes (value)
54 server_prepare_attr_value_pair(
55 provider_p const provider, uint16_t attr,
56 uint8_t *buf, uint8_t const * const eob)
58 profile_attr_create_p cf = profile_get_attr(provider->profile, attr);
62 return (0); /* no attribute */
67 SDP_PUT8(SDP_DATA_UINT16, buf);
70 len = cf(buf, eob, (uint8_t const *) provider, sizeof(*provider));
78 * seq16 value16 - 3 bytes
79 * attr value - 3+ bytes
84 server_prepare_attr_list(provider_p const provider,
85 uint8_t const *req, uint8_t const * const req_end,
86 uint8_t *rsp, uint8_t const * const rsp_end)
88 uint8_t *ptr = rsp + 3;
89 int32_t type, hi, lo, len;
94 while (req < req_end) {
99 if (req + 2 > req_end)
106 case SDP_DATA_UINT32:
107 if (req + 4 > req_end)
119 for (; lo <= hi; lo ++) {
120 len = server_prepare_attr_value_pair(provider, lo, ptr, rsp_end);
128 len = ptr - rsp; /* we put this much bytes in rsp */
130 /* Fix SEQ16 header for the rsp */
131 SDP_PUT8(SDP_DATA_SEQ16, rsp);
132 SDP_PUT16(len - 3, rsp);
138 * Prepare SDP Service Attribute Response
142 server_prepare_service_attribute_response(server_p srv, int32_t fd)
144 uint8_t const *req = srv->req + sizeof(sdp_pdu_t);
145 uint8_t const *req_end = req + ((sdp_pdu_p)(srv->req))->len;
146 uint8_t *rsp = srv->fdidx[fd].rsp;
147 uint8_t const *rsp_end = rsp + NG_L2CAP_MTU_MAXIMUM;
150 provider_t *provider = NULL;
152 int32_t type, rsp_limit, aidlen, cslen, cs;
155 * Minimal Service Attribute Request request
157 * value32 - 4 bytes ServiceRecordHandle
158 * value16 - 2 bytes MaximumAttributeByteCount
159 * seq8 len8 - 2 bytes
160 * uint16 value16 - 3 bytes AttributeIDList
161 * value8 - 1 byte ContinuationState
164 if (req_end - req < 12)
165 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
167 /* Get ServiceRecordHandle and MaximumAttributeByteCount */
168 SDP_GET32(handle, req);
169 SDP_GET16(rsp_limit, req);
171 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
173 /* Get size of AttributeIDList */
178 SDP_GET8(aidlen, req);
182 SDP_GET16(aidlen, req);
186 SDP_GET32(aidlen, req);
190 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
192 ptr = (uint8_t *) req + aidlen;
194 /* Get ContinuationState */
195 if (ptr + 1 > req_end)
196 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
198 SDP_GET8(cslen, ptr);
200 if (cslen != 2 || req_end - ptr != 2)
201 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
207 /* Process the request. First, check continuation state */
208 if (srv->fdidx[fd].rsp_cs != cs)
209 return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
210 if (srv->fdidx[fd].rsp_size > 0)
213 /* Lookup record handle */
214 if ((provider = provider_by_handle(handle)) == NULL)
215 return (SDP_ERROR_CODE_INVALID_SERVICE_RECORD_HANDLE);
218 * Service Attribute Response format
220 * value16 - 2 bytes AttributeListByteCount (not incl.)
221 * seq8 len16 - 3 bytes
222 * attr value - 3+ bytes AttributeList
226 cs = server_prepare_attr_list(provider, req, req+aidlen, rsp, rsp_end);
228 return (SDP_ERROR_CODE_INSUFFICIENT_RESOURCES);
230 /* Set reply size (not counting PDU header and continuation state) */
231 srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 2;
232 if (srv->fdidx[fd].rsp_limit > rsp_limit)
233 srv->fdidx[fd].rsp_limit = rsp_limit;
235 srv->fdidx[fd].rsp_size = cs;
236 srv->fdidx[fd].rsp_cs = 0;
242 * Send SDP Service [Search] Attribute Response
246 server_send_service_attribute_response(server_p srv, int32_t fd)
248 uint8_t *rsp = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_cs;
249 uint8_t *rsp_end = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_size;
257 /* First update continuation state (assume we will send all data) */
258 size = rsp_end - rsp;
259 srv->fdidx[fd].rsp_cs += size;
261 if (size + 1 > srv->fdidx[fd].rsp_limit) {
263 * We need to split out response. Add 3 more bytes for the
264 * continuation state and move rsp_end and rsp_cs backwards.
267 while ((rsp_end - rsp) + 3 > srv->fdidx[fd].rsp_limit) {
269 srv->fdidx[fd].rsp_cs --;
273 cs[1] = srv->fdidx[fd].rsp_cs >> 8;
274 cs[2] = srv->fdidx[fd].rsp_cs & 0xff;
278 assert(rsp_end >= rsp);
280 bcount = rsp_end - rsp;
282 if (((sdp_pdu_p)(srv->req))->pid == SDP_PDU_SERVICE_ATTRIBUTE_REQUEST)
283 pdu.pid = SDP_PDU_SERVICE_ATTRIBUTE_RESPONSE;
285 pdu.pid = SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_RESPONSE;
287 pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
288 pdu.len = htons(sizeof(bcount) + bcount + 1 + cs[0]);
290 bcount = htons(bcount);
292 iov[0].iov_base = &pdu;
293 iov[0].iov_len = sizeof(pdu);
295 iov[1].iov_base = &bcount;
296 iov[1].iov_len = sizeof(bcount);
298 iov[2].iov_base = rsp;
299 iov[2].iov_len = rsp_end - rsp;
301 iov[3].iov_base = cs;
302 iov[3].iov_len = 1 + cs[0];
305 size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
306 } while (size < 0 && errno == EINTR);
308 /* Check if we have sent (or failed to sent) last response chunk */
309 if (srv->fdidx[fd].rsp_cs == srv->fdidx[fd].rsp_size) {
310 srv->fdidx[fd].rsp_cs = 0;
311 srv->fdidx[fd].rsp_size = 0;
312 srv->fdidx[fd].rsp_limit = 0;
315 return ((size < 0)? errno : 0);