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 audio_sink_profile_descriptor;
49 extern profile_t audio_source_profile_descriptor;
50 extern profile_t dun_profile_descriptor;
51 extern profile_t ftrn_profile_descriptor;
52 extern profile_t irmc_profile_descriptor;
53 extern profile_t irmc_command_profile_descriptor;
54 extern profile_t lan_profile_descriptor;
55 extern profile_t opush_profile_descriptor;
56 extern profile_t sp_profile_descriptor;
57 extern profile_t nap_profile_descriptor;
58 extern profile_t gn_profile_descriptor;
59 extern profile_t panu_profile_descriptor;
61 static const profile_p profiles[] = {
62 &audio_sink_profile_descriptor,
63 &audio_source_profile_descriptor,
64 &dun_profile_descriptor,
65 &ftrn_profile_descriptor,
66 &irmc_profile_descriptor,
67 &irmc_command_profile_descriptor,
68 &lan_profile_descriptor,
69 &opush_profile_descriptor,
70 &sp_profile_descriptor,
71 &nap_profile_descriptor,
72 &gn_profile_descriptor,
73 &panu_profile_descriptor
78 for (i = 0; i < sizeof(profiles)/sizeof(profiles[0]); i++)
79 if (profiles[i]->uuid == uuid)
86 * Look attribute in the profile descripror
90 profile_get_attr(const profile_p profile, uint16_t attr)
92 attr_p ad = (attr_p) profile->attrs;
94 for (; ad->create != NULL; ad ++)
102 * uint32 value32 - 5 bytes
106 common_profile_create_service_record_handle(
107 uint8_t *buf, uint8_t const * const eob,
108 uint8_t const *data, uint32_t datalen)
113 SDP_PUT8(SDP_DATA_UINT32, buf);
114 SDP_PUT32(((provider_p) data)->handle, buf);
120 * seq8 len8 - 2 bytes
121 * uuid16 value16 - 3 bytes
126 common_profile_create_service_class_id_list(
127 uint8_t *buf, uint8_t const * const eob,
128 uint8_t const *data, uint32_t datalen)
130 int32_t len = 3 * (datalen >>= 1);
132 if (len <= 0 || len > 0xff || buf + 2 + len > eob)
135 SDP_PUT8(SDP_DATA_SEQ8, buf);
138 for (; datalen > 0; datalen --) {
139 SDP_PUT8(SDP_DATA_UUID16, buf);
140 SDP_PUT16(*((uint16_t const *)data), buf);
141 data += sizeof(uint16_t);
148 * seq8 len8 - 2 bytes
149 * seq 8 len8 - 2 bytes
150 * uuid16 value16 - 3 bytes
151 * uint16 value16 - 3 bytes
158 common_profile_create_bluetooth_profile_descriptor_list(
159 uint8_t *buf, uint8_t const * const eob,
160 uint8_t const *data, uint32_t datalen)
162 int32_t len = 8 * (datalen >>= 2);
164 if (len <= 0 || len > 0xff || buf + 2 + len > eob)
167 SDP_PUT8(SDP_DATA_SEQ8, buf);
170 for (; datalen > 0; datalen --) {
171 SDP_PUT8(SDP_DATA_SEQ8, buf);
173 SDP_PUT8(SDP_DATA_UUID16, buf);
174 SDP_PUT16(*((uint16_t const *)data), buf);
175 data += sizeof(uint16_t);
176 SDP_PUT8(SDP_DATA_UINT16, buf);
177 SDP_PUT16(*((uint16_t const *)data), buf);
178 data += sizeof(uint16_t);
185 * seq8 len8 - 2 bytes
186 * uint16 value16 - 3 bytes
187 * uint16 value16 - 3 bytes
188 * uint16 value16 - 3 bytes
192 common_profile_create_language_base_attribute_id_list(
193 uint8_t *buf, uint8_t const * const eob,
194 uint8_t const *data, uint32_t datalen)
199 SDP_PUT8(SDP_DATA_SEQ8, buf);
203 * Language code per ISO 639:1988. Use "en".
206 SDP_PUT8(SDP_DATA_UINT16, buf);
207 SDP_PUT16(((0x65 << 8) | 0x6e), buf);
210 * Encoding. Recommended is UTF-8. ISO639 UTF-8 MIBenum is 106
211 * (http://www.iana.org/assignments/character-sets)
214 SDP_PUT8(SDP_DATA_UINT16, buf);
218 * Offset (Primary Language Base is 0x100)
221 SDP_PUT8(SDP_DATA_UINT16, buf);
222 SDP_PUT16(SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID, buf);
228 * Common provider name is "FreeBSD"
232 common_profile_create_service_provider_name(
233 uint8_t *buf, uint8_t const * const eob,
234 uint8_t const *data, uint32_t datalen)
236 char provider_name[] = "FreeBSD";
238 return (common_profile_create_string8(buf, eob,
239 (uint8_t const *) provider_name,
240 strlen(provider_name)));
248 common_profile_create_string8(
249 uint8_t *buf, uint8_t const * const eob,
250 uint8_t const *data, uint32_t datalen)
252 if (datalen == 0 || datalen > 0xff || buf + 2 + datalen > eob)
255 SDP_PUT8(SDP_DATA_STR8, buf);
256 SDP_PUT8(datalen, buf);
257 memcpy(buf, data, datalen);
259 return (2 + datalen);
263 * Service Availability
267 common_profile_create_service_availability(
268 uint8_t *buf, uint8_t const * const eob,
269 uint8_t const *data, uint32_t datalen)
271 if (datalen != 1 || buf + 2 > eob)
274 SDP_PUT8(SDP_DATA_UINT8, buf);
275 SDP_PUT8(data[0], buf);
281 * seq8 len8 - 2 bytes
282 * seq8 len8 - 2 bytes
283 * uuid16 value16 - 3 bytes
284 * seq8 len8 - 2 bytes
285 * uuid16 value16 - 3 bytes
286 * uint8 value8 - 2 bytes
290 rfcomm_profile_create_protocol_descriptor_list(
291 uint8_t *buf, uint8_t const * const eob,
292 uint8_t const *data, uint32_t datalen)
294 if (datalen != 1 || buf + 14 > eob)
297 SDP_PUT8(SDP_DATA_SEQ8, buf);
300 SDP_PUT8(SDP_DATA_SEQ8, buf);
302 SDP_PUT8(SDP_DATA_UUID16, buf);
303 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
305 SDP_PUT8(SDP_DATA_SEQ8, buf);
307 SDP_PUT8(SDP_DATA_UUID16, buf);
308 SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf);
309 SDP_PUT8(SDP_DATA_UINT8, buf);
310 SDP_PUT8(*data, buf);
316 * seq8 len8 - 2 bytes
317 * seq8 len8 - 2 bytes
318 * uuid16 value16 - 3 bytes
319 * seq8 len8 - 2 bytes
320 * uuid16 value16 - 3 bytes
321 * uint8 value8 - 2 bytes
322 * seq8 len8 - 2 bytes
323 * uuid16 value16 - 3 bytes
327 obex_profile_create_protocol_descriptor_list(
328 uint8_t *buf, uint8_t const * const eob,
329 uint8_t const *data, uint32_t datalen)
331 if (datalen != 1 || buf + 19 > eob)
334 SDP_PUT8(SDP_DATA_SEQ8, buf);
337 SDP_PUT8(SDP_DATA_SEQ8, buf);
339 SDP_PUT8(SDP_DATA_UUID16, buf);
340 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
342 SDP_PUT8(SDP_DATA_SEQ8, buf);
344 SDP_PUT8(SDP_DATA_UUID16, buf);
345 SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf);
346 SDP_PUT8(SDP_DATA_UINT8, buf);
347 SDP_PUT8(*data, buf);
349 SDP_PUT8(SDP_DATA_SEQ8, buf);
351 SDP_PUT8(SDP_DATA_UUID16, buf);
352 SDP_PUT16(SDP_UUID_PROTOCOL_OBEX, buf);
359 * uint8 value8 - bytes
364 obex_profile_create_supported_formats_list(
365 uint8_t *buf, uint8_t const * const eob,
366 uint8_t const *data, uint32_t datalen)
368 int32_t len = 2 * datalen;
370 if (len <= 0 || len > 0xff || buf + 2 + len > eob)
373 SDP_PUT8(SDP_DATA_SEQ8, buf);
376 for (; datalen > 0; datalen --) {
377 SDP_PUT8(SDP_DATA_UINT8, buf);
378 SDP_PUT8(*data++, buf);
385 * do not check anything
389 common_profile_always_valid(uint8_t const *data, uint32_t datalen)
395 * verify server channel number (the first byte in the data)
399 common_profile_server_channel_valid(uint8_t const *data, uint32_t datalen)
401 if (data[0] < 1 || data[0] > 30)
408 * verify server channel number and supported_formats_size
409 * sdp_opush_profile and sdp_irmc_profile
413 obex_profile_data_valid(uint8_t const *data, uint32_t datalen)
415 sdp_opush_profile_p opush = (sdp_opush_profile_p) data;
417 if (opush->server_channel < 1 ||
418 opush->server_channel > 30 ||
419 opush->supported_formats_size == 0 ||
420 opush->supported_formats_size > sizeof(opush->supported_formats))
427 * BNEP protocol descriptor
431 bnep_profile_create_protocol_descriptor_list(
432 uint8_t *buf, uint8_t const * const eob,
433 uint8_t const *data, uint32_t datalen)
435 /* supported protocol types */
444 uint16_t i, psm, version = 0x0100,
445 nptypes = sizeof(ptype)/sizeof(ptype[0]),
446 nptypes_size = nptypes * 3;
448 if (datalen != 2 || 18 + nptypes_size > 255 ||
449 buf + 20 + nptypes_size > eob)
452 memcpy(&psm, data, sizeof(psm));
454 SDP_PUT8(SDP_DATA_SEQ8, buf);
455 SDP_PUT8(18 + nptypes_size, buf);
457 SDP_PUT8(SDP_DATA_SEQ8, buf);
459 SDP_PUT8(SDP_DATA_UUID16, buf);
460 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
461 SDP_PUT8(SDP_DATA_UINT16, buf);
464 SDP_PUT8(SDP_DATA_SEQ8, buf);
465 SDP_PUT8(8 + nptypes_size, buf);
466 SDP_PUT8(SDP_DATA_UUID16, buf);
467 SDP_PUT16(SDP_UUID_PROTOCOL_BNEP, buf);
468 SDP_PUT8(SDP_DATA_UINT16, buf);
469 SDP_PUT16(version, buf);
470 SDP_PUT8(SDP_DATA_SEQ8, buf);
471 SDP_PUT8(nptypes_size, buf);
472 for (i = 0; i < nptypes; i ++) {
473 SDP_PUT8(SDP_DATA_UINT16, buf);
474 SDP_PUT16(ptype[i], buf);
477 return (20 + nptypes_size);
481 * BNEP security description
485 bnep_profile_create_security_description(
486 uint8_t *buf, uint8_t const * const eob,
487 uint8_t const *data, uint32_t datalen)
489 uint16_t security_descr;
491 if (datalen != 2 || buf + 3 > eob)
494 memcpy(&security_descr, data, sizeof(security_descr));
496 SDP_PUT8(SDP_DATA_UINT16, buf);
497 SDP_PUT16(security_descr, buf);