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