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 $ */
7 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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>
44 * Lookup profile descriptor
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
74 for (i
= 0; i
< sizeof(profiles
)/sizeof(profiles
[0]); i
++)
75 if (profiles
[i
]->uuid
[0] == uuid
)
82 * Look attribute in the profile descripror
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
++)
98 * uint32 value32 - 5 bytes
102 common_profile_create_service_record_handle(
103 uint8_t *buf
, uint8_t const * const eob
,
104 uint8_t const *data
, uint32_t datalen
)
109 SDP_PUT8(SDP_DATA_UINT32
, buf
);
110 SDP_PUT32(((provider_t
const *) data
)->handle
, buf
);
116 * seq8 len8 - 2 bytes
117 * uuid16 value16 - 3 bytes
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
)
131 SDP_PUT8(SDP_DATA_SEQ8
, 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);
144 * seq8 len8 - 2 bytes
145 * seq 8 len8 - 2 bytes
146 * uuid16 value16 - 3 bytes
147 * uint16 value16 - 3 bytes
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
)
163 SDP_PUT8(SDP_DATA_SEQ8
, buf
);
166 for (; datalen
> 0; datalen
--) {
167 SDP_PUT8(SDP_DATA_SEQ8
, 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);
181 * seq8 len8 - 2 bytes
182 * uint16 value16 - 3 bytes
183 * uint16 value16 - 3 bytes
184 * uint16 value16 - 3 bytes
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
)
195 SDP_PUT8(SDP_DATA_SEQ8
, 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
);
214 * Offset (Primary Language Base is 0x100)
217 SDP_PUT8(SDP_DATA_UINT16
, buf
);
218 SDP_PUT16(SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID
, buf
);
224 * Use Operating System name as provider name
228 common_profile_create_service_provider_name(
229 uint8_t *buf
, uint8_t const * const eob
,
230 uint8_t const *data
, uint32_t datalen
)
240 return (common_profile_create_string8(buf
, eob
,
241 (uint8_t const *)name
, strlen(name
)));
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
)
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
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
)
280 SDP_PUT8(SDP_DATA_SEQ8
, buf
);
283 SDP_PUT8(SDP_DATA_SEQ8
, buf
);
285 SDP_PUT8(SDP_DATA_UUID16
, buf
);
286 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP
, buf
);
288 SDP_PUT8(SDP_DATA_SEQ8
, 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
);
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
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
)
317 SDP_PUT8(SDP_DATA_SEQ8
, buf
);
320 SDP_PUT8(SDP_DATA_SEQ8
, buf
);
322 SDP_PUT8(SDP_DATA_UUID16
, buf
);
323 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP
, buf
);
325 SDP_PUT8(SDP_DATA_SEQ8
, 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
);
334 SDP_PUT8(SDP_DATA_UUID16
, buf
);
335 SDP_PUT16(SDP_UUID_PROTOCOL_OBEX
, buf
);
342 * uint8 value8 - bytes
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
)
356 SDP_PUT8(SDP_DATA_SEQ8
, buf
);
359 for (; datalen
> 0; datalen
--) {
360 SDP_PUT8(SDP_DATA_UINT8
, buf
);
361 SDP_PUT8(*data
++, buf
);
368 * verify server channel number (the first byte in the data)
372 common_profile_server_channel_valid(uint8_t const *data
, uint32_t datalen
)
374 if (data
[0] < 1 || data
[0] > 30)
381 * verify server channel number and supported_formats_size
382 * sdp_opush_profile and sdp_irmc_profile
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
))