6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
8 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: profile.c,v 1.6 2004/01/13 19:31:54 max Exp $
36 #include <sys/queue.h>
37 #define L2CAP_SOCKET_CHECKED
38 #include <bluetooth.h>
45 * Lookup profile descriptor
49 profile_get_descriptor(uint16_t uuid)
51 extern profile_t audio_sink_profile_descriptor;
52 extern profile_t audio_source_profile_descriptor;
53 extern profile_t dun_profile_descriptor;
54 extern profile_t ftrn_profile_descriptor;
55 extern profile_t irmc_profile_descriptor;
56 extern profile_t irmc_command_profile_descriptor;
57 extern profile_t lan_profile_descriptor;
58 extern profile_t opush_profile_descriptor;
59 extern profile_t sp_profile_descriptor;
60 extern profile_t nap_profile_descriptor;
61 extern profile_t gn_profile_descriptor;
62 extern profile_t panu_profile_descriptor;
64 static const profile_p profiles[] = {
65 &audio_sink_profile_descriptor,
66 &audio_source_profile_descriptor,
67 &dun_profile_descriptor,
68 &ftrn_profile_descriptor,
69 &irmc_profile_descriptor,
70 &irmc_command_profile_descriptor,
71 &lan_profile_descriptor,
72 &opush_profile_descriptor,
73 &sp_profile_descriptor,
74 &nap_profile_descriptor,
75 &gn_profile_descriptor,
76 &panu_profile_descriptor
81 for (i = 0; i < sizeof(profiles)/sizeof(profiles[0]); i++)
82 if (profiles[i]->uuid == uuid)
89 * Look attribute in the profile descripror
93 profile_get_attr(const profile_p profile, uint16_t attr)
95 attr_p ad = (attr_p) profile->attrs;
97 for (; ad->create != NULL; ad ++)
105 * uint32 value32 - 5 bytes
109 common_profile_create_service_record_handle(
110 uint8_t *buf, uint8_t const * const eob,
111 uint8_t const *data, uint32_t datalen)
116 SDP_PUT8(SDP_DATA_UINT32, buf);
117 SDP_PUT32(((provider_p) data)->handle, buf);
123 * seq8 len8 - 2 bytes
124 * uuid16 value16 - 3 bytes
129 common_profile_create_service_class_id_list(
130 uint8_t *buf, uint8_t const * const eob,
131 uint8_t const *data, uint32_t datalen)
133 int32_t len = 3 * (datalen >>= 1);
135 if (len <= 0 || len > 0xff || buf + 2 + len > eob)
138 SDP_PUT8(SDP_DATA_SEQ8, buf);
141 for (; datalen > 0; datalen --) {
142 SDP_PUT8(SDP_DATA_UUID16, buf);
143 SDP_PUT16(*((uint16_t const *)data), buf);
144 data += sizeof(uint16_t);
151 * seq8 len8 - 2 bytes
152 * seq 8 len8 - 2 bytes
153 * uuid16 value16 - 3 bytes
154 * uint16 value16 - 3 bytes
161 common_profile_create_bluetooth_profile_descriptor_list(
162 uint8_t *buf, uint8_t const * const eob,
163 uint8_t const *data, uint32_t datalen)
165 int32_t len = 8 * (datalen >>= 2);
167 if (len <= 0 || len > 0xff || buf + 2 + len > eob)
170 SDP_PUT8(SDP_DATA_SEQ8, buf);
173 for (; datalen > 0; datalen --) {
174 SDP_PUT8(SDP_DATA_SEQ8, buf);
176 SDP_PUT8(SDP_DATA_UUID16, buf);
177 SDP_PUT16(*((uint16_t const *)data), buf);
178 data += sizeof(uint16_t);
179 SDP_PUT8(SDP_DATA_UINT16, buf);
180 SDP_PUT16(*((uint16_t const *)data), buf);
181 data += sizeof(uint16_t);
188 * seq8 len8 - 2 bytes
189 * uint16 value16 - 3 bytes
190 * uint16 value16 - 3 bytes
191 * uint16 value16 - 3 bytes
195 common_profile_create_language_base_attribute_id_list(
196 uint8_t *buf, uint8_t const * const eob,
197 uint8_t const *data, uint32_t datalen)
202 SDP_PUT8(SDP_DATA_SEQ8, buf);
206 * Language code per ISO 639:1988. Use "en".
209 SDP_PUT8(SDP_DATA_UINT16, buf);
210 SDP_PUT16(((0x65 << 8) | 0x6e), buf);
213 * Encoding. Recommended is UTF-8. ISO639 UTF-8 MIBenum is 106
214 * (http://www.iana.org/assignments/character-sets)
217 SDP_PUT8(SDP_DATA_UINT16, buf);
221 * Offset (Primary Language Base is 0x100)
224 SDP_PUT8(SDP_DATA_UINT16, buf);
225 SDP_PUT16(SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID, buf);
231 * Common provider name is "FreeBSD"
235 common_profile_create_service_provider_name(
236 uint8_t *buf, uint8_t const * const eob,
237 uint8_t const *data, uint32_t datalen)
239 char provider_name[] = "FreeBSD";
241 return (common_profile_create_string8(buf, eob,
242 (uint8_t const *) provider_name,
243 strlen(provider_name)));
251 common_profile_create_string8(
252 uint8_t *buf, uint8_t const * const eob,
253 uint8_t const *data, uint32_t datalen)
255 if (datalen == 0 || datalen > 0xff || buf + 2 + datalen > eob)
258 SDP_PUT8(SDP_DATA_STR8, buf);
259 SDP_PUT8(datalen, buf);
260 memcpy(buf, data, datalen);
262 return (2 + datalen);
266 * Service Availability
270 common_profile_create_service_availability(
271 uint8_t *buf, uint8_t const * const eob,
272 uint8_t const *data, uint32_t datalen)
274 if (datalen != 1 || buf + 2 > eob)
277 SDP_PUT8(SDP_DATA_UINT8, buf);
278 SDP_PUT8(data[0], buf);
284 * seq8 len8 - 2 bytes
285 * seq8 len8 - 2 bytes
286 * uuid16 value16 - 3 bytes
287 * seq8 len8 - 2 bytes
288 * uuid16 value16 - 3 bytes
289 * uint8 value8 - 2 bytes
293 rfcomm_profile_create_protocol_descriptor_list(
294 uint8_t *buf, uint8_t const * const eob,
295 uint8_t const *data, uint32_t datalen)
297 if (datalen != 1 || buf + 14 > eob)
300 SDP_PUT8(SDP_DATA_SEQ8, buf);
303 SDP_PUT8(SDP_DATA_SEQ8, buf);
305 SDP_PUT8(SDP_DATA_UUID16, buf);
306 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
308 SDP_PUT8(SDP_DATA_SEQ8, buf);
310 SDP_PUT8(SDP_DATA_UUID16, buf);
311 SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf);
312 SDP_PUT8(SDP_DATA_UINT8, buf);
313 SDP_PUT8(*data, buf);
319 * seq8 len8 - 2 bytes
320 * seq8 len8 - 2 bytes
321 * uuid16 value16 - 3 bytes
322 * seq8 len8 - 2 bytes
323 * uuid16 value16 - 3 bytes
324 * uint8 value8 - 2 bytes
325 * seq8 len8 - 2 bytes
326 * uuid16 value16 - 3 bytes
330 obex_profile_create_protocol_descriptor_list(
331 uint8_t *buf, uint8_t const * const eob,
332 uint8_t const *data, uint32_t datalen)
334 if (datalen != 1 || buf + 19 > eob)
337 SDP_PUT8(SDP_DATA_SEQ8, buf);
340 SDP_PUT8(SDP_DATA_SEQ8, buf);
342 SDP_PUT8(SDP_DATA_UUID16, buf);
343 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
345 SDP_PUT8(SDP_DATA_SEQ8, buf);
347 SDP_PUT8(SDP_DATA_UUID16, buf);
348 SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf);
349 SDP_PUT8(SDP_DATA_UINT8, buf);
350 SDP_PUT8(*data, buf);
352 SDP_PUT8(SDP_DATA_SEQ8, buf);
354 SDP_PUT8(SDP_DATA_UUID16, buf);
355 SDP_PUT16(SDP_UUID_PROTOCOL_OBEX, buf);
362 * uint8 value8 - bytes
367 obex_profile_create_supported_formats_list(
368 uint8_t *buf, uint8_t const * const eob,
369 uint8_t const *data, uint32_t datalen)
371 int32_t len = 2 * datalen;
373 if (len <= 0 || len > 0xff || buf + 2 + len > eob)
376 SDP_PUT8(SDP_DATA_SEQ8, buf);
379 for (; datalen > 0; datalen --) {
380 SDP_PUT8(SDP_DATA_UINT8, buf);
381 SDP_PUT8(*data++, buf);
388 * do not check anything
392 common_profile_always_valid(uint8_t const *data, uint32_t datalen)
398 * verify server channel number (the first byte in the data)
402 common_profile_server_channel_valid(uint8_t const *data, uint32_t datalen)
404 if (data[0] < 1 || data[0] > 30)
411 * verify server channel number and supported_formats_size
412 * sdp_opush_profile and sdp_irmc_profile
416 obex_profile_data_valid(uint8_t const *data, uint32_t datalen)
418 sdp_opush_profile_p opush = (sdp_opush_profile_p) data;
420 if (opush->server_channel < 1 ||
421 opush->server_channel > 30 ||
422 opush->supported_formats_size == 0 ||
423 opush->supported_formats_size > sizeof(opush->supported_formats))
430 * BNEP protocol descriptor
434 bnep_profile_create_protocol_descriptor_list(
435 uint8_t *buf, uint8_t const * const eob,
436 uint8_t const *data, uint32_t datalen)
438 /* supported protocol types */
447 uint16_t i, psm, version = 0x0100,
448 nptypes = sizeof(ptype)/sizeof(ptype[0]),
449 nptypes_size = nptypes * 3;
451 if (datalen != 2 || 18 + nptypes_size > 255 ||
452 buf + 20 + nptypes_size > eob)
455 memcpy(&psm, data, sizeof(psm));
457 SDP_PUT8(SDP_DATA_SEQ8, buf);
458 SDP_PUT8(18 + nptypes_size, buf);
460 SDP_PUT8(SDP_DATA_SEQ8, buf);
462 SDP_PUT8(SDP_DATA_UUID16, buf);
463 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
464 SDP_PUT8(SDP_DATA_UINT16, buf);
467 SDP_PUT8(SDP_DATA_SEQ8, buf);
468 SDP_PUT8(8 + nptypes_size, buf);
469 SDP_PUT8(SDP_DATA_UUID16, buf);
470 SDP_PUT16(SDP_UUID_PROTOCOL_BNEP, buf);
471 SDP_PUT8(SDP_DATA_UINT16, buf);
472 SDP_PUT16(version, buf);
473 SDP_PUT8(SDP_DATA_SEQ8, buf);
474 SDP_PUT8(nptypes_size, buf);
475 for (i = 0; i < nptypes; i ++) {
476 SDP_PUT8(SDP_DATA_UINT16, buf);
477 SDP_PUT16(ptype[i], buf);
480 return (20 + nptypes_size);
484 * BNEP security description
488 bnep_profile_create_security_description(
489 uint8_t *buf, uint8_t const * const eob,
490 uint8_t const *data, uint32_t datalen)
492 uint16_t security_descr;
494 if (datalen != 2 || buf + 3 > eob)
497 memcpy(&security_descr, data, sizeof(security_descr));
499 SDP_PUT8(SDP_DATA_UINT16, buf);
500 SDP_PUT16(security_descr, buf);