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: ssr.c,v 1.5 2004/01/13 01:54:39 max Exp $
32 #include <sys/queue.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
37 #include <bluetooth.h>
46 * Prepare SDP Service Search Response
50 server_prepare_service_search_response(server_p srv, int32_t fd)
52 uint8_t const *req = srv->req + sizeof(sdp_pdu_t);
53 uint8_t const *req_end = req + ((sdp_pdu_p)(srv->req))->len;
54 uint8_t *rsp = srv->fdidx[fd].rsp;
55 uint8_t const *rsp_end = rsp + NG_L2CAP_MTU_MAXIMUM;
58 provider_t *provider = NULL;
59 int32_t type, ssplen, rsp_limit, rcount, cslen, cs, uuid;
62 * Minimal SDP Service Search Request
65 * uuid16 value16 - 3 bytes ServiceSearchPattern
66 * value16 - 2 bytes MaximumServiceRecordCount
67 * value8 - 1 byte ContinuationState
70 if (req_end - req < 8)
71 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
73 /* Get size of ServiceSearchPattern */
78 SDP_GET8(ssplen, req);
82 SDP_GET16(ssplen, req);
86 SDP_GET32(ssplen, req);
90 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
92 ptr = (uint8_t *) req + ssplen;
94 /* Get MaximumServiceRecordCount */
95 if (ptr + 2 > req_end)
96 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
98 SDP_GET16(rsp_limit, ptr);
100 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
102 /* Get ContinuationState */
103 if (ptr + 1 > req_end)
104 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
106 SDP_GET8(cslen, ptr);
108 if (cslen != 2 || req_end - ptr != 2)
109 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
115 /* Process the request. First, check continuation state */
116 if (srv->fdidx[fd].rsp_cs != cs)
117 return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
118 if (srv->fdidx[fd].rsp_size > 0)
122 * Service Search Response format
124 * value16 - 2 bytes TotalServiceRecordCount (not incl.)
125 * value16 - 2 bytes CurrentServiceRecordCount (not incl.)
126 * value32 - 4 bytes handle
129 * Calculate how many record handles we can fit
130 * in our reply buffer and adjust rlimit.
134 rcount = (rsp_end - ptr) / 4;
135 if (rcount < rsp_limit)
138 /* Look for the record handles */
139 for (rcount = 0; ssplen > 0 && rcount < rsp_limit; ) {
144 case SDP_DATA_UUID16:
146 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
148 SDP_GET16(uuid, req);
152 case SDP_DATA_UUID32: /* XXX FIXME */
153 case SDP_DATA_UUID128: /* XXX FIXME */
155 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
159 for (provider = provider_get_first();
160 provider != NULL && rcount < rsp_limit;
161 provider = provider_get_next(provider)) {
162 if (!provider_match_bdaddr(provider, &srv->req_sa.l2cap_bdaddr))
165 if (provider->profile->uuid == uuid ||
166 SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP == uuid) {
167 SDP_PUT32(provider->handle, ptr);
173 /* Set reply size (not counting PDU header and continuation state) */
174 srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 4;
175 srv->fdidx[fd].rsp_size = ptr - rsp;
176 srv->fdidx[fd].rsp_cs = 0;
182 * Send SDP Service Search Response
186 server_send_service_search_response(server_p srv, int32_t fd)
188 uint8_t *rsp = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_cs;
189 uint8_t *rsp_end = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_size;
197 /* First update continuation state (assume we will send all data) */
198 size = rsp_end - rsp;
199 srv->fdidx[fd].rsp_cs += size;
201 if (size + 1 > srv->fdidx[fd].rsp_limit) {
203 * We need to split out response. Add 3 more bytes for the
204 * continuation state and move rsp_end and rsp_cs backwards.
207 while ((rsp_end - rsp) + 3 > srv->fdidx[fd].rsp_limit) {
209 srv->fdidx[fd].rsp_cs -= 4;
213 cs[1] = srv->fdidx[fd].rsp_cs >> 8;
214 cs[2] = srv->fdidx[fd].rsp_cs & 0xff;
218 assert(rsp_end >= rsp);
220 rcounts[0] = srv->fdidx[fd].rsp_size / 4; /* TotalServiceRecordCount */
221 rcounts[1] = (rsp_end - rsp) / 4; /* CurrentServiceRecordCount */
223 pdu.pid = SDP_PDU_SERVICE_SEARCH_RESPONSE;
224 pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
225 pdu.len = htons(sizeof(rcounts) + rcounts[1] * 4 + 1 + cs[0]);
227 rcounts[0] = htons(rcounts[0]);
228 rcounts[1] = htons(rcounts[1]);
230 iov[0].iov_base = &pdu;
231 iov[0].iov_len = sizeof(pdu);
233 iov[1].iov_base = rcounts;
234 iov[1].iov_len = sizeof(rcounts);
236 iov[2].iov_base = rsp;
237 iov[2].iov_len = rsp_end - rsp;
239 iov[3].iov_base = cs;
240 iov[3].iov_len = 1 + cs[0];
243 size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
244 } while (size < 0 && errno == EINTR);
246 /* Check if we have sent (or failed to sent) last response chunk */
247 if (srv->fdidx[fd].rsp_cs == srv->fdidx[fd].rsp_size) {
248 srv->fdidx[fd].rsp_cs = 0;
249 srv->fdidx[fd].rsp_size = 0;
250 srv->fdidx[fd].rsp_limit = 0;
253 return ((size < 0)? errno : 0);