6 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
30 * $Id: profile.c,v 1.6 2004/01/13 19:31:54 max Exp $
34 #include <sys/queue.h>
35 #include <bluetooth.h>
42 * Lookup profile descriptor
46 profile_get_descriptor(uint16_t uuid)
48 extern profile_t dun_profile_descriptor;
49 extern profile_t ftrn_profile_descriptor;
50 extern profile_t irmc_profile_descriptor;
51 extern profile_t irmc_command_profile_descriptor;
52 extern profile_t lan_profile_descriptor;
53 extern profile_t opush_profile_descriptor;
54 extern profile_t sp_profile_descriptor;
55 extern profile_t nap_profile_descriptor;
56 extern profile_t gn_profile_descriptor;
57 extern profile_t panu_profile_descriptor;
59 static const profile_p profiles[] = {
60 &dun_profile_descriptor,
61 &ftrn_profile_descriptor,
62 &irmc_profile_descriptor,
63 &irmc_command_profile_descriptor,
64 &lan_profile_descriptor,
65 &opush_profile_descriptor,
66 &sp_profile_descriptor,
67 &nap_profile_descriptor,
68 &gn_profile_descriptor,
69 &panu_profile_descriptor
74 for (i = 0; i < sizeof(profiles)/sizeof(profiles[0]); i++)
75 if (profiles[i]->uuid == uuid)
82 * Look attribute in the profile descripror
86 profile_get_attr(const profile_p profile, uint16_t attr)
88 attr_p ad = (attr_p) 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_p) 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 * Common provider name is "FreeBSD"
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 char provider_name[] = "FreeBSD";
234 return (common_profile_create_string8(buf, eob,
235 (uint8_t const *) provider_name,
236 strlen(provider_name)));
244 common_profile_create_string8(
245 uint8_t *buf, uint8_t const * const eob,
246 uint8_t const *data, uint32_t datalen)
248 if (datalen == 0 || datalen > 0xff || buf + 2 + datalen > eob)
251 SDP_PUT8(SDP_DATA_STR8, buf);
252 SDP_PUT8(datalen, buf);
253 memcpy(buf, data, datalen);
255 return (2 + datalen);
259 * Service Availability
263 common_profile_create_service_availability(
264 uint8_t *buf, uint8_t const * const eob,
265 uint8_t const *data, uint32_t datalen)
267 if (datalen != 1 || buf + 2 > eob)
270 SDP_PUT8(SDP_DATA_UINT8, buf);
271 SDP_PUT8(data[0], buf);
277 * seq8 len8 - 2 bytes
278 * seq8 len8 - 2 bytes
279 * uuid16 value16 - 3 bytes
280 * seq8 len8 - 2 bytes
281 * uuid16 value16 - 3 bytes
282 * uint8 value8 - 2 bytes
286 rfcomm_profile_create_protocol_descriptor_list(
287 uint8_t *buf, uint8_t const * const eob,
288 uint8_t const *data, uint32_t datalen)
290 if (datalen != 1 || buf + 14 > eob)
293 SDP_PUT8(SDP_DATA_SEQ8, buf);
296 SDP_PUT8(SDP_DATA_SEQ8, buf);
298 SDP_PUT8(SDP_DATA_UUID16, buf);
299 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
301 SDP_PUT8(SDP_DATA_SEQ8, buf);
303 SDP_PUT8(SDP_DATA_UUID16, buf);
304 SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf);
305 SDP_PUT8(SDP_DATA_UINT8, buf);
306 SDP_PUT8(*data, buf);
312 * seq8 len8 - 2 bytes
313 * seq8 len8 - 2 bytes
314 * uuid16 value16 - 3 bytes
315 * seq8 len8 - 2 bytes
316 * uuid16 value16 - 3 bytes
317 * uint8 value8 - 2 bytes
318 * seq8 len8 - 2 bytes
319 * uuid16 value16 - 3 bytes
323 obex_profile_create_protocol_descriptor_list(
324 uint8_t *buf, uint8_t const * const eob,
325 uint8_t const *data, uint32_t datalen)
327 if (datalen != 1 || buf + 19 > eob)
330 SDP_PUT8(SDP_DATA_SEQ8, buf);
333 SDP_PUT8(SDP_DATA_SEQ8, buf);
335 SDP_PUT8(SDP_DATA_UUID16, buf);
336 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
338 SDP_PUT8(SDP_DATA_SEQ8, buf);
340 SDP_PUT8(SDP_DATA_UUID16, buf);
341 SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf);
342 SDP_PUT8(SDP_DATA_UINT8, buf);
343 SDP_PUT8(*data, buf);
345 SDP_PUT8(SDP_DATA_SEQ8, buf);
347 SDP_PUT8(SDP_DATA_UUID16, buf);
348 SDP_PUT16(SDP_UUID_PROTOCOL_OBEX, buf);
355 * uint8 value8 - bytes
360 obex_profile_create_supported_formats_list(
361 uint8_t *buf, uint8_t const * const eob,
362 uint8_t const *data, uint32_t datalen)
364 int32_t len = 2 * datalen;
366 if (len <= 0 || len > 0xff || buf + 2 + len > eob)
369 SDP_PUT8(SDP_DATA_SEQ8, buf);
372 for (; datalen > 0; datalen --) {
373 SDP_PUT8(SDP_DATA_UINT8, buf);
374 SDP_PUT8(*data++, buf);
381 * do not check anything
385 common_profile_always_valid(uint8_t const *data, uint32_t datalen)
391 * verify server channel number (the first byte in the data)
395 common_profile_server_channel_valid(uint8_t const *data, uint32_t datalen)
397 if (data[0] < 1 || data[0] > 30)
404 * verify server channel number and supported_formats_size
405 * sdp_opush_profile and sdp_irmc_profile
409 obex_profile_data_valid(uint8_t const *data, uint32_t datalen)
411 sdp_opush_profile_p opush = (sdp_opush_profile_p) data;
413 if (opush->server_channel < 1 ||
414 opush->server_channel > 30 ||
415 opush->supported_formats_size == 0 ||
416 opush->supported_formats_size > sizeof(opush->supported_formats))
423 * BNEP protocol descriptor
427 bnep_profile_create_protocol_descriptor_list(
428 uint8_t *buf, uint8_t const * const eob,
429 uint8_t const *data, uint32_t datalen)
431 /* supported protocol types */
440 uint16_t i, psm, version = 0x0100,
441 nptypes = sizeof(ptype)/sizeof(ptype[0]),
442 nptypes_size = nptypes * 3;
444 if (datalen != 2 || 18 + nptypes_size > 255 ||
445 buf + 20 + nptypes_size > eob)
448 memcpy(&psm, data, sizeof(psm));
450 SDP_PUT8(SDP_DATA_SEQ8, buf);
451 SDP_PUT8(18 + nptypes_size, buf);
453 SDP_PUT8(SDP_DATA_SEQ8, buf);
455 SDP_PUT8(SDP_DATA_UUID16, buf);
456 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
457 SDP_PUT8(SDP_DATA_UINT16, buf);
460 SDP_PUT8(SDP_DATA_SEQ8, buf);
461 SDP_PUT8(8 + nptypes_size, buf);
462 SDP_PUT8(SDP_DATA_UUID16, buf);
463 SDP_PUT16(SDP_UUID_PROTOCOL_BNEP, buf);
464 SDP_PUT8(SDP_DATA_UINT16, buf);
465 SDP_PUT16(version, buf);
466 SDP_PUT8(SDP_DATA_SEQ8, buf);
467 SDP_PUT8(nptypes_size, buf);
468 for (i = 0; i < nptypes; i ++) {
469 SDP_PUT8(SDP_DATA_UINT16, buf);
470 SDP_PUT16(ptype[i], buf);
473 return (20 + nptypes_size);
477 * BNEP security description
481 bnep_profile_create_security_description(
482 uint8_t *buf, uint8_t const * const eob,
483 uint8_t const *data, uint32_t datalen)
485 uint16_t security_descr;
487 if (datalen != 2 || buf + 3 > eob)
490 memcpy(&security_descr, data, sizeof(security_descr));
492 SDP_PUT8(SDP_DATA_UINT16, buf);
493 SDP_PUT16(security_descr, buf);