Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libbluetooth / sdp_record.c
blob21bec80cc095df58a95d8468c3a77c2ff4fac5e8
1 /* $NetBSD: sdp_record.c$ */
3 /*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Iain Hibbert.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: sdp_record.c$");
35 #include <errno.h>
36 #include <sdp.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
41 #include "sdp-int.h"
44 * This is the interface to sdpd(8); These PDU IDs are NOT part
45 * of the Bluetooth specification.
48 bool
49 sdp_record_insert(struct sdp_session *ss, bdaddr_t *bdaddr,
50 uint32_t *handle, const sdp_data_t *rec)
52 struct iovec req[4];
53 sdp_data_t hdr;
54 uint8_t data[5];
55 ssize_t len;
56 bdaddr_t ba;
57 uint16_t ec;
60 * setup BluetoothDeviceAddress
62 bdaddr_copy(&ba, (bdaddr == NULL) ? BDADDR_ANY : bdaddr);
63 req[1].iov_base = &ba;
64 req[1].iov_len = sizeof(bdaddr_t);
67 * setup ServiceRecord
69 len = rec->end - rec->next;
70 if (len < 0 || len > UINT16_MAX) {
71 errno = EINVAL;
72 return false;
75 hdr.next = data;
76 hdr.end = data + sizeof(data) + len;
77 sdp_put_seq(&hdr, len);
78 req[2].iov_base = data;
79 req[2].iov_len = hdr.next - data;
81 req[3].iov_base = rec->next;
82 req[3].iov_len = len;
85 * InsertRecord Transaction
87 if (!_sdp_send_pdu(ss, SDP_PDU_RECORD_INSERT_REQUEST,
88 req, __arraycount(req)))
89 return false;
91 len = _sdp_recv_pdu(ss, SDP_PDU_ERROR_RESPONSE);
92 if (len == -1)
93 return false;
95 if (len != sizeof(uint16_t) + sizeof(uint32_t)) {
96 errno = EIO;
97 return false;
101 * extract and check ErrorCode (success == 0)
103 ec = be16dec(ss->ibuf);
104 if (ec != 0) {
105 errno = _sdp_errno(ec);
106 return false;
110 * extract ServiceRecordHandle if required
112 if (handle != NULL)
113 *handle = be32dec(ss->ibuf + sizeof(uint16_t));
115 return true;
118 bool
119 sdp_record_update(struct sdp_session *ss, uint32_t handle,
120 const sdp_data_t *rec)
122 struct iovec req[4];
123 sdp_data_t hdr;
124 uint8_t data[5];
125 ssize_t len;
126 uint16_t ec;
129 * setup ServiceRecordHandle
131 handle = htobe32(handle);
132 req[1].iov_base = &handle;
133 req[1].iov_len = sizeof(handle);
136 * setup ServiceRecord
138 len = rec->end - rec->next;
139 if (len < 0 || len > UINT16_MAX) {
140 errno = EINVAL;
141 return false;
144 hdr.next = data;
145 hdr.end = data + sizeof(data) + len;
146 sdp_put_seq(&hdr, len);
147 req[2].iov_base = data;
148 req[2].iov_len = hdr.next - data;
150 req[3].iov_base = rec->next;
151 req[3].iov_len = len;
154 * UpdateRecord Transaction
156 if (!_sdp_send_pdu(ss, SDP_PDU_RECORD_UPDATE_REQUEST,
157 req, __arraycount(req)))
158 return false;
160 len = _sdp_recv_pdu(ss, SDP_PDU_ERROR_RESPONSE);
161 if (len == -1)
162 return false;
164 if (len != sizeof(uint16_t)) {
165 errno = EIO;
166 return false;
170 * extract and check ErrorCode (success == 0)
172 if ((ec = be16dec(ss->ibuf)) != 0) {
173 errno = _sdp_errno(ec);
174 return false;
177 return true;
180 bool
181 sdp_record_remove(struct sdp_session *ss, uint32_t handle)
183 struct iovec req[2];
184 ssize_t len;
185 uint16_t ec;
188 * setup ServiceRecordHandle
190 handle = htobe32(handle);
191 req[1].iov_base = &handle;
192 req[1].iov_len = sizeof(handle);
195 * RemoveRecord Transaction
197 if (!_sdp_send_pdu(ss, SDP_PDU_RECORD_REMOVE_REQUEST,
198 req, __arraycount(req)))
199 return false;
201 len = _sdp_recv_pdu(ss, SDP_PDU_ERROR_RESPONSE);
202 if (len == -1)
203 return false;
205 if (len != sizeof(uint16_t)) {
206 errno = EIO;
207 return false;
211 * extract and check ErrorCode (success == 0)
213 ec = be16dec(ss->ibuf);
214 if (ec != 0) {
215 errno = _sdp_errno(ec);
216 return false;
219 return true;