1124758Semax/*
2124758Semax * scr.c
3124758Semax *
4124758Semax * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5124758Semax * All rights reserved.
6124758Semax *
7124758Semax * Redistribution and use in source and binary forms, with or without
8124758Semax * modification, are permitted provided that the following conditions
9124758Semax * are met:
10124758Semax * 1. Redistributions of source code must retain the above copyright
11124758Semax *    notice, this list of conditions and the following disclaimer.
12124758Semax * 2. Redistributions in binary form must reproduce the above copyright
13124758Semax *    notice, this list of conditions and the following disclaimer in the
14124758Semax *    documentation and/or other materials provided with the distribution.
15124758Semax *
16124758Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17124758Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18124758Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19124758Semax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20124758Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21124758Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22124758Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23124758Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24124758Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25124758Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26124758Semax * SUCH DAMAGE.
27124758Semax *
28124758Semax * $Id: scr.c,v 1.1 2004/01/13 01:54:39 max Exp $
29124758Semax * $FreeBSD$
30124758Semax */
31124758Semax
32124758Semax#include <sys/queue.h>
33124758Semax#include <bluetooth.h>
34124758Semax#include <errno.h>
35124758Semax#include <sdp.h>
36124758Semax#include <string.h>
37124758Semax#include "profile.h"
38124758Semax#include "provider.h"
39124758Semax#include "server.h"
40124758Semax
41124758Semax/*
42124758Semax * Prepare Service Change response
43124758Semax */
44124758Semax
45124758Semaxint32_t
46124758Semaxserver_prepare_service_change_response(server_p srv, int32_t fd)
47124758Semax{
48124758Semax	uint8_t const	*req = srv->req + sizeof(sdp_pdu_t);
49124758Semax	uint8_t const	*req_end = req + ((sdp_pdu_p)(srv->req))->len;
50124758Semax	uint8_t		*rsp = srv->fdidx[fd].rsp;
51124758Semax
52124758Semax	provider_t	*provider = NULL;
53124758Semax	uint32_t	 handle;
54124758Semax
55124758Semax	/*
56124758Semax	 * Minimal Service Change Request
57124758Semax	 *
58124758Semax	 * value32	- handle 4 bytes
59124758Semax	 */
60124758Semax
61153176Semax	if (!srv->fdidx[fd].control ||
62153176Semax	    !srv->fdidx[fd].priv || req_end - req < 4)
63124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
64124758Semax
65124758Semax	/* Get handle */
66124758Semax	SDP_GET32(handle, req);
67124758Semax
68124758Semax	/* Lookup provider */
69124758Semax	provider = provider_by_handle(handle);
70124758Semax	if (provider == NULL || provider->fd != fd)
71124758Semax		return (SDP_ERROR_CODE_INVALID_SERVICE_RECORD_HANDLE);
72124758Semax
73124758Semax	/* Validate user data */
74124758Semax	if (req_end - req < provider->profile->dsize ||
75124758Semax	    provider->profile->valid == NULL ||
76124758Semax	    (provider->profile->valid)(req, req_end - req) == 0)
77124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
78124758Semax
79124758Semax	/* Update provider */
80124758Semax	if (provider_update(provider, req, req_end - req) < 0)
81124758Semax		return (SDP_ERROR_CODE_INSUFFICIENT_RESOURCES);
82124758Semax
83124758Semax	SDP_PUT16(0, rsp);
84124758Semax
85124758Semax	/* Set reply size */
86124758Semax	srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t);
87124758Semax	srv->fdidx[fd].rsp_size = rsp - srv->fdidx[fd].rsp;
88124758Semax	srv->fdidx[fd].rsp_cs = 0;
89124758Semax
90124758Semax	return (0);
91124758Semax}
92124758Semax
93