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