Add sdpd(8) (Bluetooth Service Discovery Protocol daemon) and sdpquery(1)
[dragonfly.git] / usr.sbin / sdpd / sd.c
1 /* $NetBSD: sd.c,v 1.2 2007/11/09 20:08:41 plunky Exp $ */
2 /* $DragonFly: src/usr.sbin/sdpd/sd.c,v 1.1 2008/01/06 21:51:30 hasso Exp $ */
3
4 /*
5  * sd.c
6  *
7  * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
8  * All rights reserved.
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $Id: sd.c,v 1.2 2007/11/30 07:39:37 griffin Exp $
32  * $FreeBSD: src/usr.sbin/bluetooth/sdpd/sd.c,v 1.1 2004/01/20 20:48:26 emax Exp $
33  */
34
35 #include <sys/queue.h>
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_service_id(
59                 uint8_t *buf, uint8_t const * const eob,
60                 uint8_t const *data, uint32_t datalen)
61 {
62         if (buf + 3 > eob)
63                 return (-1);
64
65         /*
66          * The ServiceID is a UUID that universally and uniquely identifies
67          * the service instance described by the service record. This service
68          * attribute is particularly useful if the same service is described
69          * by service records in more than one SDP server
70          */
71
72         SDP_PUT8(SDP_DATA_UUID16, buf);
73         SDP_PUT16(SDP_UUID_PROTOCOL_SDP, buf); /* XXX ??? */
74
75         return (3);
76 }
77
78 static int32_t
79 sd_profile_create_service_name(
80                 uint8_t *buf, uint8_t const * const eob,
81                 uint8_t const *data, uint32_t datalen)
82 {
83         static char     service_name[] = "Bluetooth service discovery";
84
85         return (common_profile_create_string8(
86                         buf, eob,
87                         (uint8_t const *) service_name, strlen(service_name)));
88 }
89
90 static int32_t
91 sd_profile_create_protocol_descriptor_list(
92                 uint8_t *buf, uint8_t const * const eob,
93                 uint8_t const *data, uint32_t datalen)
94 {
95         if (buf + 13 > eob)
96                 return (-1);
97
98         SDP_PUT8(SDP_DATA_SEQ8, buf);
99         SDP_PUT8(11, buf);
100
101         SDP_PUT8(SDP_DATA_SEQ8, buf);
102         SDP_PUT8(9, buf);
103
104         SDP_PUT8(SDP_DATA_UUID16, buf);
105         SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
106
107         SDP_PUT8(SDP_DATA_UINT16, buf);
108         SDP_PUT16(L2CAP_PSM_SDP, buf);
109
110         SDP_PUT8(SDP_DATA_UINT16, buf);
111         SDP_PUT16(1, buf); /* version */
112
113         return (13);
114 }
115
116 static int32_t
117 sd_profile_create_browse_group_list(
118                 uint8_t *buf, uint8_t const * const eob,
119                 uint8_t const *data, uint32_t datalen)
120 {
121         if (buf + 5 > eob)
122                 return (-1);
123
124         SDP_PUT8(SDP_DATA_SEQ8, buf);
125         SDP_PUT8(3, buf);
126
127         /*
128          * The top-level browse group ID, called PublicBrowseRoot and
129          * representing the root of the browsing hierarchy, has the value
130          * 00001002-0000-1000-8000-00805F9B34FB (UUID16: 0x1002) from the
131          * Bluetooth Assigned Numbers document
132          */
133
134         SDP_PUT8(SDP_DATA_UUID16, buf);
135         SDP_PUT16(SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP, buf);
136
137         return (5);
138 }
139
140 static int32_t
141 sd_profile_create_version_number_list(
142                 uint8_t *buf, uint8_t const * const eob,
143                 uint8_t const *data, uint32_t datalen)
144 {
145         if (buf + 5 > eob)
146                 return (-1);
147
148         SDP_PUT8(SDP_DATA_SEQ8, buf);
149         SDP_PUT8(3, buf);
150
151         /*
152          * The VersionNumberList is a data element sequence in which each
153          * element of the sequence is a version number supported by the SDP
154          * server. A version number is a 16-bit unsigned integer consisting
155          * of two fields. The higher-order 8 bits contain the major version
156          * number field and the low-order 8 bits contain the minor version
157          * number field. The initial version of SDP has a major version of
158          * 1 and a minor version of 0
159          */
160
161         SDP_PUT8(SDP_DATA_UINT16, buf);
162         SDP_PUT16(0x0100, buf);
163
164         return (5);
165 }
166
167 static int32_t
168 sd_profile_create_service_database_state(
169                 uint8_t *buf, uint8_t const * const eob,
170                 uint8_t const *data, uint32_t datalen)
171 {
172         uint32_t        change_state = provider_get_change_state();
173
174         if (buf + 5 > eob)
175                 return (-1);
176
177         SDP_PUT8(SDP_DATA_UINT32, buf);
178         SDP_PUT32(change_state, buf);
179
180         return (5);
181 }
182
183 static attr_t   sd_profile_attrs[] = {
184         { SDP_ATTR_SERVICE_RECORD_HANDLE,
185           common_profile_create_service_record_handle },
186         { SDP_ATTR_SERVICE_CLASS_ID_LIST,
187           sd_profile_create_service_class_id_list },
188         { SDP_ATTR_SERVICE_ID,
189           sd_profile_create_service_id },
190         { SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST,
191           common_profile_create_language_base_attribute_id_list },
192         { SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_SERVICE_NAME_OFFSET,
193           sd_profile_create_service_name },
194         { SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_SERVICE_DESCRIPTION_OFFSET,
195           sd_profile_create_service_name },
196         { SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_PROVIDER_NAME_OFFSET,
197           common_profile_create_service_provider_name },
198         { SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
199           sd_profile_create_protocol_descriptor_list },
200         { SDP_ATTR_BROWSE_GROUP_LIST,
201           sd_profile_create_browse_group_list },
202         { SDP_ATTR_VERSION_NUMBER_LIST,
203           sd_profile_create_version_number_list },
204         { SDP_ATTR_SERVICE_DATABASE_STATE,
205           sd_profile_create_service_database_state },
206         { 0, NULL } /* end entry */
207 };
208
209 static uint16_t sd_profile_uuids[] = {
210         SDP_SERVICE_CLASS_SERVICE_DISCOVERY_SERVER,
211         SDP_UUID_PROTOCOL_L2CAP,
212 };
213
214 profile_t       sd_profile_descriptor = {
215         sd_profile_uuids,
216         sizeof(sd_profile_uuids),
217         0,
218         (profile_data_valid_p) NULL,
219         (attr_t const * const) &sd_profile_attrs
220 };