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 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
44 static int32_t sdp_receive_error_pdu(sdp_session_p ss);
47 sdp_register_service(void *xss, uint16_t uuid, bdaddr_p const bdaddr,
48 uint8_t const *data, uint32_t datalen, uint32_t *handle)
50 sdp_session_p ss = (sdp_session_p) xss;
57 if (bdaddr == NULL || data == NULL ||
58 datalen == 0 || !(ss->flags & SDP_SESSION_LOCAL)) {
62 if (sizeof(pdu)+sizeof(uuid)+sizeof(*bdaddr)+datalen > SDP_LOCAL_MTU) {
67 pdu.pid = SDP_PDU_SERVICE_REGISTER_REQUEST;
68 pdu.tid = htons(++ss->tid);
69 pdu.len = htons(sizeof(uuid) + sizeof(*bdaddr) + datalen);
73 iov[0].iov_base = (void *) &pdu;
74 iov[0].iov_len = sizeof(pdu);
76 iov[1].iov_base = (void *) &uuid;
77 iov[1].iov_len = sizeof(uuid);
79 iov[2].iov_base = (void *) bdaddr;
80 iov[2].iov_len = sizeof(*bdaddr);
82 iov[3].iov_base = (void *) data;
83 iov[3].iov_len = datalen;
86 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
87 } while (len < 0 && errno == EINTR);
94 len = sdp_receive_error_pdu(ss);
97 if (len != sizeof(pdu) + sizeof(uint16_t) + sizeof(uint32_t)) {
102 if (handle != NULL) {
103 *handle = (uint32_t) ss->rsp[--len];
104 *handle |= (uint32_t) ss->rsp[--len] << 8;
105 *handle |= (uint32_t) ss->rsp[--len] << 16;
106 *handle |= (uint32_t) ss->rsp[--len] << 24;
113 sdp_unregister_service(void *xss, uint32_t handle)
115 sdp_session_p ss = (sdp_session_p) xss;
122 if (!(ss->flags & SDP_SESSION_LOCAL)) {
126 if (sizeof(pdu) + sizeof(handle) > SDP_LOCAL_MTU) {
127 ss->error = EMSGSIZE;
131 pdu.pid = SDP_PDU_SERVICE_UNREGISTER_REQUEST;
132 pdu.tid = htons(++ss->tid);
133 pdu.len = htons(sizeof(handle));
135 handle = htonl(handle);
137 iov[0].iov_base = (void *) &pdu;
138 iov[0].iov_len = sizeof(pdu);
140 iov[1].iov_base = (void *) &handle;
141 iov[1].iov_len = sizeof(handle);
144 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
145 } while (len < 0 && errno == EINTR);
152 return ((sdp_receive_error_pdu(ss) < 0)? -1 : 0);
156 sdp_change_service(void *xss, uint32_t handle,
157 uint8_t const *data, uint32_t datalen)
159 sdp_session_p ss = (sdp_session_p) xss;
166 if (data == NULL || datalen == 0 || !(ss->flags & SDP_SESSION_LOCAL)) {
170 if (sizeof(pdu) + sizeof(handle) + datalen > SDP_LOCAL_MTU) {
171 ss->error = EMSGSIZE;
175 pdu.pid = SDP_PDU_SERVICE_CHANGE_REQUEST;
176 pdu.tid = htons(++ss->tid);
177 pdu.len = htons(sizeof(handle) + datalen);
179 handle = htons(handle);
181 iov[0].iov_base = (void *) &pdu;
182 iov[0].iov_len = sizeof(pdu);
184 iov[1].iov_base = (void *) &handle;
185 iov[1].iov_len = sizeof(handle);
187 iov[2].iov_base = (void *) data;
188 iov[2].iov_len = datalen;
191 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
192 } while (len < 0 && errno == EINTR);
199 return ((sdp_receive_error_pdu(ss) < 0)? -1 : 0);
203 sdp_receive_error_pdu(sdp_session_p ss)
210 len = read(ss->s, ss->rsp, ss->rsp_e - ss->rsp);
211 } while (len < 0 && errno == EINTR);
218 pdu = (sdp_pdu_p) ss->rsp;
219 pdu->tid = ntohs(pdu->tid);
220 pdu->len = ntohs(pdu->len);
222 if (pdu->pid != SDP_PDU_ERROR_RESPONSE || pdu->tid != ss->tid ||
223 pdu->len < 2 || pdu->len != len - sizeof(*pdu)) {
228 error = (uint16_t) ss->rsp[sizeof(pdu)] << 8;
229 error |= (uint16_t) ss->rsp[sizeof(pdu) + 1];