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 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
43 * Lookup profile descriptor
47 profile_get_descriptor(uint16_t uuid)
49 extern profile_t dun_profile_descriptor;
50 extern profile_t ftrn_profile_descriptor;
51 extern profile_t irmc_profile_descriptor;
52 extern profile_t irmc_command_profile_descriptor;
53 extern profile_t lan_profile_descriptor;
54 extern profile_t opush_profile_descriptor;
55 extern profile_t sp_profile_descriptor;
56 extern profile_t nap_profile_descriptor;
57 extern profile_t gn_profile_descriptor;
58 extern profile_t panu_profile_descriptor;
60 static const profile_p profiles[] = {
61 &dun_profile_descriptor,
62 &ftrn_profile_descriptor,
63 &irmc_profile_descriptor,
64 &irmc_command_profile_descriptor,
65 &lan_profile_descriptor,
66 &opush_profile_descriptor,
67 &sp_profile_descriptor,
68 &nap_profile_descriptor,
69 &gn_profile_descriptor,
70 &panu_profile_descriptor
75 for (i = 0; i < sizeof(profiles)/sizeof(profiles[0]); i++)
76 if (profiles[i]->uuid == uuid)
83 * Look attribute in the profile descripror
87 profile_get_attr(const profile_p profile, uint16_t attr)
89 attr_p ad = (attr_p) profile->attrs;
91 for (; ad->create != NULL; ad ++)
99 * uint32 value32 - 5 bytes
103 common_profile_create_service_record_handle(
104 uint8_t *buf, uint8_t const * const eob,
105 uint8_t const *data, uint32_t datalen)
110 SDP_PUT8(SDP_DATA_UINT32, buf);
111 SDP_PUT32(((provider_p) data)->handle, buf);
117 * seq8 len8 - 2 bytes
118 * uuid16 value16 - 3 bytes
123 common_profile_create_service_class_id_list(
124 uint8_t *buf, uint8_t const * const eob,
125 uint8_t const *data, uint32_t datalen)
127 int32_t len = 3 * (datalen >>= 1);
129 if (len <= 0 || len > 0xff || buf + 2 + len > eob)
132 SDP_PUT8(SDP_DATA_SEQ8, buf);
135 for (; datalen > 0; datalen --) {
136 SDP_PUT8(SDP_DATA_UUID16, buf);
137 SDP_PUT16(*((uint16_t const *)data), buf);
138 data += sizeof(uint16_t);
145 * seq8 len8 - 2 bytes
146 * seq 8 len8 - 2 bytes
147 * uuid16 value16 - 3 bytes
148 * uint16 value16 - 3 bytes
155 common_profile_create_bluetooth_profile_descriptor_list(
156 uint8_t *buf, uint8_t const * const eob,
157 uint8_t const *data, uint32_t datalen)
159 int32_t len = 8 * (datalen >>= 2);
161 if (len <= 0 || len > 0xff || buf + 2 + len > eob)
164 SDP_PUT8(SDP_DATA_SEQ8, buf);
167 for (; datalen > 0; datalen --) {
168 SDP_PUT8(SDP_DATA_SEQ8, buf);
170 SDP_PUT8(SDP_DATA_UUID16, buf);
171 SDP_PUT16(*((uint16_t const *)data), buf);
172 data += sizeof(uint16_t);
173 SDP_PUT8(SDP_DATA_UINT16, buf);
174 SDP_PUT16(*((uint16_t const *)data), buf);
175 data += sizeof(uint16_t);
182 * seq8 len8 - 2 bytes
183 * uint16 value16 - 3 bytes
184 * uint16 value16 - 3 bytes
185 * uint16 value16 - 3 bytes
189 common_profile_create_language_base_attribute_id_list(
190 uint8_t *buf, uint8_t const * const eob,
191 uint8_t const *data, uint32_t datalen)
196 SDP_PUT8(SDP_DATA_SEQ8, buf);
200 * Language code per ISO 639:1988. Use "en".
203 SDP_PUT8(SDP_DATA_UINT16, buf);
204 SDP_PUT16(((0x65 << 8) | 0x6e), buf);
207 * Encoding. Recommended is UTF-8. ISO639 UTF-8 MIBenum is 106
208 * (http://www.iana.org/assignments/character-sets)
211 SDP_PUT8(SDP_DATA_UINT16, buf);
215 * Offset (Primary Language Base is 0x100)
218 SDP_PUT8(SDP_DATA_UINT16, buf);
219 SDP_PUT16(SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID, buf);
225 * Common provider name is "FreeBSD"
229 common_profile_create_service_provider_name(
230 uint8_t *buf, uint8_t const * const eob,
231 uint8_t const *data, uint32_t datalen)
233 char provider_name[] = "FreeBSD";
235 return (common_profile_create_string8(buf, eob,
236 (uint8_t const *) provider_name,
237 strlen(provider_name)));
245 common_profile_create_string8(
246 uint8_t *buf, uint8_t const * const eob,
247 uint8_t const *data, uint32_t datalen)
249 if (datalen == 0 || datalen > 0xff || buf + 2 + datalen > eob)
252 SDP_PUT8(SDP_DATA_STR8, buf);
253 SDP_PUT8(datalen, buf);
254 memcpy(buf, data, datalen);
256 return (2 + datalen);
260 * Service Availability
264 common_profile_create_service_availability(
265 uint8_t *buf, uint8_t const * const eob,
266 uint8_t const *data, uint32_t datalen)
268 if (datalen != 1 || buf + 2 > eob)
271 SDP_PUT8(SDP_DATA_UINT8, buf);
272 SDP_PUT8(data[0], buf);
278 * seq8 len8 - 2 bytes
279 * seq8 len8 - 2 bytes
280 * uuid16 value16 - 3 bytes
281 * seq8 len8 - 2 bytes
282 * uuid16 value16 - 3 bytes
283 * uint8 value8 - 2 bytes
287 rfcomm_profile_create_protocol_descriptor_list(
288 uint8_t *buf, uint8_t const * const eob,
289 uint8_t const *data, uint32_t datalen)
291 if (datalen != 1 || buf + 14 > eob)
294 SDP_PUT8(SDP_DATA_SEQ8, buf);
297 SDP_PUT8(SDP_DATA_SEQ8, buf);
299 SDP_PUT8(SDP_DATA_UUID16, buf);
300 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
302 SDP_PUT8(SDP_DATA_SEQ8, buf);
304 SDP_PUT8(SDP_DATA_UUID16, buf);
305 SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf);
306 SDP_PUT8(SDP_DATA_UINT8, buf);
307 SDP_PUT8(*data, buf);
313 * seq8 len8 - 2 bytes
314 * seq8 len8 - 2 bytes
315 * uuid16 value16 - 3 bytes
316 * seq8 len8 - 2 bytes
317 * uuid16 value16 - 3 bytes
318 * uint8 value8 - 2 bytes
319 * seq8 len8 - 2 bytes
320 * uuid16 value16 - 3 bytes
324 obex_profile_create_protocol_descriptor_list(
325 uint8_t *buf, uint8_t const * const eob,
326 uint8_t const *data, uint32_t datalen)
328 if (datalen != 1 || buf + 19 > eob)
331 SDP_PUT8(SDP_DATA_SEQ8, buf);
334 SDP_PUT8(SDP_DATA_SEQ8, buf);
336 SDP_PUT8(SDP_DATA_UUID16, buf);
337 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
339 SDP_PUT8(SDP_DATA_SEQ8, buf);
341 SDP_PUT8(SDP_DATA_UUID16, buf);
342 SDP_PUT16(SDP_UUID_PROTOCOL_RFCOMM, buf);
343 SDP_PUT8(SDP_DATA_UINT8, buf);
344 SDP_PUT8(*data, buf);
346 SDP_PUT8(SDP_DATA_SEQ8, buf);
348 SDP_PUT8(SDP_DATA_UUID16, buf);
349 SDP_PUT16(SDP_UUID_PROTOCOL_OBEX, buf);
356 * uint8 value8 - bytes
361 obex_profile_create_supported_formats_list(
362 uint8_t *buf, uint8_t const * const eob,
363 uint8_t const *data, uint32_t datalen)
365 int32_t len = 2 * datalen;
367 if (len <= 0 || len > 0xff || buf + 2 + len > eob)
370 SDP_PUT8(SDP_DATA_SEQ8, buf);
373 for (; datalen > 0; datalen --) {
374 SDP_PUT8(SDP_DATA_UINT8, buf);
375 SDP_PUT8(*data++, buf);
382 * do not check anything
386 common_profile_always_valid(uint8_t const *data, uint32_t datalen)
392 * verify server channel number (the first byte in the data)
396 common_profile_server_channel_valid(uint8_t const *data, uint32_t datalen)
398 if (data[0] < 1 || data[0] > 30)
405 * verify server channel number and supported_formats_size
406 * sdp_opush_profile and sdp_irmc_profile
410 obex_profile_data_valid(uint8_t const *data, uint32_t datalen)
412 sdp_opush_profile_p opush = (sdp_opush_profile_p) data;
414 if (opush->server_channel < 1 ||
415 opush->server_channel > 30 ||
416 opush->supported_formats_size == 0 ||
417 opush->supported_formats_size > sizeof(opush->supported_formats))
424 * BNEP protocol descriptor
428 bnep_profile_create_protocol_descriptor_list(
429 uint8_t *buf, uint8_t const * const eob,
430 uint8_t const *data, uint32_t datalen)
432 /* supported protocol types */
441 uint16_t i, psm, version = 0x0100,
442 nptypes = sizeof(ptype)/sizeof(ptype[0]),
443 nptypes_size = nptypes * 3;
445 if (datalen != 2 || 18 + nptypes_size > 255 ||
446 buf + 20 + nptypes_size > eob)
449 memcpy(&psm, data, sizeof(psm));
451 SDP_PUT8(SDP_DATA_SEQ8, buf);
452 SDP_PUT8(18 + nptypes_size, buf);
454 SDP_PUT8(SDP_DATA_SEQ8, buf);
456 SDP_PUT8(SDP_DATA_UUID16, buf);
457 SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
458 SDP_PUT8(SDP_DATA_UINT16, buf);
461 SDP_PUT8(SDP_DATA_SEQ8, buf);
462 SDP_PUT8(8 + nptypes_size, buf);
463 SDP_PUT8(SDP_DATA_UUID16, buf);
464 SDP_PUT16(SDP_UUID_PROTOCOL_BNEP, buf);
465 SDP_PUT8(SDP_DATA_UINT16, buf);
466 SDP_PUT16(version, buf);
467 SDP_PUT8(SDP_DATA_SEQ8, buf);
468 SDP_PUT8(nptypes_size, buf);
469 for (i = 0; i < nptypes; i ++) {
470 SDP_PUT8(SDP_DATA_UINT16, buf);
471 SDP_PUT16(ptype[i], buf);
474 return (20 + nptypes_size);
478 * BNEP security description
482 bnep_profile_create_security_description(
483 uint8_t *buf, uint8_t const * const eob,
484 uint8_t const *data, uint32_t datalen)
486 uint16_t security_descr;
488 if (datalen != 2 || buf + 3 > eob)
491 memcpy(&security_descr, data, sizeof(security_descr));
493 SDP_PUT8(SDP_DATA_UINT16, buf);
494 SDP_PUT16(security_descr, buf);