xref: /netbsd/lib/libbluetooth/sdp_record.c (revision 6550d01e)
1 /*	$NetBSD: sdp_record.c,v 1.1 2009/05/12 10:05:06 plunky Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Iain Hibbert.
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  *
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.
30  */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: sdp_record.c,v 1.1 2009/05/12 10:05:06 plunky Exp $");
34 
35 #include <errno.h>
36 #include <sdp.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include "sdp-int.h"
42 
43 /*
44  * This is the interface to sdpd(8); These PDU IDs are NOT part
45  * of the Bluetooth specification.
46  */
47 
48 bool
49 sdp_record_insert(struct sdp_session *ss, bdaddr_t *bdaddr,
50     uint32_t *handle, const sdp_data_t *rec)
51 {
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;
58 
59 	/*
60 	 * setup BluetoothDeviceAddress
61 	 */
62 	bdaddr_copy(&ba, (bdaddr == NULL) ? BDADDR_ANY : bdaddr);
63 	req[1].iov_base = &ba;
64 	req[1].iov_len = sizeof(bdaddr_t);
65 
66 	/*
67 	 * setup ServiceRecord
68 	 */
69 	len = rec->end - rec->next;
70 	if (len < 0 || len > UINT16_MAX) {
71 		errno = EINVAL;
72 		return false;
73 	}
74 
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;
80 
81 	req[3].iov_base = rec->next;
82 	req[3].iov_len = len;
83 
84 	/*
85 	 * InsertRecord Transaction
86 	 */
87 	if (!_sdp_send_pdu(ss, SDP_PDU_RECORD_INSERT_REQUEST,
88 	    req, __arraycount(req)))
89 		return false;
90 
91 	len = _sdp_recv_pdu(ss, SDP_PDU_ERROR_RESPONSE);
92 	if (len == -1)
93 		return false;
94 
95 	if (len != sizeof(uint16_t) + sizeof(uint32_t)) {
96 		errno = EIO;
97 		return false;
98 	}
99 
100 	/*
101 	 * extract and check ErrorCode (success == 0)
102 	 */
103 	ec = be16dec(ss->ibuf);
104 	if (ec != 0) {
105 		errno = _sdp_errno(ec);
106 		return false;
107 	}
108 
109 	/*
110 	 * extract ServiceRecordHandle if required
111 	 */
112 	if (handle != NULL)
113 		*handle = be32dec(ss->ibuf + sizeof(uint16_t));
114 
115 	return true;
116 }
117 
118 bool
119 sdp_record_update(struct sdp_session *ss, uint32_t handle,
120     const sdp_data_t *rec)
121 {
122 	struct iovec	req[4];
123 	sdp_data_t	hdr;
124 	uint8_t		data[5];
125 	ssize_t		len;
126 	uint16_t	ec;
127 
128 	/*
129 	 * setup ServiceRecordHandle
130 	 */
131 	handle = htobe32(handle);
132 	req[1].iov_base = &handle;
133 	req[1].iov_len = sizeof(handle);
134 
135 	/*
136 	 * setup ServiceRecord
137 	 */
138 	len = rec->end - rec->next;
139 	if (len < 0 || len > UINT16_MAX) {
140 		errno = EINVAL;
141 		return false;
142 	}
143 
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;
149 
150 	req[3].iov_base = rec->next;
151 	req[3].iov_len = len;
152 
153 	/*
154 	 * UpdateRecord Transaction
155 	 */
156 	if (!_sdp_send_pdu(ss, SDP_PDU_RECORD_UPDATE_REQUEST,
157 	    req, __arraycount(req)))
158 		return false;
159 
160 	len = _sdp_recv_pdu(ss, SDP_PDU_ERROR_RESPONSE);
161 	if (len == -1)
162 		return false;
163 
164 	if (len != sizeof(uint16_t)) {
165 		errno = EIO;
166 		return false;
167 	}
168 
169 	/*
170 	 * extract and check ErrorCode (success == 0)
171 	 */
172 	if ((ec = be16dec(ss->ibuf)) != 0) {
173 		errno = _sdp_errno(ec);
174 		return false;
175 	}
176 
177 	return true;
178 }
179 
180 bool
181 sdp_record_remove(struct sdp_session *ss, uint32_t handle)
182 {
183 	struct iovec	req[2];
184 	ssize_t		len;
185 	uint16_t	ec;
186 
187 	/*
188 	 * setup ServiceRecordHandle
189 	 */
190 	handle = htobe32(handle);
191 	req[1].iov_base = &handle;
192 	req[1].iov_len = sizeof(handle);
193 
194 	/*
195 	 * RemoveRecord Transaction
196 	 */
197 	if (!_sdp_send_pdu(ss, SDP_PDU_RECORD_REMOVE_REQUEST,
198 	    req, __arraycount(req)))
199 		return false;
200 
201 	len = _sdp_recv_pdu(ss, SDP_PDU_ERROR_RESPONSE);
202 	if (len == -1)
203 		return false;
204 
205 	if (len != sizeof(uint16_t)) {
206 		errno = EIO;
207 		return false;
208 	}
209 
210 	/*
211 	 * extract and check ErrorCode (success == 0)
212 	 */
213 	ec = be16dec(ss->ibuf);
214 	if (ec != 0) {
215 		errno = _sdp_errno(ec);
216 		return false;
217 	}
218 
219 	return true;
220 }
221