xref: /freebsd/usr.sbin/bluetooth/sdpd/sd.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * sd.c
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id: sd.c,v 1.4 2004/01/13 01:54:39 max Exp $
31  * $FreeBSD$
32  */
33 
34 #include <sys/queue.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
37 #include <sdp.h>
38 #include <string.h>
39 #include "profile.h"
40 #include "provider.h"
41 
42 static int32_t
43 sd_profile_create_service_class_id_list(
44 		uint8_t *buf, uint8_t const * const eob,
45 		uint8_t const *data, uint32_t datalen)
46 {
47 	static uint16_t	service_classes[] = {
48 		SDP_SERVICE_CLASS_SERVICE_DISCOVERY_SERVER
49 	};
50 
51 	return (common_profile_create_service_class_id_list(
52 			buf, eob,
53 			(uint8_t const *) service_classes,
54 			sizeof(service_classes)));
55 }
56 
57 static int32_t
58 sd_profile_create_bluetooth_profile_descriptor_list(
59 		uint8_t *buf, uint8_t const * const eob,
60 		uint8_t const *data, uint32_t datalen)
61 {
62 	static uint16_t profile_descriptor_list[] = {
63 		SDP_SERVICE_CLASS_SERVICE_DISCOVERY_SERVER,
64 		0x0100
65 	};
66 
67 	return (common_profile_create_bluetooth_profile_descriptor_list(
68 			buf, eob,
69 			(uint8_t const *) profile_descriptor_list,
70 			sizeof(profile_descriptor_list)));
71 }
72 
73 static int32_t
74 sd_profile_create_service_id(
75 		uint8_t *buf, uint8_t const * const eob,
76 		uint8_t const *data, uint32_t datalen)
77 {
78 	if (buf + 3 > eob)
79 		return (-1);
80 
81 	/*
82 	 * The ServiceID is a UUID that universally and uniquely identifies
83 	 * the service instance described by the service record. This service
84 	 * attribute is particularly useful if the same service is described
85 	 * by service records in more than one SDP server
86 	 */
87 
88 	SDP_PUT8(SDP_DATA_UUID16, buf);
89 	SDP_PUT16(SDP_UUID_PROTOCOL_SDP, buf); /* XXX ??? */
90 
91 	return (3);
92 }
93 
94 static int32_t
95 sd_profile_create_service_name(
96 		uint8_t *buf, uint8_t const * const eob,
97 		uint8_t const *data, uint32_t datalen)
98 {
99 	static char	service_name[] = "Bluetooth service discovery";
100 
101 	return (common_profile_create_string8(
102 			buf, eob,
103 			(uint8_t const *) service_name, strlen(service_name)));
104 }
105 
106 static int32_t
107 sd_profile_create_protocol_descriptor_list(
108 		uint8_t *buf, uint8_t const * const eob,
109 		uint8_t const *data, uint32_t datalen)
110 {
111 	if (buf + 12 > eob)
112 		return (-1);
113 
114 	SDP_PUT8(SDP_DATA_SEQ8, buf);
115 	SDP_PUT8(10, buf);
116 
117 	SDP_PUT8(SDP_DATA_SEQ8, buf);
118 	SDP_PUT8(3, buf);
119 	SDP_PUT8(SDP_DATA_UUID16, buf);
120 	SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
121 
122 	SDP_PUT8(SDP_DATA_SEQ8, buf);
123 	SDP_PUT8(3, buf);
124 	SDP_PUT8(SDP_DATA_UUID16, buf);
125 	SDP_PUT16(SDP_UUID_PROTOCOL_SDP, buf);
126 
127 	return (12);
128 }
129 
130 static int32_t
131 sd_profile_create_browse_group_list(
132 		uint8_t *buf, uint8_t const * const eob,
133 		uint8_t const *data, uint32_t datalen)
134 {
135 	if (buf + 5 > eob)
136 		return (-1);
137 
138 	SDP_PUT8(SDP_DATA_SEQ8, buf);
139 	SDP_PUT8(3, buf);
140 
141 	/*
142 	 * The top-level browse group ID, called PublicBrowseRoot and
143 	 * representing the root of the browsing hierarchy, has the value
144 	 * 00001002-0000-1000-8000-00805F9B34FB (UUID16: 0x1002) from the
145 	 * Bluetooth Assigned Numbers document
146 	 */
147 
148 	SDP_PUT8(SDP_DATA_UUID16, buf);
149 	SDP_PUT16(SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP, buf);
150 
151 	return (5);
152 }
153 
154 static int32_t
155 sd_profile_create_version_number_list(
156 		uint8_t *buf, uint8_t const * const eob,
157 		uint8_t const *data, uint32_t datalen)
158 {
159 	if (buf + 5 > eob)
160 		return (-1);
161 
162 	SDP_PUT8(SDP_DATA_SEQ8, buf);
163 	SDP_PUT8(3, buf);
164 
165 	/*
166 	 * The VersionNumberList is a data element sequence in which each
167 	 * element of the sequence is a version number supported by the SDP
168 	 * server. A version number is a 16-bit unsigned integer consisting
169 	 * of two fields. The higher-order 8 bits contain the major version
170 	 * number field and the low-order 8 bits contain the minor version
171 	 * number field. The initial version of SDP has a major version of
172 	 * 1 and a minor version of 0
173 	 */
174 
175 	SDP_PUT8(SDP_DATA_UINT16, buf);
176 	SDP_PUT16(0x0100, buf);
177 
178 	return (5);
179 }
180 
181 static int32_t
182 sd_profile_create_service_database_state(
183 		uint8_t *buf, uint8_t const * const eob,
184 		uint8_t const *data, uint32_t datalen)
185 {
186 	uint32_t	change_state = provider_get_change_state();
187 
188 	if (buf + 5 > eob)
189 		return (-1);
190 
191 	SDP_PUT8(SDP_DATA_UINT32, buf);
192 	SDP_PUT32(change_state, buf);
193 
194 	return (5);
195 }
196 
197 static attr_t	sd_profile_attrs[] = {
198 	{ SDP_ATTR_SERVICE_RECORD_HANDLE,
199 	  common_profile_create_service_record_handle },
200 	{ SDP_ATTR_SERVICE_CLASS_ID_LIST,
201 	  sd_profile_create_service_class_id_list },
202 	{ SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
203 	  sd_profile_create_bluetooth_profile_descriptor_list },
204 	{ SDP_ATTR_SERVICE_ID,
205 	  sd_profile_create_service_id },
206 	{ SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST,
207 	  common_profile_create_language_base_attribute_id_list },
208 	{ SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_SERVICE_NAME_OFFSET,
209 	  sd_profile_create_service_name },
210 	{ SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_SERVICE_DESCRIPTION_OFFSET,
211 	  sd_profile_create_service_name },
212 	{ SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_PROVIDER_NAME_OFFSET,
213 	  common_profile_create_service_provider_name },
214 	{ SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
215 	  sd_profile_create_protocol_descriptor_list },
216 	{ SDP_ATTR_BROWSE_GROUP_LIST,
217 	  sd_profile_create_browse_group_list },
218 	{ SDP_ATTR_VERSION_NUMBER_LIST,
219 	  sd_profile_create_version_number_list },
220 	{ SDP_ATTR_SERVICE_DATABASE_STATE,
221 	  sd_profile_create_service_database_state },
222 	{ 0, NULL } /* end entry */
223 };
224 
225 profile_t	sd_profile_descriptor = {
226 	SDP_SERVICE_CLASS_SERVICE_DISCOVERY_SERVER,
227 	0,
228 	(profile_data_valid_p) NULL,
229 	(attr_t const * const) &sd_profile_attrs
230 };
231 
232