Add bluetooth userspace libraries - bluetooth(3) and sdp(3).
[dragonfly.git] / lib / libsdp / service.c
1 /* $NetBSD: service.c,v 1.1 2006/06/19 15:44:36 gdamore Exp $ */
2 /* $DragonFly: src/lib/libsdp/service.c,v 1.1 2008/01/03 11:47:53 hasso Exp $ */
3
4 /*-
5  * Copyright (c) 2006 Itronix Inc.
6  * All rights reserved.
7  *
8  * Written by Iain Hibbert for Itronix Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of Itronix Inc. may not be used to endorse
19  *    or promote products derived from this software without specific
20  *    prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 /*
35  * service.c
36  *
37  * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
38  * All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  * $Id: service.c,v 1.1 2006/06/19 15:44:36 gdamore Exp $
62  * $FreeBSD: src/lib/libsdp/service.c,v 1.1 2004/01/20 20:48:25 emax Exp $
63  */
64
65 #include <sys/uio.h>
66 #include <netinet/in.h>
67 #include <arpa/inet.h>
68 #include <bluetooth.h>
69 #include <errno.h>
70 #include <string.h>
71 #include <unistd.h>
72
73 #include <sdp-int.h>
74 #include <sdp.h>
75
76 static int32_t sdp_receive_error_pdu(sdp_session_p ss);
77
78 int32_t
79 sdp_register_service(void *xss, uint16_t uuid, bdaddr_t *bdaddr,
80                 uint8_t *data, uint32_t datalen, uint32_t *handle)
81 {
82         sdp_session_p   ss = (sdp_session_p) xss;
83         struct iovec    iov[4];
84         sdp_pdu_t       pdu;
85         int32_t         len;
86
87         if (ss == NULL)
88                 return (-1);
89         if (bdaddr == NULL || data == NULL ||
90             datalen == 0 || !(ss->flags & SDP_SESSION_LOCAL)) {
91                 ss->error = EINVAL;
92                 return (-1);
93         }
94         if (sizeof(pdu)+sizeof(uuid)+sizeof(*bdaddr)+datalen > SDP_LOCAL_MTU) {
95                 ss->error = EMSGSIZE;
96                 return (-1);
97         }
98
99         pdu.pid = SDP_PDU_SERVICE_REGISTER_REQUEST;
100         pdu.tid = htons(++ss->tid);
101         pdu.len = htons(sizeof(uuid) + sizeof(*bdaddr) + datalen);
102
103         uuid = htons(uuid);
104
105         iov[0].iov_base = (void *) &pdu;
106         iov[0].iov_len = sizeof(pdu);
107
108         iov[1].iov_base = (void *) &uuid;
109         iov[1].iov_len = sizeof(uuid);
110
111         iov[2].iov_base = (void *) bdaddr;
112         iov[2].iov_len = sizeof(*bdaddr);
113
114         iov[3].iov_base = (void *) data;
115         iov[3].iov_len = datalen;
116
117         do {
118                 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
119         } while (len < 0 && errno == EINTR);
120
121         if (len < 0) {
122                 ss->error = errno;
123                 return (-1);
124         }
125
126         len = sdp_receive_error_pdu(ss);
127         if (len < 0)
128                 return (-1);
129         if (len != sizeof(pdu) + sizeof(uint16_t) + sizeof(uint32_t)) {
130                 ss->error = EIO;
131                 return (-1);
132         }
133
134         if (handle != NULL) {
135                 *handle  = (uint32_t) ss->rsp[--len];
136                 *handle |= (uint32_t) ss->rsp[--len] << 8;
137                 *handle |= (uint32_t) ss->rsp[--len] << 16;
138                 *handle |= (uint32_t) ss->rsp[--len] << 24;
139         }
140
141         return (0);
142 }
143
144 int32_t
145 sdp_unregister_service(void *xss, uint32_t handle)
146 {
147         sdp_session_p   ss = (sdp_session_p) xss;
148         struct iovec    iov[2];
149         sdp_pdu_t       pdu;
150         int32_t         len;
151
152         if (ss == NULL)
153                 return (-1);
154         if (!(ss->flags & SDP_SESSION_LOCAL)) {
155                 ss->error = EINVAL;
156                 return (-1);
157         }
158 #if 0
159         if (sizeof(pdu) + sizeof(handle) > SDP_LOCAL_MTU) {
160                 ss->error = EMSGSIZE;
161                 return (-1);
162         }
163 #endif
164
165         pdu.pid = SDP_PDU_SERVICE_UNREGISTER_REQUEST;
166         pdu.tid = htons(++ss->tid);
167         pdu.len = htons(sizeof(handle));
168
169         handle = htonl(handle);
170
171         iov[0].iov_base = (void *) &pdu;
172         iov[0].iov_len = sizeof(pdu);
173
174         iov[1].iov_base = (void *) &handle;
175         iov[1].iov_len = sizeof(handle);
176
177         do {
178                 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
179         } while (len < 0 && errno == EINTR);
180
181         if (len < 0) {
182                 ss->error = errno;
183                 return (-1);
184         }
185
186         return ((sdp_receive_error_pdu(ss) < 0)? -1 : 0);
187 }
188
189 int32_t
190 sdp_change_service(void *xss, uint32_t handle,
191                 uint8_t *data, uint32_t datalen)
192 {
193         sdp_session_p   ss = (sdp_session_p) xss;
194         struct iovec    iov[3];
195         sdp_pdu_t       pdu;
196         int32_t         len;
197
198         if (ss == NULL)
199                 return (-1);
200         if (data == NULL || datalen == 0 || !(ss->flags & SDP_SESSION_LOCAL)) {
201                 ss->error = EINVAL;
202                 return (-1);
203         }
204         if (sizeof(pdu) + sizeof(handle) + datalen > SDP_LOCAL_MTU) {
205                 ss->error = EMSGSIZE;
206                 return (-1);
207         }
208
209         pdu.pid = SDP_PDU_SERVICE_CHANGE_REQUEST;
210         pdu.tid = htons(++ss->tid);
211         pdu.len = htons(sizeof(handle) + datalen);
212
213         handle = htons(handle);
214
215         iov[0].iov_base = (void *) &pdu;
216         iov[0].iov_len = sizeof(pdu);
217
218         iov[1].iov_base = (void *) &handle;
219         iov[1].iov_len = sizeof(handle);
220
221         iov[2].iov_base = (void *) data;
222         iov[2].iov_len = datalen;
223
224         do {
225                 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
226         } while (len < 0 && errno == EINTR);
227
228         if (len < 0) {
229                 ss->error = errno;
230                 return (-1);
231         }
232
233         return ((sdp_receive_error_pdu(ss) < 0)? -1 : 0);
234 }
235
236 static int32_t
237 sdp_receive_error_pdu(sdp_session_p ss)
238 {
239         sdp_pdu_t       pdu;
240         int32_t         len;
241         uint16_t        error;
242
243         do {
244                 len = read(ss->s, ss->rsp, (size_t)(ss->rsp_e - ss->rsp));
245         } while (len < 0 && errno == EINTR);
246
247         if (len < 0) {
248                 ss->error = errno;
249                 return (-1);
250         }
251
252         memcpy(&pdu, ss->rsp, sizeof(pdu));
253         pdu.tid = ntohs(pdu.tid);
254         pdu.len = ntohs(pdu.len);
255         memcpy(ss->rsp, &pdu, sizeof(pdu));
256
257         if (pdu.pid != SDP_PDU_ERROR_RESPONSE || pdu.tid != ss->tid ||
258             pdu.len < 2 || pdu.len != len - sizeof(pdu)) {
259                 ss->error = EIO;
260                 return (-1);
261         }
262
263         error  = (uint16_t) ss->rsp[sizeof(pdu)] << 8;
264         error |= (uint16_t) ss->rsp[sizeof(pdu) + 1];
265
266         if (error != 0) {
267                 ss->error = EIO;
268                 return (-1);
269         }
270
271         return (len);
272 }