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: service.c,v 1.1 2004/01/13 19:32:36 max Exp $
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <bluetooth.h>
43 static int32_t sdp_receive_error_pdu(sdp_session_p ss);
46 sdp_register_service(void *xss, uint16_t uuid, bdaddr_p const bdaddr,
47 uint8_t const *data, uint32_t datalen, uint32_t *handle)
49 sdp_session_p ss = (sdp_session_p) xss;
56 if (bdaddr == NULL || data == NULL ||
57 datalen == 0 || !(ss->flags & SDP_SESSION_LOCAL)) {
61 if (sizeof(pdu)+sizeof(uuid)+sizeof(*bdaddr)+datalen > SDP_LOCAL_MTU) {
66 pdu.pid = SDP_PDU_SERVICE_REGISTER_REQUEST;
67 pdu.tid = htons(++ss->tid);
68 pdu.len = htons(sizeof(uuid) + sizeof(*bdaddr) + datalen);
72 iov[0].iov_base = (void *) &pdu;
73 iov[0].iov_len = sizeof(pdu);
75 iov[1].iov_base = (void *) &uuid;
76 iov[1].iov_len = sizeof(uuid);
78 iov[2].iov_base = (void *) bdaddr;
79 iov[2].iov_len = sizeof(*bdaddr);
81 iov[3].iov_base = (void *) data;
82 iov[3].iov_len = datalen;
85 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
86 } while (len < 0 && errno == EINTR);
93 len = sdp_receive_error_pdu(ss);
96 if (len != sizeof(pdu) + sizeof(uint16_t) + sizeof(uint32_t)) {
101 if (handle != NULL) {
102 *handle = (uint32_t) ss->rsp[--len];
103 *handle |= (uint32_t) ss->rsp[--len] << 8;
104 *handle |= (uint32_t) ss->rsp[--len] << 16;
105 *handle |= (uint32_t) ss->rsp[--len] << 24;
112 sdp_unregister_service(void *xss, uint32_t handle)
114 sdp_session_p ss = (sdp_session_p) xss;
121 if (!(ss->flags & SDP_SESSION_LOCAL)) {
125 if (sizeof(pdu) + sizeof(handle) > SDP_LOCAL_MTU) {
126 ss->error = EMSGSIZE;
130 pdu.pid = SDP_PDU_SERVICE_UNREGISTER_REQUEST;
131 pdu.tid = htons(++ss->tid);
132 pdu.len = htons(sizeof(handle));
134 handle = htonl(handle);
136 iov[0].iov_base = (void *) &pdu;
137 iov[0].iov_len = sizeof(pdu);
139 iov[1].iov_base = (void *) &handle;
140 iov[1].iov_len = sizeof(handle);
143 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
144 } while (len < 0 && errno == EINTR);
151 return ((sdp_receive_error_pdu(ss) < 0)? -1 : 0);
155 sdp_change_service(void *xss, uint32_t handle,
156 uint8_t const *data, uint32_t datalen)
158 sdp_session_p ss = (sdp_session_p) xss;
165 if (data == NULL || datalen == 0 || !(ss->flags & SDP_SESSION_LOCAL)) {
169 if (sizeof(pdu) + sizeof(handle) + datalen > SDP_LOCAL_MTU) {
170 ss->error = EMSGSIZE;
174 pdu.pid = SDP_PDU_SERVICE_CHANGE_REQUEST;
175 pdu.tid = htons(++ss->tid);
176 pdu.len = htons(sizeof(handle) + datalen);
178 handle = htons(handle);
180 iov[0].iov_base = (void *) &pdu;
181 iov[0].iov_len = sizeof(pdu);
183 iov[1].iov_base = (void *) &handle;
184 iov[1].iov_len = sizeof(handle);
186 iov[2].iov_base = (void *) data;
187 iov[2].iov_len = datalen;
190 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
191 } while (len < 0 && errno == EINTR);
198 return ((sdp_receive_error_pdu(ss) < 0)? -1 : 0);
202 sdp_receive_error_pdu(sdp_session_p ss)
209 len = read(ss->s, ss->rsp, ss->rsp_e - ss->rsp);
210 } while (len < 0 && errno == EINTR);
217 pdu = (sdp_pdu_p) ss->rsp;
218 pdu->tid = ntohs(pdu->tid);
219 pdu->len = ntohs(pdu->len);
221 if (pdu->pid != SDP_PDU_ERROR_RESPONSE || pdu->tid != ss->tid ||
222 pdu->len < 2 || pdu->len != len - sizeof(*pdu)) {
227 error = (uint16_t) ss->rsp[sizeof(pdu)] << 8;
228 error |= (uint16_t) ss->rsp[sizeof(pdu) + 1];