kernel - Order ipfw3 module before other ipfw3_* modules
[dragonfly.git] / usr.sbin / sdpd / profile.c
blobc1d33a5d1618af1b95ce9721ef7b6e1de0c620c7
1 /* $NetBSD: profile.c,v 1.4 2007/11/09 20:08:41 plunky Exp $ */
2 /* $DragonFly: src/usr.sbin/sdpd/profile.c,v 1.1 2008/01/06 21:51:30 hasso Exp $ */
4 /*
5 * profile.c
7 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
8 * All rights reserved.
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.
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.
31 * $Id: profile.c,v 1.2 2007/11/30 07:39:37 griffin Exp $
32 * $FreeBSD: src/usr.sbin/bluetooth/sdpd/profile.c,v 1.2 2004/07/28 07:15:44 kan Exp $
35 #include <sys/queue.h>
36 #include <sys/utsname.h>
37 #include <bluetooth.h>
38 #include <sdp.h>
39 #include <string.h>
40 #include "profile.h"
41 #include "provider.h"
44 * Lookup profile descriptor
47 profile_p
48 profile_get_descriptor(uint16_t uuid)
50 extern profile_t dun_profile_descriptor;
51 extern profile_t ftrn_profile_descriptor;
52 extern profile_t hf_profile_descriptor;
53 extern profile_t hset_profile_descriptor;
54 extern profile_t irmc_profile_descriptor;
55 extern profile_t irmc_command_profile_descriptor;
56 extern profile_t lan_profile_descriptor;
57 extern profile_t opush_profile_descriptor;
58 extern profile_t sp_profile_descriptor;
60 static const profile_p profiles[] = {
61 &dun_profile_descriptor,
62 &ftrn_profile_descriptor,
63 &hf_profile_descriptor,
64 &hset_profile_descriptor,
65 &irmc_profile_descriptor,
66 &irmc_command_profile_descriptor,
67 &lan_profile_descriptor,
68 &opush_profile_descriptor,
69 &sp_profile_descriptor
72 int32_t i;
74 for (i = 0; i < sizeof(profiles)/sizeof(profiles[0]); i++)
75 if (profiles[i]->uuid[0] == uuid)
76 return (profiles[i]);
78 return (NULL);
82 * Look attribute in the profile descripror
85 profile_attr_create_p
86 profile_get_attr(const profile_p profile, uint16_t attr)
88 attr_t const *ad = (attr_t const *) profile->attrs;
90 for (; ad->create != NULL; ad ++)
91 if (ad->attr == attr)
92 return (ad->create);
94 return (NULL);
98 * uint32 value32 - 5 bytes
101 int32_t
102 common_profile_create_service_record_handle(
103 uint8_t *buf, uint8_t const * const eob,
104 uint8_t const *data, uint32_t datalen)
106 if (buf + 5 > eob)
107 return (-1);
109 SDP_PUT8(SDP_DATA_UINT32, buf);
110 SDP_PUT32(((provider_t const *) data)->handle, buf);
112 return (5);
116 * seq8 len8 - 2 bytes
117 * uuid16 value16 - 3 bytes
118 * [ uuid16 value ]
121 int32_t
122 common_profile_create_service_class_id_list(
123 uint8_t *buf, uint8_t const * const eob,
124 uint8_t const *data, uint32_t datalen)
126 int32_t len = 3 * (datalen >>= 1);
128 if (len <= 0 || len > 0xff || buf + 2 + len > eob)
129 return (-1);
131 SDP_PUT8(SDP_DATA_SEQ8, buf);
132 SDP_PUT8(len, buf);
134 for (; datalen > 0; datalen --) {
135 SDP_PUT8(SDP_DATA_UUID16, buf);
136 SDP_PUT16(*((uint16_t const *)data), buf);
137 data += sizeof(uint16_t);
140 return (2 + len);
144 * seq8 len8 - 2 bytes
145 * seq 8 len8 - 2 bytes
146 * uuid16 value16 - 3 bytes
147 * uint16 value16 - 3 bytes
148 * [ seq 8 len8
149 * uuid16 value16
150 * uint16 value16 ]
153 int32_t
154 common_profile_create_bluetooth_profile_descriptor_list(
155 uint8_t *buf, uint8_t const * const eob,
156 uint8_t const *data, uint32_t datalen)
158 int32_t len = 8 * (datalen >>= 2);
160 if (len <= 0 || len > 0xff || buf + 2 + len > eob)
161 return (-1);
163 SDP_PUT8(SDP_DATA_SEQ8, buf);
164 SDP_PUT8(len, buf);
166 for (; datalen > 0; datalen --) {
167 SDP_PUT8(SDP_DATA_SEQ8, buf);
168 SDP_PUT8(6, buf);
169 SDP_PUT8(SDP_DATA_UUID16, buf);
170 SDP_PUT16(*((uint16_t const *)data), buf);
171 data += sizeof(uint16_t);
172 SDP_PUT8(SDP_DATA_UINT16, buf);
173 SDP_PUT16(*((uint16_t const *)data), buf);
174 data += sizeof(uint16_t);
177 return (2 + len);
181 * seq8 len8 - 2 bytes
182 * uint16 value16 - 3 bytes
183 * uint16 value16 - 3 bytes
184 * uint16 value16 - 3 bytes
187 int32_t
188 common_profile_create_language_base_attribute_id_list(
189 uint8_t *buf, uint8_t const * const eob,
190 uint8_t const *data, uint32_t datalen)
192 if (buf + 11 > eob)
193 return (-1);
195 SDP_PUT8(SDP_DATA_SEQ8, buf);
196 SDP_PUT8(9, buf);
199 * Language code per ISO 639:1988. Use "en".
202 SDP_PUT8(SDP_DATA_UINT16, buf);
203 SDP_PUT16(((0x65 << 8) | 0x6e), buf);
206 * Encoding. Recommended is UTF-8. ISO639 UTF-8 MIBenum is 106
207 * (http://www.iana.org/assignments/character-sets)
210 SDP_PUT8(SDP_DATA_UINT16, buf);
211 SDP_PUT16(106, buf);
214 * Offset (Primary Language Base is 0x100)
217 SDP_PUT8(SDP_DATA_UINT16, buf);
218 SDP_PUT16(SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID, buf);
220 return (11);
224 * Use Operating System name as provider name
227 int32_t
228 common_profile_create_service_provider_name(
229 uint8_t *buf, uint8_t const * const eob,
230 uint8_t const *data, uint32_t datalen)
232 struct utsname u;
233 char const *name;
235 if (uname(&u) < 0)
236 name = "Unknown";
237 else
238 name = u.sysname;
240 return (common_profile_create_string8(buf, eob,
241 (uint8_t const *)name, strlen(name)));
245 * str8 len8 string
248 int32_t
249 common_profile_create_string8(
250 uint8_t *buf, uint8_t const * const eob,
251 uint8_t const *data, uint32_t datalen)
253 if (datalen == 0 || datalen > 0xff || buf + 2 + datalen > eob)
254 return (-1);
256 SDP_PUT8(SDP_DATA_STR8, buf);
257 SDP_PUT8(datalen, buf);
258 memcpy(buf, data, datalen);
260 return (2 + datalen);
264 * seq8 len8 - 2 bytes
265 * seq8 len8 - 2 bytes
266 * uuid16 value16 - 3 bytes
267 * seq8 len8 - 2 bytes
268 * uuid16 value16 - 3 bytes
269 * uint8 value8 - 2 bytes
272 int32_t
273 rfcomm_profile_create_protocol_descriptor_list(
274 uint8_t *buf, uint8_t const * const eob,
275 uint8_t const *data, uint32_t datalen)
277 if (datalen != 1 || buf + 14 > eob)
278 return (-1);
280 SDP_PUT8(SDP_DATA_SEQ8, buf);
281 SDP_PUT8(12, buf);
283 SDP_PUT8(SDP_DATA_SEQ8, buf);
284 SDP_PUT8(3, buf);
285 SDP_PUT8(SDP_DATA_UUID16, buf);
286 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
288 SDP_PUT8(SDP_DATA_SEQ8, buf);
289 SDP_PUT8(5, buf);
290 SDP_PUT8(SDP_DATA_UUID16, buf);
291 SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf);
292 SDP_PUT8(SDP_DATA_UINT8, buf);
293 SDP_PUT8(*data, buf);
295 return (14);
299 * seq8 len8 - 2 bytes
300 * seq8 len8 - 2 bytes
301 * uuid16 value16 - 3 bytes
302 * seq8 len8 - 2 bytes
303 * uuid16 value16 - 3 bytes
304 * uint8 value8 - 2 bytes
305 * seq8 len8 - 2 bytes
306 * uuid16 value16 - 3 bytes
309 int32_t
310 obex_profile_create_protocol_descriptor_list(
311 uint8_t *buf, uint8_t const * const eob,
312 uint8_t const *data, uint32_t datalen)
314 if (datalen != 1 || buf + 19 > eob)
315 return (-1);
317 SDP_PUT8(SDP_DATA_SEQ8, buf);
318 SDP_PUT8(17, buf);
320 SDP_PUT8(SDP_DATA_SEQ8, buf);
321 SDP_PUT8(3, buf);
322 SDP_PUT8(SDP_DATA_UUID16, buf);
323 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
325 SDP_PUT8(SDP_DATA_SEQ8, buf);
326 SDP_PUT8(5, buf);
327 SDP_PUT8(SDP_DATA_UUID16, buf);
328 SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf);
329 SDP_PUT8(SDP_DATA_UINT8, buf);
330 SDP_PUT8(*data, buf);
332 SDP_PUT8(SDP_DATA_SEQ8, buf);
333 SDP_PUT8(3, buf);
334 SDP_PUT8(SDP_DATA_UUID16, buf);
335 SDP_PUT16(SDP_UUID_PROTOCOL_OBEX, buf);
337 return (19);
341 * seq8 len8
342 * uint8 value8 - bytes
343 * [ uint8 value 8 ]
346 int32_t
347 obex_profile_create_supported_formats_list(
348 uint8_t *buf, uint8_t const * const eob,
349 uint8_t const *data, uint32_t datalen)
351 int32_t len = 2 * datalen;
353 if (len <= 0 || len > 0xff || buf + 2 + len > eob)
354 return (-1);
356 SDP_PUT8(SDP_DATA_SEQ8, buf);
357 SDP_PUT8(len, buf);
359 for (; datalen > 0; datalen --) {
360 SDP_PUT8(SDP_DATA_UINT8, buf);
361 SDP_PUT8(*data++, buf);
364 return (2 + len);
368 * verify server channel number (the first byte in the data)
371 int32_t
372 common_profile_server_channel_valid(uint8_t const *data, uint32_t datalen)
374 if (data[0] < 1 || data[0] > 30)
375 return (0);
377 return (1);
381 * verify server channel number and supported_formats_size
382 * sdp_opush_profile and sdp_irmc_profile
385 int32_t
386 obex_profile_data_valid(uint8_t const *data, uint32_t datalen)
388 sdp_opush_profile_t const *opush = (sdp_opush_profile_t const *) data;
390 if (opush->server_channel < 1 ||
391 opush->server_channel > 30 ||
392 opush->supported_formats_size == 0 ||
393 opush->supported_formats_size > sizeof(opush->supported_formats))
394 return (0);
396 return (1);