3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2001-2002 Nokia Corporation
6 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
7 * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
8 * Copyright (C) 2002-2003 Stephen Crane <steve.crane@rococosoft.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
40 #include <sys/types.h>
41 #include <sys/socket.h>
43 #include <netinet/in.h>
45 #include "bluetooth.h"
52 #define SDPINF(fmt, arg...) syslog(LOG_INFO, fmt "\n", ## arg)
53 #define SDPERR(fmt, arg...) syslog(LOG_ERR, "%s: " fmt "\n", __func__ , ## arg)
55 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
58 #define SDPDBG(fmt, arg...) syslog(LOG_DEBUG, "%s: " fmt "\n", __func__ , ## arg)
60 #define SDPDBG(fmt...)
63 static uint128_t bluetooth_base_uuid = {
64 .data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
65 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }
68 #define SDP_MAX_ATTR_LEN 65535
70 /* match MTU used by RFCOMM */
71 #define SDP_LARGE_L2CAP_MTU 1013
73 static sdp_data_t *sdp_copy_seq(sdp_data_t *data);
74 static int sdp_attr_add_new_with_length(sdp_record_t *rec,
75 uint16_t attr, uint8_t dtd, const void *value, uint32_t len);
76 static int sdp_gen_buffer(sdp_buf_t *buf, sdp_data_t *d);
78 /* Message structure. */
84 static struct tupla Protocol[] = {
87 { RFCOMM_UUID, "RFCOMM" },
89 { TCS_BIN_UUID, "TCS-BIN" },
90 { TCS_AT_UUID, "TCS-AT" },
91 { OBEX_UUID, "OBEX" },
94 { HTTP_UUID, "HTTP" },
96 { BNEP_UUID, "BNEP" },
97 { UPNP_UUID, "UPNP" },
98 { HIDP_UUID, "HIDP" },
99 { HCRP_CTRL_UUID, "HCRP-Ctrl" },
100 { HCRP_DATA_UUID, "HCRP-Data" },
101 { HCRP_NOTE_UUID, "HCRP-Notify" },
102 { AVCTP_UUID, "AVCTP" },
103 { AVDTP_UUID, "AVDTP" },
104 { CMTP_UUID, "CMTP" },
106 { MCAP_CTRL_UUID, "MCAP-Ctrl" },
107 { MCAP_DATA_UUID, "MCAP-Data" },
108 { L2CAP_UUID, "L2CAP" },
113 static struct tupla ServiceClass[] = {
114 { SDP_SERVER_SVCLASS_ID, "SDP Server" },
115 { BROWSE_GRP_DESC_SVCLASS_ID, "Browse Group Descriptor" },
116 { PUBLIC_BROWSE_GROUP, "Public Browse Group" },
117 { SERIAL_PORT_SVCLASS_ID, "Serial Port" },
118 { LAN_ACCESS_SVCLASS_ID, "LAN Access Using PPP" },
119 { DIALUP_NET_SVCLASS_ID, "Dialup Networking" },
120 { IRMC_SYNC_SVCLASS_ID, "IrMC Sync" },
121 { OBEX_OBJPUSH_SVCLASS_ID, "OBEX Object Push" },
122 { OBEX_FILETRANS_SVCLASS_ID, "OBEX File Transfer" },
123 { IRMC_SYNC_CMD_SVCLASS_ID, "IrMC Sync Command" },
124 { HEADSET_SVCLASS_ID, "Headset" },
125 { CORDLESS_TELEPHONY_SVCLASS_ID, "Cordless Telephony" },
126 { AUDIO_SOURCE_SVCLASS_ID, "Audio Source" },
127 { AUDIO_SINK_SVCLASS_ID, "Audio Sink" },
128 { AV_REMOTE_TARGET_SVCLASS_ID, "AV Remote Target" },
129 { ADVANCED_AUDIO_SVCLASS_ID, "Advanced Audio" },
130 { AV_REMOTE_SVCLASS_ID, "AV Remote" },
131 { AV_REMOTE_CONTROLLER_SVCLASS_ID, "AV Remote Controller" },
132 { INTERCOM_SVCLASS_ID, "Intercom" },
133 { FAX_SVCLASS_ID, "Fax" },
134 { HEADSET_AGW_SVCLASS_ID, "Headset Audio Gateway" },
135 { WAP_SVCLASS_ID, "WAP" },
136 { WAP_CLIENT_SVCLASS_ID, "WAP Client" },
137 { PANU_SVCLASS_ID, "PAN User" },
138 { NAP_SVCLASS_ID, "Network Access Point" },
139 { GN_SVCLASS_ID, "PAN Group Network" },
140 { DIRECT_PRINTING_SVCLASS_ID, "Direct Printing" },
141 { REFERENCE_PRINTING_SVCLASS_ID, "Reference Printing" },
142 { IMAGING_SVCLASS_ID, "Imaging" },
143 { IMAGING_RESPONDER_SVCLASS_ID, "Imaging Responder" },
144 { IMAGING_ARCHIVE_SVCLASS_ID, "Imaging Automatic Archive" },
145 { IMAGING_REFOBJS_SVCLASS_ID, "Imaging Referenced Objects" },
146 { HANDSFREE_SVCLASS_ID, "Handsfree" },
147 { HANDSFREE_AGW_SVCLASS_ID, "Handsfree Audio Gateway" },
148 { DIRECT_PRT_REFOBJS_SVCLASS_ID, "Direct Printing Ref. Objects" },
149 { REFLECTED_UI_SVCLASS_ID, "Reflected UI" },
150 { BASIC_PRINTING_SVCLASS_ID, "Basic Printing" },
151 { PRINTING_STATUS_SVCLASS_ID, "Printing Status" },
152 { HID_SVCLASS_ID, "Human Interface Device" },
153 { HCR_SVCLASS_ID, "Hardcopy Cable Replacement" },
154 { HCR_PRINT_SVCLASS_ID, "HCR Print" },
155 { HCR_SCAN_SVCLASS_ID, "HCR Scan" },
156 { CIP_SVCLASS_ID, "Common ISDN Access" },
157 { VIDEO_CONF_GW_SVCLASS_ID, "Video Conferencing Gateway" },
158 { UDI_MT_SVCLASS_ID, "UDI MT" },
159 { UDI_TA_SVCLASS_ID, "UDI TA" },
160 { AV_SVCLASS_ID, "Audio/Video" },
161 { SAP_SVCLASS_ID, "SIM Access" },
162 { PBAP_PCE_SVCLASS_ID, "Phonebook Access - PCE" },
163 { PBAP_PSE_SVCLASS_ID, "Phonebook Access - PSE" },
164 { PBAP_SVCLASS_ID, "Phonebook Access" },
165 { MAP_MSE_SVCLASS_ID, "Message Access - MAS" },
166 { MAP_MCE_SVCLASS_ID, "Message Access - MNS" },
167 { MAP_SVCLASS_ID, "Message Access" },
168 { PNP_INFO_SVCLASS_ID, "PnP Information" },
169 { GENERIC_NETWORKING_SVCLASS_ID, "Generic Networking" },
170 { GENERIC_FILETRANS_SVCLASS_ID, "Generic File Transfer" },
171 { GENERIC_AUDIO_SVCLASS_ID, "Generic Audio" },
172 { GENERIC_TELEPHONY_SVCLASS_ID, "Generic Telephony" },
173 { UPNP_SVCLASS_ID, "UPnP" },
174 { UPNP_IP_SVCLASS_ID, "UPnP IP" },
175 { UPNP_PAN_SVCLASS_ID, "UPnP PAN" },
176 { UPNP_LAP_SVCLASS_ID, "UPnP LAP" },
177 { UPNP_L2CAP_SVCLASS_ID, "UPnP L2CAP" },
178 { VIDEO_SOURCE_SVCLASS_ID, "Video Source" },
179 { VIDEO_SINK_SVCLASS_ID, "Video Sink" },
180 { VIDEO_DISTRIBUTION_SVCLASS_ID, "Video Distribution" },
181 { HDP_SVCLASS_ID, "HDP" },
182 { HDP_SOURCE_SVCLASS_ID, "HDP Source" },
183 { HDP_SINK_SVCLASS_ID, "HDP Sink" },
184 { GENERIC_ACCESS_SVCLASS_ID, "Generic Access" },
185 { GENERIC_ATTRIB_SVCLASS_ID, "Generic Attribute" },
186 { APPLE_AGENT_SVCLASS_ID, "Apple Agent" },
190 #define Profile ServiceClass
192 static char *string_lookup(struct tupla *pt0, int index)
196 for (pt = pt0; pt->index; pt++)
197 if (pt->index == index)
203 static char *string_lookup_uuid(struct tupla *pt0, const uuid_t *uuid)
207 memcpy(&tmp_uuid, uuid, sizeof(tmp_uuid));
209 if (sdp_uuid128_to_uuid(&tmp_uuid)) {
210 switch (tmp_uuid.type) {
212 return string_lookup(pt0, tmp_uuid.value.uuid16);
214 return string_lookup(pt0, tmp_uuid.value.uuid32);
222 * Prints into a string the Protocol UUID
223 * coping a maximum of n characters.
225 static int uuid2str(struct tupla *message, const uuid_t *uuid, char *str, size_t n)
230 snprintf(str, n, "NULL");
234 switch (uuid->type) {
236 str2 = string_lookup(message, uuid->value.uuid16);
237 snprintf(str, n, "%s", str2);
240 str2 = string_lookup(message, uuid->value.uuid32);
241 snprintf(str, n, "%s", str2);
244 str2 = string_lookup_uuid(message, uuid);
245 snprintf(str, n, "%s", str2);
248 snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type);
255 int sdp_proto_uuid2strn(const uuid_t *uuid, char *str, size_t n)
257 return uuid2str(Protocol, uuid, str, n);
260 int sdp_svclass_uuid2strn(const uuid_t *uuid, char *str, size_t n)
262 return uuid2str(ServiceClass, uuid, str, n);
265 int sdp_profile_uuid2strn(const uuid_t *uuid, char *str, size_t n)
267 return uuid2str(Profile, uuid, str, n);
271 * convert the UUID to string, copying a maximum of n characters.
273 int sdp_uuid2strn(const uuid_t *uuid, char *str, size_t n)
276 snprintf(str, n, "NULL");
279 switch (uuid->type) {
281 snprintf(str, n, "%.4x", uuid->value.uuid16);
284 snprintf(str, n, "%.8x", uuid->value.uuid32);
288 unsigned short data1;
289 unsigned short data2;
290 unsigned short data3;
292 unsigned short data5;
294 memcpy(&data0, &uuid->value.uuid128.data[0], 4);
295 memcpy(&data1, &uuid->value.uuid128.data[4], 2);
296 memcpy(&data2, &uuid->value.uuid128.data[6], 2);
297 memcpy(&data3, &uuid->value.uuid128.data[8], 2);
298 memcpy(&data4, &uuid->value.uuid128.data[10], 4);
299 memcpy(&data5, &uuid->value.uuid128.data[14], 2);
301 snprintf(str, n, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
302 ntohl(data0), ntohs(data1),
303 ntohs(data2), ntohs(data3),
304 ntohl(data4), ntohs(data5));
308 snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type);
309 return -1; /* Enum type of UUID not set */
316 * Function prints the UUID in hex as per defined syntax -
318 * 4bytes-2bytes-2bytes-2bytes-6bytes
320 * There is some ugly code, including hardcoding, but
321 * that is just the way it is converting 16 and 32 bit
322 * UUIDs to 128 bit as defined in the SDP doc
324 void sdp_uuid_print(const uuid_t *uuid)
327 SDPERR("Null passed to print UUID");
330 if (uuid->type == SDP_UUID16) {
331 SDPDBG(" uint16_t : 0x%.4x", uuid->value.uuid16);
332 } else if (uuid->type == SDP_UUID32) {
333 SDPDBG(" uint32_t : 0x%.8x", uuid->value.uuid32);
334 } else if (uuid->type == SDP_UUID128) {
336 unsigned short data1;
337 unsigned short data2;
338 unsigned short data3;
340 unsigned short data5;
342 memcpy(&data0, &uuid->value.uuid128.data[0], 4);
343 memcpy(&data1, &uuid->value.uuid128.data[4], 2);
344 memcpy(&data2, &uuid->value.uuid128.data[6], 2);
345 memcpy(&data3, &uuid->value.uuid128.data[8], 2);
346 memcpy(&data4, &uuid->value.uuid128.data[10], 4);
347 memcpy(&data5, &uuid->value.uuid128.data[14], 2);
349 SDPDBG(" uint128_t : 0x%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
350 ntohl(data0), ntohs(data1), ntohs(data2),
351 ntohs(data3), ntohl(data4), ntohs(data5));
353 SDPERR("Enum type of UUID not set");
357 sdp_data_t *sdp_data_alloc_with_length(uint8_t dtd, const void *value,
361 sdp_data_t *d = malloc(sizeof(sdp_data_t));
366 memset(d, 0, sizeof(sdp_data_t));
368 d->unitSize = sizeof(uint8_t);
374 d->val.uint8 = *(uint8_t *) value;
375 d->unitSize += sizeof(uint8_t);
379 d->val.int8 = *(int8_t *) value;
380 d->unitSize += sizeof(int8_t);
383 d->val.uint16 = bt_get_unaligned((uint16_t *) value);
384 d->unitSize += sizeof(uint16_t);
387 d->val.int16 = bt_get_unaligned((int16_t *) value);
388 d->unitSize += sizeof(int16_t);
391 d->val.uint32 = bt_get_unaligned((uint32_t *) value);
392 d->unitSize += sizeof(uint32_t);
395 d->val.int32 = bt_get_unaligned((int32_t *) value);
396 d->unitSize += sizeof(int32_t);
399 d->val.int64 = bt_get_unaligned((int64_t *) value);
400 d->unitSize += sizeof(int64_t);
403 d->val.uint64 = bt_get_unaligned((uint64_t *) value);
404 d->unitSize += sizeof(uint64_t);
407 memcpy(&d->val.uint128.data, value, sizeof(uint128_t));
408 d->unitSize += sizeof(uint128_t);
411 memcpy(&d->val.int128.data, value, sizeof(uint128_t));
412 d->unitSize += sizeof(uint128_t);
415 sdp_uuid16_create(&d->val.uuid, bt_get_unaligned((uint16_t *) value));
416 d->unitSize += sizeof(uint16_t);
419 sdp_uuid32_create(&d->val.uuid, bt_get_unaligned((uint32_t *) value));
420 d->unitSize += sizeof(uint32_t);
423 sdp_uuid128_create(&d->val.uuid, value);
424 d->unitSize += sizeof(uint128_t);
435 d->unitSize += length;
436 if (length <= USHRT_MAX) {
437 d->val.str = malloc(length);
443 memcpy(d->val.str, value, length);
445 SDPERR("Strings of size > USHRT_MAX not supported");
452 SDPERR("Strings of size > USHRT_MAX not supported");
460 if (dtd == SDP_ALT8 || dtd == SDP_SEQ8)
461 d->unitSize += sizeof(uint8_t);
462 else if (dtd == SDP_ALT16 || dtd == SDP_SEQ16)
463 d->unitSize += sizeof(uint16_t);
464 else if (dtd == SDP_ALT32 || dtd == SDP_SEQ32)
465 d->unitSize += sizeof(uint32_t);
466 seq = (sdp_data_t *)value;
467 d->val.dataseq = seq;
468 for (; seq; seq = seq->next)
469 d->unitSize += seq->unitSize;
479 sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value)
491 length = strlen((char *) value);
498 return sdp_data_alloc_with_length(dtd, value, length);
501 sdp_data_t *sdp_seq_append(sdp_data_t *seq, sdp_data_t *d)
505 for (p = seq; p->next; p = p->next);
513 sdp_data_t *sdp_seq_alloc_with_length(void **dtds, void **values, int *length,
516 sdp_data_t *curr = NULL, *seq = NULL;
519 for (i = 0; i < len; i++) {
521 int8_t dtd = *(uint8_t *) dtds[i];
523 if (dtd >= SDP_SEQ8 && dtd <= SDP_ALT32)
524 data = (sdp_data_t *) values[i];
526 data = sdp_data_alloc_with_length(dtd, values[i], length[i]);
539 return sdp_data_alloc(SDP_SEQ8, seq);
542 sdp_data_t *sdp_seq_alloc(void **dtds, void **values, int len)
544 sdp_data_t *curr = NULL, *seq = NULL;
547 for (i = 0; i < len; i++) {
549 uint8_t dtd = *(uint8_t *) dtds[i];
551 if (dtd >= SDP_SEQ8 && dtd <= SDP_ALT32)
552 data = (sdp_data_t *) values[i];
554 data = sdp_data_alloc(dtd, values[i]);
567 return sdp_data_alloc(SDP_SEQ8, seq);
570 static void extract_svclass_uuid(sdp_data_t *data, uuid_t *uuid)
574 if (!data || !SDP_IS_SEQ(data->dtd))
577 d = data->val.dataseq;
581 if (d->dtd < SDP_UUID16 || d->dtd > SDP_UUID128)
587 int sdp_attr_add(sdp_record_t *rec, uint16_t attr, sdp_data_t *d)
589 sdp_data_t *p = sdp_data_get(rec, attr);
595 rec->attrlist = sdp_list_insert_sorted(rec->attrlist, d, sdp_attrid_comp_func);
597 if (attr == SDP_ATTR_SVCLASS_ID_LIST)
598 extract_svclass_uuid(d, &rec->svclass);
603 void sdp_attr_remove(sdp_record_t *rec, uint16_t attr)
605 sdp_data_t *d = sdp_data_get(rec, attr);
608 rec->attrlist = sdp_list_remove(rec->attrlist, d);
610 if (attr == SDP_ATTR_SVCLASS_ID_LIST)
611 memset(&rec->svclass, 0, sizeof(rec->svclass));
614 void sdp_set_seq_len(uint8_t *ptr, uint32_t length)
616 uint8_t dtd = *ptr++;
623 *ptr = (uint8_t) length;
629 bt_put_be16(length, ptr);
635 bt_put_be32(length, ptr);
640 static int sdp_get_data_type_size(uint8_t dtd)
642 int size = sizeof(uint8_t);
649 size += sizeof(uint8_t);
655 size += sizeof(uint16_t);
661 size += sizeof(uint32_t);
668 void sdp_set_attrid(sdp_buf_t *buf, uint16_t attr)
670 uint8_t *p = buf->data;
672 /* data type for attr */
674 buf->data_size = sizeof(uint8_t);
675 bt_put_be16(attr, p);
676 buf->data_size += sizeof(uint16_t);
679 static int get_data_size(sdp_buf_t *buf, sdp_data_t *sdpdata)
684 for (d = sdpdata->val.dataseq; d; d = d->next) {
686 n += sdp_gen_pdu(buf, d);
688 n += sdp_gen_buffer(buf, d);
694 static int sdp_get_data_size(sdp_buf_t *buf, sdp_data_t *d)
696 uint32_t data_size = 0;
697 uint8_t dtd = d->dtd;
703 data_size = sizeof(uint8_t);
706 data_size = sizeof(uint16_t);
709 data_size = sizeof(uint32_t);
712 data_size = sizeof(uint64_t);
715 data_size = sizeof(uint128_t);
719 data_size = sizeof(int8_t);
722 data_size = sizeof(int16_t);
725 data_size = sizeof(int32_t);
728 data_size = sizeof(int64_t);
731 data_size = sizeof(uint128_t);
739 data_size = d->unitSize - sizeof(uint8_t);
744 data_size = get_data_size(buf, d);
749 data_size = get_data_size(buf, d);
752 data_size = sizeof(uint16_t);
755 data_size = sizeof(uint32_t);
758 data_size = sizeof(uint128_t);
767 static int sdp_gen_buffer(sdp_buf_t *buf, sdp_data_t *d)
769 int orig = buf->buf_size;
771 if (buf->buf_size == 0 && d->dtd == 0) {
772 /* create initial sequence */
773 buf->buf_size += sizeof(uint8_t);
775 /* reserve space for sequence size */
776 buf->buf_size += sizeof(uint8_t);
779 /* attribute length */
780 buf->buf_size += sizeof(uint8_t) + sizeof(uint16_t);
782 buf->buf_size += sdp_get_data_type_size(d->dtd);
783 buf->buf_size += sdp_get_data_size(buf, d);
785 if (buf->buf_size > UCHAR_MAX && d->dtd == SDP_SEQ8)
786 buf->buf_size += sizeof(uint8_t);
788 return buf->buf_size - orig;
791 int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d)
793 uint32_t pdu_size, data_size;
794 unsigned char *src = NULL, is_seq = 0, is_alt = 0;
799 uint8_t *seqp = buf->data + buf->data_size;
800 uint32_t orig_data_size = buf->data_size;
803 pdu_size = sdp_get_data_type_size(d->dtd);
804 buf->data_size += pdu_size;
806 data_size = sdp_get_data_size(buf, d);
807 if (data_size > UCHAR_MAX && d->dtd == SDP_SEQ8) {
808 buf->data_size = orig_data_size;
822 u16 = htons(d->val.uint16);
823 src = (unsigned char *) &u16;
826 u32 = htonl(d->val.uint32);
827 src = (unsigned char *) &u32;
830 u64 = hton64(d->val.uint64);
831 src = (unsigned char *) &u64;
834 hton128(&d->val.uint128, &u128);
835 src = (unsigned char *) &u128;
839 src = (unsigned char *) &d->val.int8;
842 u16 = htons(d->val.int16);
843 src = (unsigned char *) &u16;
846 u32 = htonl(d->val.int32);
847 src = (unsigned char *) &u32;
850 u64 = hton64(d->val.int64);
851 src = (unsigned char *) &u64;
854 hton128(&d->val.int128, &u128);
855 src = (unsigned char *) &u128;
863 src = (unsigned char *) d->val.str;
864 sdp_set_seq_len(seqp, data_size);
870 sdp_set_seq_len(seqp, data_size);
876 sdp_set_seq_len(seqp, data_size);
879 u16 = htons(d->val.uuid.value.uuid16);
880 src = (unsigned char *) &u16;
883 u32 = htonl(d->val.uuid.value.uuid32);
884 src = (unsigned char *) &u32;
887 src = (unsigned char *) &d->val.uuid.value.uuid128;
893 if (!is_seq && !is_alt) {
894 if (src && buf->buf_size >= buf->data_size + data_size) {
895 memcpy(buf->data + buf->data_size, src, data_size);
896 buf->data_size += data_size;
897 } else if (d->dtd != SDP_DATA_NIL) {
898 SDPDBG("Gen PDU : Can't copy from invalid source or dest");
902 pdu_size += data_size;
907 static void sdp_attr_pdu(void *value, void *udata)
909 sdp_append_to_pdu((sdp_buf_t *)udata, (sdp_data_t *)value);
912 static void sdp_attr_size(void *value, void *udata)
914 sdp_gen_buffer((sdp_buf_t *)udata, (sdp_data_t *)value);
917 int sdp_gen_record_pdu(const sdp_record_t *rec, sdp_buf_t *buf)
919 memset(buf, 0, sizeof(sdp_buf_t));
920 sdp_list_foreach(rec->attrlist, sdp_attr_size, buf);
922 buf->data = malloc(buf->buf_size);
926 memset(buf->data, 0, buf->buf_size);
928 sdp_list_foreach(rec->attrlist, sdp_attr_pdu, buf);
933 void sdp_attr_replace(sdp_record_t *rec, uint16_t attr, sdp_data_t *d)
940 p = sdp_data_get(rec, attr);
942 rec->attrlist = sdp_list_remove(rec->attrlist, p);
947 rec->attrlist = sdp_list_insert_sorted(rec->attrlist, d, sdp_attrid_comp_func);
949 if (attr == SDP_ATTR_SVCLASS_ID_LIST)
950 extract_svclass_uuid(d, &rec->svclass);
953 int sdp_attrid_comp_func(const void *key1, const void *key2)
955 const sdp_data_t *d1 = (const sdp_data_t *)key1;
956 const sdp_data_t *d2 = (const sdp_data_t *)key2;
959 return d1->attrId - d2->attrId;
963 static void data_seq_free(sdp_data_t *seq)
965 sdp_data_t *d = seq->val.dataseq;
968 sdp_data_t *next = d->next;
974 void sdp_data_free(sdp_data_t *d)
994 int sdp_uuid_extract(const uint8_t *p, int bufsize, uuid_t *uuid, int *scanned)
998 if (bufsize < (int) sizeof(uint8_t)) {
999 SDPERR("Unexpected end of packet");
1003 type = *(const uint8_t *) p;
1005 if (!SDP_IS_UUID(type)) {
1006 SDPERR("Unknown data type : %d expecting a svc UUID", type);
1009 p += sizeof(uint8_t);
1010 *scanned += sizeof(uint8_t);
1011 bufsize -= sizeof(uint8_t);
1012 if (type == SDP_UUID16) {
1013 if (bufsize < (int) sizeof(uint16_t)) {
1014 SDPERR("Not enough room for 16-bit UUID");
1017 sdp_uuid16_create(uuid, bt_get_be16(p));
1018 *scanned += sizeof(uint16_t);
1019 } else if (type == SDP_UUID32) {
1020 if (bufsize < (int) sizeof(uint32_t)) {
1021 SDPERR("Not enough room for 32-bit UUID");
1024 sdp_uuid32_create(uuid, bt_get_be32(p));
1025 *scanned += sizeof(uint32_t);
1027 if (bufsize < (int) sizeof(uint128_t)) {
1028 SDPERR("Not enough room for 128-bit UUID");
1031 sdp_uuid128_create(uuid, p);
1032 *scanned += sizeof(uint128_t);
1037 static sdp_data_t *extract_int(const void *p, int bufsize, int *len)
1041 if (bufsize < (int) sizeof(uint8_t)) {
1042 SDPERR("Unexpected end of packet");
1046 d = malloc(sizeof(sdp_data_t));
1050 SDPDBG("Extracting integer");
1051 memset(d, 0, sizeof(sdp_data_t));
1052 d->dtd = *(uint8_t *) p;
1053 p += sizeof(uint8_t);
1054 *len += sizeof(uint8_t);
1055 bufsize -= sizeof(uint8_t);
1063 if (bufsize < (int) sizeof(uint8_t)) {
1064 SDPERR("Unexpected end of packet");
1068 *len += sizeof(uint8_t);
1069 d->val.uint8 = *(uint8_t *) p;
1073 if (bufsize < (int) sizeof(uint16_t)) {
1074 SDPERR("Unexpected end of packet");
1078 *len += sizeof(uint16_t);
1079 d->val.uint16 = bt_get_be16(p);
1083 if (bufsize < (int) sizeof(uint32_t)) {
1084 SDPERR("Unexpected end of packet");
1088 *len += sizeof(uint32_t);
1089 d->val.uint32 = bt_get_be32(p);
1093 if (bufsize < (int) sizeof(uint64_t)) {
1094 SDPERR("Unexpected end of packet");
1098 *len += sizeof(uint64_t);
1099 d->val.uint64 = bt_get_be64(p);
1103 if (bufsize < (int) sizeof(uint128_t)) {
1104 SDPERR("Unexpected end of packet");
1108 *len += sizeof(uint128_t);
1109 ntoh128((uint128_t *) p, &d->val.uint128);
1118 static sdp_data_t *extract_uuid(const uint8_t *p, int bufsize, int *len,
1121 sdp_data_t *d = malloc(sizeof(sdp_data_t));
1126 SDPDBG("Extracting UUID");
1127 memset(d, 0, sizeof(sdp_data_t));
1128 if (sdp_uuid_extract(p, bufsize, &d->val.uuid, len) < 0) {
1134 sdp_pattern_add_uuid(rec, &d->val.uuid);
1139 * Extract strings from the PDU (could be service description and similar info)
1141 static sdp_data_t *extract_str(const void *p, int bufsize, int *len)
1147 if (bufsize < (int) sizeof(uint8_t)) {
1148 SDPERR("Unexpected end of packet");
1152 d = malloc(sizeof(sdp_data_t));
1156 memset(d, 0, sizeof(sdp_data_t));
1157 d->dtd = *(uint8_t *) p;
1158 p += sizeof(uint8_t);
1159 *len += sizeof(uint8_t);
1160 bufsize -= sizeof(uint8_t);
1165 if (bufsize < (int) sizeof(uint8_t)) {
1166 SDPERR("Unexpected end of packet");
1171 p += sizeof(uint8_t);
1172 *len += sizeof(uint8_t);
1173 bufsize -= sizeof(uint8_t);
1175 case SDP_TEXT_STR16:
1177 if (bufsize < (int) sizeof(uint16_t)) {
1178 SDPERR("Unexpected end of packet");
1183 p += sizeof(uint16_t);
1184 *len += sizeof(uint16_t);
1185 bufsize -= sizeof(uint16_t);
1188 SDPERR("Sizeof text string > UINT16_MAX");
1194 SDPERR("String too long to fit in packet");
1201 SDPERR("Not enough memory for incoming string");
1205 memset(s, 0, n + 1);
1210 SDPDBG("Len : %d", n);
1211 SDPDBG("Str : %s", s);
1214 d->unitSize = n + sizeof(uint8_t);
1219 * Extract the sequence type and its length, and return offset into buf
1222 int sdp_extract_seqtype(const uint8_t *buf, int bufsize, uint8_t *dtdp, int *size)
1225 int scanned = sizeof(uint8_t);
1227 if (bufsize < (int) sizeof(uint8_t)) {
1228 SDPERR("Unexpected end of packet");
1232 dtd = *(uint8_t *) buf;
1233 buf += sizeof(uint8_t);
1234 bufsize -= sizeof(uint8_t);
1239 if (bufsize < (int) sizeof(uint8_t)) {
1240 SDPERR("Unexpected end of packet");
1243 *size = *(uint8_t *) buf;
1244 scanned += sizeof(uint8_t);
1248 if (bufsize < (int) sizeof(uint16_t)) {
1249 SDPERR("Unexpected end of packet");
1252 *size = bt_get_be16(buf);
1253 scanned += sizeof(uint16_t);
1257 if (bufsize < (int) sizeof(uint32_t)) {
1258 SDPERR("Unexpected end of packet");
1261 *size = bt_get_be32(buf);
1262 scanned += sizeof(uint32_t);
1265 SDPERR("Unknown sequence type, aborting");
1271 static sdp_data_t *extract_seq(const void *p, int bufsize, int *len,
1275 sdp_data_t *curr, *prev;
1276 sdp_data_t *d = malloc(sizeof(sdp_data_t));
1281 SDPDBG("Extracting SEQ");
1282 memset(d, 0, sizeof(sdp_data_t));
1283 *len = sdp_extract_seqtype(p, bufsize, &d->dtd, &seqlen);
1284 SDPDBG("Sequence Type : 0x%x length : 0x%x", d->dtd, seqlen);
1289 if (*len > bufsize) {
1290 SDPERR("Packet not big enough to hold sequence.");
1298 while (n < seqlen) {
1300 curr = sdp_extract_attr(p, bufsize, &attrlen, rec);
1307 d->val.dataseq = curr;
1313 SDPDBG("Extracted: %d SequenceLength: %d", n, seqlen);
1320 sdp_data_t *sdp_extract_attr(const uint8_t *p, int bufsize, int *size,
1327 if (bufsize < (int) sizeof(uint8_t)) {
1328 SDPERR("Unexpected end of packet");
1332 dtd = *(const uint8_t *)p;
1334 SDPDBG("extract_attr: dtd=0x%x", dtd);
1348 elem = extract_int(p, bufsize, &n);
1353 elem = extract_uuid(p, bufsize, &n, rec);
1356 case SDP_TEXT_STR16:
1357 case SDP_TEXT_STR32:
1361 elem = extract_str(p, bufsize, &n);
1369 elem = extract_seq(p, bufsize, &n, rec);
1372 SDPERR("Unknown data descriptor : 0x%x terminating", dtd);
1380 static void attr_print_func(void *value, void *userData)
1382 sdp_data_t *d = (sdp_data_t *)value;
1384 SDPDBG("=====================================");
1385 SDPDBG("ATTRIBUTE IDENTIFIER : 0x%x", d->attrId);
1386 SDPDBG("ATTRIBUTE VALUE PTR : %p", value);
1390 SDPDBG("NULL value");
1391 SDPDBG("=====================================");
1394 void sdp_print_service_attr(sdp_list_t *svcAttrList)
1396 SDPDBG("Printing service attr list %p", svcAttrList);
1397 sdp_list_foreach(svcAttrList, attr_print_func, NULL);
1398 SDPDBG("Printed service attr list %p", svcAttrList);
1402 sdp_record_t *sdp_extract_pdu(const uint8_t *buf, int bufsize, int *scanned)
1404 int extracted = 0, seqlen = 0;
1407 sdp_record_t *rec = sdp_record_alloc();
1408 const uint8_t *p = buf;
1410 *scanned = sdp_extract_seqtype(buf, bufsize, &dtd, &seqlen);
1412 bufsize -= *scanned;
1413 rec->attrlist = NULL;
1415 while (extracted < seqlen && bufsize > 0) {
1416 int n = sizeof(uint8_t), attrlen = 0;
1417 sdp_data_t *data = NULL;
1419 SDPDBG("Extract PDU, sequenceLength: %d localExtractedLength: %d",
1422 if (bufsize < n + (int) sizeof(uint16_t)) {
1423 SDPERR("Unexpected end of packet");
1427 dtd = *(uint8_t *) p;
1428 attr = bt_get_be16(p + n);
1429 n += sizeof(uint16_t);
1431 SDPDBG("DTD of attrId : %d Attr id : 0x%x ", dtd, attr);
1433 data = sdp_extract_attr(p + n, bufsize - n, &attrlen, rec);
1435 SDPDBG("Attr id : 0x%x attrValueLength : %d", attr, attrlen);
1439 SDPDBG("Terminating extraction of attributes");
1443 if (attr == SDP_ATTR_RECORD_HANDLE)
1444 rec->handle = data->val.uint32;
1446 if (attr == SDP_ATTR_SVCLASS_ID_LIST)
1447 extract_svclass_uuid(data, &rec->svclass);
1452 sdp_attr_replace(rec, attr, data);
1454 SDPDBG("Extract PDU, seqLength: %d localExtractedLength: %d",
1458 SDPDBG("Successful extracting of Svc Rec attributes");
1459 sdp_print_service_attr(rec->attrlist);
1465 static void sdp_copy_pattern(void *value, void *udata)
1467 uuid_t *uuid = value;
1468 sdp_record_t *rec = udata;
1470 sdp_pattern_add_uuid(rec, uuid);
1473 static void *sdp_data_value(sdp_data_t *data, uint32_t *len)
1477 switch (data->dtd) {
1481 val = &data->val.uint8;
1485 val = &data->val.int8;
1488 val = &data->val.uint16;
1491 val = &data->val.int16;
1494 val = &data->val.uint32;
1497 val = &data->val.int32;
1500 val = &data->val.int64;
1503 val = &data->val.uint64;
1506 val = &data->val.uint128;
1509 val = &data->val.int128;
1512 val = &data->val.uuid.value.uuid16;
1515 val = &data->val.uuid.value.uuid32;
1518 val = &data->val.uuid.value.uuid128;
1523 case SDP_TEXT_STR16:
1525 case SDP_TEXT_STR32:
1526 val = data->val.str;
1528 *len = data->unitSize - 1;
1536 val = sdp_copy_seq(data->val.dataseq);
1543 static sdp_data_t *sdp_copy_seq(sdp_data_t *data)
1545 sdp_data_t *tmp, *seq = NULL, *cur = NULL;
1547 for (tmp = data; tmp; tmp = tmp->next) {
1548 sdp_data_t *datatmp;
1551 value = sdp_data_value(tmp, NULL);
1552 datatmp = sdp_data_alloc_with_length(tmp->dtd, value,
1556 cur->next = datatmp;
1566 static void sdp_copy_attrlist(void *value, void *udata)
1568 sdp_data_t *data = value;
1569 sdp_record_t *rec = udata;
1573 val = sdp_data_value(data, &len);
1576 sdp_attr_add_new(rec, data->attrId, data->dtd, val);
1578 sdp_attr_add_new_with_length(rec, data->attrId,
1579 data->dtd, val, len);
1582 sdp_record_t *sdp_copy_record(sdp_record_t *rec)
1586 cpy = sdp_record_alloc();
1588 cpy->handle = rec->handle;
1590 sdp_list_foreach(rec->pattern, sdp_copy_pattern, cpy);
1591 sdp_list_foreach(rec->attrlist, sdp_copy_attrlist, cpy);
1593 cpy->svclass = rec->svclass;
1599 static void print_dataseq(sdp_data_t *p)
1603 for (d = p; d; d = d->next)
1608 void sdp_record_print(const sdp_record_t *rec)
1610 sdp_data_t *d = sdp_data_get(rec, SDP_ATTR_SVCNAME_PRIMARY);
1611 if (d && SDP_IS_TEXT_STR(d->dtd))
1612 printf("Service Name: %.*s\n", d->unitSize, d->val.str);
1613 d = sdp_data_get(rec, SDP_ATTR_SVCDESC_PRIMARY);
1614 if (d && SDP_IS_TEXT_STR(d->dtd))
1615 printf("Service Description: %.*s\n", d->unitSize, d->val.str);
1616 d = sdp_data_get(rec, SDP_ATTR_PROVNAME_PRIMARY);
1617 if (d && SDP_IS_TEXT_STR(d->dtd))
1618 printf("Service Provider: %.*s\n", d->unitSize, d->val.str);
1622 void sdp_data_print(sdp_data_t *d)
1639 SDPDBG("Integer : 0x%x", d->val.uint32);
1645 sdp_uuid_print(&d->val.uuid);
1648 case SDP_TEXT_STR16:
1649 case SDP_TEXT_STR32:
1650 SDPDBG("Text : %s", d->val.str);
1655 SDPDBG("URL : %s", d->val.str);
1660 print_dataseq(d->val.dataseq);
1665 SDPDBG("Data Sequence Alternates");
1666 print_dataseq(d->val.dataseq);
1672 sdp_data_t *sdp_data_get(const sdp_record_t *rec, uint16_t attrId)
1674 if (rec && rec->attrlist) {
1675 sdp_data_t sdpTemplate;
1678 sdpTemplate.attrId = attrId;
1679 p = sdp_list_find(rec->attrlist, &sdpTemplate, sdp_attrid_comp_func);
1686 static int sdp_send_req(sdp_session_t *session, uint8_t *buf, uint32_t size)
1690 while (sent < size) {
1691 int n = send(session->sock, buf + sent, size - sent, 0);
1699 static int sdp_read_rsp(sdp_session_t *session, uint8_t *buf, uint32_t size)
1702 struct timeval timeout = { SDP_RESPONSE_TIMEOUT, 0 };
1705 FD_SET(session->sock, &readFds);
1706 SDPDBG("Waiting for response");
1707 if (select(session->sock + 1, &readFds, NULL, NULL, &timeout) == 0) {
1708 SDPERR("Client timed out");
1712 return recv(session->sock, buf, size, 0);
1716 * generic send request, wait for response method.
1718 int sdp_send_req_w4_rsp(sdp_session_t *session, uint8_t *reqbuf,
1719 uint8_t *rspbuf, uint32_t reqsize, uint32_t *rspsize)
1722 sdp_pdu_hdr_t *reqhdr = (sdp_pdu_hdr_t *) reqbuf;
1723 sdp_pdu_hdr_t *rsphdr = (sdp_pdu_hdr_t *) rspbuf;
1726 if (0 > sdp_send_req(session, reqbuf, reqsize)) {
1727 SDPERR("Error sending data:%m");
1730 n = sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE);
1733 SDPDBG("Read : %d", n);
1734 if (n == 0 || reqhdr->tid != rsphdr->tid) {
1743 * singly-linked lists (after openobex implementation)
1745 sdp_list_t *sdp_list_append(sdp_list_t *p, void *d)
1747 sdp_list_t *q, *n = malloc(sizeof(sdp_list_t));
1758 for (q = p; q->next; q = q->next);
1764 sdp_list_t *sdp_list_remove(sdp_list_t *list, void *d)
1768 for (q = 0, p = list; p; q = p, p = p->next)
1781 sdp_list_t *sdp_list_insert_sorted(sdp_list_t *list, void *d,
1784 sdp_list_t *q, *p, *n;
1786 n = malloc(sizeof(sdp_list_t));
1790 for (q = 0, p = list; p; q = p, p = p->next)
1791 if (f(p->data, d) >= 0)
1793 /* insert between q and p; if !q insert at head */
1803 * Every element of the list points to things which need
1804 * to be free()'d. This method frees the list's contents
1806 void sdp_list_free(sdp_list_t *list, sdp_free_func_t f)
1818 static inline int __find_port(sdp_data_t *seq, int proto)
1820 if (!seq || !seq->next)
1823 if (SDP_IS_UUID(seq->dtd) && sdp_uuid_to_proto(&seq->val.uuid) == proto) {
1827 return seq->val.uint8;
1829 return seq->val.uint16;
1835 int sdp_get_proto_port(const sdp_list_t *list, int proto)
1837 if (proto != L2CAP_UUID && proto != RFCOMM_UUID) {
1842 for (; list; list = list->next) {
1844 for (p = list->data; p; p = p->next) {
1845 sdp_data_t *seq = p->data;
1846 int port = __find_port(seq, proto);
1854 sdp_data_t *sdp_get_proto_desc(sdp_list_t *list, int proto)
1856 for (; list; list = list->next) {
1858 for (p = list->data; p; p = p->next) {
1859 sdp_data_t *seq = p->data;
1860 if (SDP_IS_UUID(seq->dtd) &&
1861 sdp_uuid_to_proto(&seq->val.uuid) == proto)
1868 static int sdp_get_proto_descs(uint16_t attr_id, const sdp_record_t *rec,
1871 sdp_data_t *pdlist, *curr;
1872 sdp_list_t *ap = NULL;
1874 pdlist = sdp_data_get(rec, attr_id);
1875 if (pdlist == NULL) {
1880 SDPDBG("Attribute value type: 0x%02x", pdlist->dtd);
1882 if (attr_id == SDP_ATTR_ADD_PROTO_DESC_LIST) {
1883 if (!SDP_IS_SEQ(pdlist->dtd)) {
1887 pdlist = pdlist->val.dataseq;
1890 for (; pdlist; pdlist = pdlist->next) {
1891 sdp_list_t *pds = NULL;
1893 if (!SDP_IS_SEQ(pdlist->dtd) && !SDP_IS_ALT(pdlist->dtd))
1896 for (curr = pdlist->val.dataseq; curr; curr = curr->next) {
1897 if (!SDP_IS_SEQ(curr->dtd)) {
1898 sdp_list_free(pds, NULL);
1901 pds = sdp_list_append(pds, curr->val.dataseq);
1904 ap = sdp_list_append(ap, pds);
1912 sdp_list_foreach(ap, (sdp_list_func_t) sdp_list_free, NULL);
1913 sdp_list_free(ap, NULL);
1919 int sdp_get_access_protos(const sdp_record_t *rec, sdp_list_t **pap)
1921 return sdp_get_proto_descs(SDP_ATTR_PROTO_DESC_LIST, rec, pap);
1924 int sdp_get_add_access_protos(const sdp_record_t *rec, sdp_list_t **pap)
1926 return sdp_get_proto_descs(SDP_ATTR_ADD_PROTO_DESC_LIST, rec, pap);
1929 int sdp_get_uuidseq_attr(const sdp_record_t *rec, uint16_t attr,
1932 sdp_data_t *sdpdata = sdp_data_get(rec, attr);
1935 if (sdpdata && SDP_IS_SEQ(sdpdata->dtd)) {
1937 for (d = sdpdata->val.dataseq; d; d = d->next) {
1939 if (d->dtd < SDP_UUID16 || d->dtd > SDP_UUID128) {
1944 u = malloc(sizeof(uuid_t));
1949 *seqp = sdp_list_append(*seqp, u);
1954 sdp_list_free(*seqp, free);
1959 int sdp_set_uuidseq_attr(sdp_record_t *rec, uint16_t aid, sdp_list_t *seq)
1961 int status = 0, i, len;
1962 void **dtds, **values;
1963 uint8_t uuid16 = SDP_UUID16;
1964 uint8_t uuid32 = SDP_UUID32;
1965 uint8_t uuid128 = SDP_UUID128;
1968 len = sdp_list_len(seq);
1969 if (!seq || len == 0)
1971 dtds = malloc(len * sizeof(void *));
1975 values = malloc(len * sizeof(void *));
1981 for (p = seq, i = 0; i < len; i++, p = p->next) {
1982 uuid_t *uuid = p->data;
1984 switch (uuid->type) {
1987 values[i] = &uuid->value.uuid16;
1991 values[i] = &uuid->value.uuid32;
1995 values[i] = &uuid->value.uuid128;
2007 sdp_data_t *data = sdp_seq_alloc(dtds, values, len);
2008 sdp_attr_replace(rec, aid, data);
2009 sdp_pattern_add_uuidseq(rec, seq);
2016 int sdp_get_lang_attr(const sdp_record_t *rec, sdp_list_t **langSeq)
2018 sdp_lang_attr_t *lang;
2019 sdp_data_t *sdpdata, *curr_data;
2022 sdpdata = sdp_data_get(rec, SDP_ATTR_LANG_BASE_ATTR_ID_LIST);
2023 if (sdpdata == NULL) {
2028 if (!SDP_IS_SEQ(sdpdata->dtd))
2030 curr_data = sdpdata->val.dataseq;
2033 sdp_data_t *pCode, *pEncoding, *pOffset;
2036 if (pCode->dtd != SDP_UINT16)
2039 /* LanguageBaseAttributeIDList entries are always grouped as
2041 if (!pCode->next || !pCode->next->next)
2044 pEncoding = pCode->next;
2045 if (pEncoding->dtd != SDP_UINT16)
2048 pOffset = pEncoding->next;
2049 if (pOffset->dtd != SDP_UINT16)
2052 lang = malloc(sizeof(sdp_lang_attr_t));
2054 sdp_list_free(*langSeq, free);
2058 lang->code_ISO639 = pCode->val.uint16;
2059 lang->encoding = pEncoding->val.uint16;
2060 lang->base_offset = pOffset->val.uint16;
2061 SDPDBG("code_ISO639 : 0x%02x", lang->code_ISO639);
2062 SDPDBG("encoding : 0x%02x", lang->encoding);
2063 SDPDBG("base_offfset : 0x%02x", lang->base_offset);
2064 *langSeq = sdp_list_append(*langSeq, lang);
2066 curr_data = pOffset->next;
2072 sdp_list_free(*langSeq, free);
2079 int sdp_get_profile_descs(const sdp_record_t *rec, sdp_list_t **profDescSeq)
2081 sdp_profile_desc_t *profDesc;
2082 sdp_data_t *sdpdata, *seq;
2084 *profDescSeq = NULL;
2085 sdpdata = sdp_data_get(rec, SDP_ATTR_PFILE_DESC_LIST);
2086 if (sdpdata == NULL) {
2091 if (!SDP_IS_SEQ(sdpdata->dtd) || sdpdata->val.dataseq == NULL)
2094 for (seq = sdpdata->val.dataseq; seq; seq = seq->next) {
2095 uuid_t *uuid = NULL;
2096 uint16_t version = 0x100;
2098 if (SDP_IS_UUID(seq->dtd)) {
2099 /* Mac OS X 10.7.3 and old Samsung phones do not comply
2100 * to the SDP specification for
2101 * BluetoothProfileDescriptorList. This workaround
2102 * allows to properly parse UUID/version from SDP
2103 * record published by these systems. */
2104 sdp_data_t *next = seq->next;
2105 uuid = &seq->val.uuid;
2106 if (next && next->dtd == SDP_UINT16) {
2107 version = next->val.uint16;
2110 } else if (SDP_IS_SEQ(seq->dtd)) {
2111 sdp_data_t *puuid, *pVnum;
2113 puuid = seq->val.dataseq;
2114 if (puuid == NULL || !SDP_IS_UUID(puuid->dtd))
2117 uuid = &puuid->val.uuid;
2119 pVnum = puuid->next;
2120 if (pVnum == NULL || pVnum->dtd != SDP_UINT16)
2123 version = pVnum->val.uint16;
2128 profDesc = malloc(sizeof(sdp_profile_desc_t));
2130 sdp_list_free(*profDescSeq, free);
2131 *profDescSeq = NULL;
2134 profDesc->uuid = *uuid;
2135 profDesc->version = version;
2137 sdp_uuid_print(&profDesc->uuid);
2138 SDPDBG("Vnum : 0x%04x", profDesc->version);
2140 *profDescSeq = sdp_list_append(*profDescSeq, profDesc);
2146 sdp_list_free(*profDescSeq, free);
2147 *profDescSeq = NULL;
2153 int sdp_get_server_ver(const sdp_record_t *rec, sdp_list_t **u16)
2155 sdp_data_t *d, *curr;
2158 d = sdp_data_get(rec, SDP_ATTR_VERSION_NUM_LIST);
2164 if (!SDP_IS_SEQ(d->dtd) || d->val.dataseq == NULL)
2167 for (curr = d->val.dataseq; curr; curr = curr->next) {
2168 if (curr->dtd != SDP_UINT16)
2170 *u16 = sdp_list_append(*u16, &curr->val.uint16);
2176 sdp_list_free(*u16, NULL);
2183 /* flexible extraction of basic attributes - Jean II */
2184 /* How do we expect caller to extract predefined data sequences? */
2185 int sdp_get_int_attr(const sdp_record_t *rec, uint16_t attrid, int *value)
2187 sdp_data_t *sdpdata = sdp_data_get(rec, attrid);
2190 /* Verify that it is what the caller expects */
2191 if (sdpdata->dtd == SDP_BOOL || sdpdata->dtd == SDP_UINT8 ||
2192 sdpdata->dtd == SDP_UINT16 || sdpdata->dtd == SDP_UINT32 ||
2193 sdpdata->dtd == SDP_INT8 || sdpdata->dtd == SDP_INT16 ||
2194 sdpdata->dtd == SDP_INT32) {
2195 *value = sdpdata->val.uint32;
2202 int sdp_get_string_attr(const sdp_record_t *rec, uint16_t attrid, char *value,
2205 sdp_data_t *sdpdata = sdp_data_get(rec, attrid);
2207 /* Verify that it is what the caller expects */
2208 if (SDP_IS_TEXT_STR(sdpdata->dtd))
2209 if ((int) strlen(sdpdata->val.str) < valuelen) {
2210 strcpy(value, sdpdata->val.str);
2217 #define get_basic_attr(attrID, pAttrValue, fieldName) \
2218 sdp_data_t *data = sdp_data_get(rec, attrID); \
2220 *pAttrValue = data->val.fieldName; \
2226 int sdp_get_service_id(const sdp_record_t *rec, uuid_t *uuid)
2228 get_basic_attr(SDP_ATTR_SERVICE_ID, uuid, uuid);
2231 int sdp_get_group_id(const sdp_record_t *rec, uuid_t *uuid)
2233 get_basic_attr(SDP_ATTR_GROUP_ID, uuid, uuid);
2236 int sdp_get_record_state(const sdp_record_t *rec, uint32_t *svcRecState)
2238 get_basic_attr(SDP_ATTR_RECORD_STATE, svcRecState, uint32);
2241 int sdp_get_service_avail(const sdp_record_t *rec, uint8_t *svcAvail)
2243 get_basic_attr(SDP_ATTR_SERVICE_AVAILABILITY, svcAvail, uint8);
2246 int sdp_get_service_ttl(const sdp_record_t *rec, uint32_t *svcTTLInfo)
2248 get_basic_attr(SDP_ATTR_SVCINFO_TTL, svcTTLInfo, uint32);
2251 int sdp_get_database_state(const sdp_record_t *rec, uint32_t *svcDBState)
2253 get_basic_attr(SDP_ATTR_SVCDB_STATE, svcDBState, uint32);
2257 * NOTE that none of the setXXX() functions below will
2258 * actually update the SDP server, unless the
2259 * {register, update}sdp_record_t() function is invoked.
2262 int sdp_attr_add_new(sdp_record_t *rec, uint16_t attr, uint8_t dtd,
2265 sdp_data_t *d = sdp_data_alloc(dtd, value);
2267 sdp_attr_replace(rec, attr, d);
2273 static int sdp_attr_add_new_with_length(sdp_record_t *rec,
2274 uint16_t attr, uint8_t dtd, const void *value, uint32_t len)
2278 d = sdp_data_alloc_with_length(dtd, value, len);
2282 sdp_attr_replace(rec, attr, d);
2288 * Set the information attributes of the service
2289 * pointed to by rec. The attributes are
2290 * service name, description and provider name
2292 void sdp_set_info_attr(sdp_record_t *rec, const char *name, const char *prov,
2296 sdp_attr_add_new(rec, SDP_ATTR_SVCNAME_PRIMARY,
2297 SDP_TEXT_STR8, name);
2299 sdp_attr_add_new(rec, SDP_ATTR_PROVNAME_PRIMARY,
2300 SDP_TEXT_STR8, prov);
2302 sdp_attr_add_new(rec, SDP_ATTR_SVCDESC_PRIMARY,
2303 SDP_TEXT_STR8, desc);
2306 static sdp_data_t *access_proto_to_dataseq(sdp_record_t *rec, sdp_list_t *proto)
2308 sdp_data_t *seq = NULL;
2309 void *dtds[10], *values[10];
2310 void **seqDTDs, **seqs;
2314 seqlen = sdp_list_len(proto);
2315 seqDTDs = malloc(seqlen * sizeof(void *));
2319 seqs = malloc(seqlen * sizeof(void *));
2325 for (i = 0, p = proto; p; p = p->next, i++) {
2326 sdp_list_t *elt = p->data;
2328 uuid_t *uuid = NULL;
2329 unsigned int pslen = 0;
2330 for (; elt && pslen < ARRAY_SIZE(dtds); elt = elt->next, pslen++) {
2331 sdp_data_t *d = elt->data;
2332 dtds[pslen] = &d->dtd;
2335 uuid = (uuid_t *) d;
2336 values[pslen] = &uuid->value.uuid16;
2339 uuid = (uuid_t *) d;
2340 values[pslen] = &uuid->value.uuid32;
2343 uuid = (uuid_t *) d;
2344 values[pslen] = &uuid->value.uuid128;
2347 values[pslen] = &d->val.uint8;
2350 values[pslen] = &d->val.uint16;
2360 s = sdp_seq_alloc(dtds, values, pslen);
2362 seqDTDs[i] = &s->dtd;
2365 sdp_pattern_add_uuid(rec, uuid);
2368 seq = sdp_seq_alloc(seqDTDs, seqs, seqlen);
2375 * sets the access protocols of the service specified
2376 * to the value specified in "access_proto"
2378 * Note that if there are alternate mechanisms by
2379 * which the service is accessed, then they should
2380 * be specified as sequences
2382 * Using a value of NULL for accessProtocols has
2383 * effect of removing this attribute (if previously set)
2385 * This function replaces the existing sdp_access_proto_t
2386 * structure (if any) with the new one specified.
2388 * returns 0 if successful or -1 if there is a failure.
2390 int sdp_set_access_protos(sdp_record_t *rec, const sdp_list_t *ap)
2392 const sdp_list_t *p;
2393 sdp_data_t *protos = NULL;
2395 for (p = ap; p; p = p->next) {
2396 sdp_data_t *seq = access_proto_to_dataseq(rec, p->data);
2397 protos = sdp_seq_append(protos, seq);
2400 sdp_attr_add(rec, SDP_ATTR_PROTO_DESC_LIST, protos);
2405 int sdp_set_add_access_protos(sdp_record_t *rec, const sdp_list_t *ap)
2407 const sdp_list_t *p;
2408 sdp_data_t *protos = NULL;
2410 for (p = ap; p; p = p->next) {
2411 sdp_data_t *seq = access_proto_to_dataseq(rec, p->data);
2412 protos = sdp_seq_append(protos, seq);
2415 sdp_attr_add(rec, SDP_ATTR_ADD_PROTO_DESC_LIST,
2416 protos ? sdp_data_alloc(SDP_SEQ8, protos) : NULL);
2422 * set the "LanguageBase" attributes of the service record
2423 * record to the value specified in "langAttrList".
2425 * "langAttrList" is a linked list of "sdp_lang_attr_t"
2426 * objects, one for each language in which user visible
2427 * attributes are present in the service record.
2429 * Using a value of NULL for langAttrList has
2430 * effect of removing this attribute (if previously set)
2432 * This function replaces the exisiting sdp_lang_attr_t
2433 * structure (if any) with the new one specified.
2435 * returns 0 if successful or -1 if there is a failure.
2437 int sdp_set_lang_attr(sdp_record_t *rec, const sdp_list_t *seq)
2439 uint8_t uint16 = SDP_UINT16;
2440 int status = 0, i = 0, seqlen = sdp_list_len(seq);
2441 void **dtds, **values;
2442 const sdp_list_t *p;
2444 dtds = malloc(3 * seqlen * sizeof(void *));
2448 values = malloc(3 * seqlen * sizeof(void *));
2454 for (p = seq; p; p = p->next) {
2455 sdp_lang_attr_t *lang = p->data;
2461 values[i] = &lang->code_ISO639;
2464 values[i] = &lang->encoding;
2467 values[i] = &lang->base_offset;
2471 sdp_data_t *seq = sdp_seq_alloc(dtds, values, 3 * seqlen);
2472 sdp_attr_add(rec, SDP_ATTR_LANG_BASE_ATTR_ID_LIST, seq);
2480 * set the "ServiceID" attribute of the service.
2482 * This is the UUID of the service.
2484 * returns 0 if successful or -1 if there is a failure.
2486 void sdp_set_service_id(sdp_record_t *rec, uuid_t uuid)
2488 switch (uuid.type) {
2490 sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID16,
2491 &uuid.value.uuid16);
2494 sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID32,
2495 &uuid.value.uuid32);
2498 sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID128,
2499 &uuid.value.uuid128);
2502 sdp_pattern_add_uuid(rec, &uuid);
2506 * set the GroupID attribute of the service record defining a group.
2508 * This is the UUID of the group.
2510 * returns 0 if successful or -1 if there is a failure.
2512 void sdp_set_group_id(sdp_record_t *rec, uuid_t uuid)
2514 switch (uuid.type) {
2516 sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID16,
2517 &uuid.value.uuid16);
2520 sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID32,
2521 &uuid.value.uuid32);
2524 sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID128,
2525 &uuid.value.uuid128);
2528 sdp_pattern_add_uuid(rec, &uuid);
2532 * set the ProfileDescriptorList attribute of the service record
2533 * pointed to by record to the value specified in "profileDesc".
2535 * Each element in the list is an object of type
2536 * sdp_profile_desc_t which is a definition of the
2537 * Bluetooth profile that this service conforms to.
2539 * Using a value of NULL for profileDesc has
2540 * effect of removing this attribute (if previously set)
2542 * This function replaces the exisiting ProfileDescriptorList
2543 * structure (if any) with the new one specified.
2545 * returns 0 if successful or -1 if there is a failure.
2547 int sdp_set_profile_descs(sdp_record_t *rec, const sdp_list_t *profiles)
2550 uint8_t uuid16 = SDP_UUID16;
2551 uint8_t uuid32 = SDP_UUID32;
2552 uint8_t uuid128 = SDP_UUID128;
2553 uint8_t uint16 = SDP_UINT16;
2554 int i = 0, seqlen = sdp_list_len(profiles);
2555 void **seqDTDs, **seqs;
2556 const sdp_list_t *p;
2559 seqDTDs = malloc(seqlen * sizeof(void *));
2563 seqs = malloc(seqlen * sizeof(void *));
2569 for (p = profiles; p; p = p->next) {
2571 void *dtds[2], *values[2];
2572 sdp_profile_desc_t *profile = p->data;
2577 switch (profile->uuid.type) {
2580 values[0] = &profile->uuid.value.uuid16;
2584 values[0] = &profile->uuid.value.uuid32;
2588 values[0] = &profile->uuid.value.uuid128;
2595 values[1] = &profile->version;
2596 seq = sdp_seq_alloc(dtds, values, 2);
2603 seqDTDs[i] = &seq->dtd;
2605 sdp_pattern_add_uuid(rec, &profile->uuid);
2609 pAPSeq = sdp_seq_alloc(seqDTDs, seqs, seqlen);
2610 sdp_attr_add(rec, SDP_ATTR_PFILE_DESC_LIST, pAPSeq);
2618 * sets various URL attributes of the service
2619 * pointed to by record. The URL include
2621 * client: a URL to the client's
2622 * platform specific (WinCE, PalmOS) executable
2623 * code that can be used to access this service.
2625 * doc: a URL pointing to service documentation
2627 * icon: a URL to an icon that can be used to represent
2630 * Note that you need to pass NULL for any URLs
2631 * that you don't want to set or remove
2633 void sdp_set_url_attr(sdp_record_t *rec, const char *client, const char *doc,
2636 sdp_attr_add_new(rec, SDP_ATTR_CLNT_EXEC_URL, SDP_URL_STR8, client);
2637 sdp_attr_add_new(rec, SDP_ATTR_DOC_URL, SDP_URL_STR8, doc);
2638 sdp_attr_add_new(rec, SDP_ATTR_ICON_URL, SDP_URL_STR8, icon);
2641 uuid_t *sdp_uuid16_create(uuid_t *u, uint16_t val)
2643 memset(u, 0, sizeof(uuid_t));
2644 u->type = SDP_UUID16;
2645 u->value.uuid16 = val;
2649 uuid_t *sdp_uuid32_create(uuid_t *u, uint32_t val)
2651 memset(u, 0, sizeof(uuid_t));
2652 u->type = SDP_UUID32;
2653 u->value.uuid32 = val;
2657 uuid_t *sdp_uuid128_create(uuid_t *u, const void *val)
2659 memset(u, 0, sizeof(uuid_t));
2660 u->type = SDP_UUID128;
2661 memcpy(&u->value.uuid128, val, sizeof(uint128_t));
2666 * UUID comparison function
2667 * returns 0 if uuidValue1 == uuidValue2 else -1
2669 int sdp_uuid_cmp(const void *p1, const void *p2)
2671 uuid_t *u1 = sdp_uuid_to_uuid128(p1);
2672 uuid_t *u2 = sdp_uuid_to_uuid128(p2);
2675 ret = sdp_uuid128_cmp(u1, u2);
2684 * UUID comparison function
2685 * returns 0 if uuidValue1 == uuidValue2 else -1
2687 int sdp_uuid16_cmp(const void *p1, const void *p2)
2689 const uuid_t *u1 = p1;
2690 const uuid_t *u2 = p2;
2691 return memcmp(&u1->value.uuid16, &u2->value.uuid16, sizeof(uint16_t));
2695 * UUID comparison function
2696 * returns 0 if uuidValue1 == uuidValue2 else -1
2698 int sdp_uuid128_cmp(const void *p1, const void *p2)
2700 const uuid_t *u1 = p1;
2701 const uuid_t *u2 = p2;
2702 return memcmp(&u1->value.uuid128, &u2->value.uuid128, sizeof(uint128_t));
2706 * 128 to 16 bit and 32 to 16 bit UUID conversion functions
2707 * yet to be implemented. Note that the input is in NBO in
2708 * both 32 and 128 bit UUIDs and conversion is needed
2710 void sdp_uuid16_to_uuid128(uuid_t *uuid128, const uuid_t *uuid16)
2713 * We have a 16 bit value, which needs to be added to
2714 * bytes 3 and 4 (at indices 2 and 3) of the Bluetooth base
2716 unsigned short data1;
2718 /* allocate a 128bit UUID and init to the Bluetooth base UUID */
2719 uuid128->value.uuid128 = bluetooth_base_uuid;
2720 uuid128->type = SDP_UUID128;
2722 /* extract bytes 2 and 3 of 128bit BT base UUID */
2723 memcpy(&data1, &bluetooth_base_uuid.data[2], 2);
2725 /* add the given UUID (16 bits) */
2726 data1 += htons(uuid16->value.uuid16);
2728 /* set bytes 2 and 3 of the 128 bit value */
2729 memcpy(&uuid128->value.uuid128.data[2], &data1, 2);
2732 void sdp_uuid32_to_uuid128(uuid_t *uuid128, const uuid_t *uuid32)
2735 * We have a 32 bit value, which needs to be added to
2736 * bytes 1->4 (at indices 0 thru 3) of the Bluetooth base
2740 /* allocate a 128bit UUID and init to the Bluetooth base UUID */
2741 uuid128->value.uuid128 = bluetooth_base_uuid;
2742 uuid128->type = SDP_UUID128;
2744 /* extract first 4 bytes */
2745 memcpy(&data0, &bluetooth_base_uuid.data[0], 4);
2747 /* add the given UUID (32bits) */
2748 data0 += htonl(uuid32->value.uuid32);
2750 /* set the 4 bytes of the 128 bit value */
2751 memcpy(&uuid128->value.uuid128.data[0], &data0, 4);
2754 uuid_t *sdp_uuid_to_uuid128(const uuid_t *uuid)
2756 uuid_t *uuid128 = bt_malloc(sizeof(uuid_t));
2761 memset(uuid128, 0, sizeof(uuid_t));
2762 switch (uuid->type) {
2767 sdp_uuid32_to_uuid128(uuid128, uuid);
2770 sdp_uuid16_to_uuid128(uuid128, uuid);
2777 * converts a 128-bit uuid to a 16/32-bit one if possible
2778 * returns true if uuid contains a 16/32-bit UUID at exit
2780 int sdp_uuid128_to_uuid(uuid_t *uuid)
2782 uint128_t *b = &bluetooth_base_uuid;
2783 uint128_t *u = &uuid->value.uuid128;
2787 if (uuid->type != SDP_UUID128)
2790 for (i = 4; i < sizeof(b->data); i++)
2791 if (b->data[i] != u->data[i])
2794 memcpy(&data, u->data, 4);
2796 if (data <= 0xffff) {
2797 uuid->type = SDP_UUID16;
2798 uuid->value.uuid16 = (uint16_t) data;
2800 uuid->type = SDP_UUID32;
2801 uuid->value.uuid32 = data;
2807 * convert a UUID to the 16-bit short-form
2809 int sdp_uuid_to_proto(uuid_t *uuid)
2812 if (sdp_uuid128_to_uuid(&u)) {
2815 return u.value.uuid16;
2817 return u.value.uuid32;
2824 * This function appends data to the PDU buffer "dst" from source "src".
2825 * The data length is also computed and set.
2826 * Should the PDU length exceed 2^8, then sequence type is
2827 * set accordingly and the data is memmove()'d.
2829 void sdp_append_to_buf(sdp_buf_t *dst, uint8_t *data, uint32_t len)
2831 uint8_t *p = dst->data;
2834 SDPDBG("Append src size: %d", len);
2835 SDPDBG("Append dst size: %d", dst->data_size);
2836 SDPDBG("Dst buffer size: %d", dst->buf_size);
2837 if (dst->data_size == 0 && dtd == 0) {
2838 /* create initial sequence */
2840 dst->data_size += sizeof(uint8_t);
2841 /* reserve space for sequence size */
2842 dst->data_size += sizeof(uint8_t);
2845 memcpy(dst->data + dst->data_size, data, len);
2846 dst->data_size += len;
2848 dtd = *(uint8_t *) dst->data;
2849 if (dst->data_size > UCHAR_MAX && dtd == SDP_SEQ8) {
2850 short offset = sizeof(uint8_t) + sizeof(uint8_t);
2851 memmove(dst->data + offset + 1, dst->data + offset,
2852 dst->data_size - offset);
2854 dst->data_size += 1;
2856 dtd = *(uint8_t *) p;
2857 p += sizeof(uint8_t);
2860 *(uint8_t *) p = dst->data_size - sizeof(uint8_t) - sizeof(uint8_t);
2863 bt_put_be16(dst->data_size - sizeof(uint8_t) - sizeof(uint16_t), p);
2866 bt_put_be32(dst->data_size - sizeof(uint8_t) - sizeof(uint32_t), p);
2871 void sdp_append_to_pdu(sdp_buf_t *pdu, sdp_data_t *d)
2875 memset(&append, 0, sizeof(sdp_buf_t));
2876 sdp_gen_buffer(&append, d);
2877 append.data = malloc(append.buf_size);
2881 sdp_set_attrid(&append, d->attrId);
2882 sdp_gen_pdu(&append, d);
2883 sdp_append_to_buf(pdu, append.data, append.data_size);
2888 * Registers an sdp record.
2890 * It is incorrect to call this method on a record that
2891 * has been already registered with the server.
2893 * Returns zero on success, otherwise -1 (and sets errno).
2895 int sdp_device_record_register_binary(sdp_session_t *session, bdaddr_t *device, uint8_t *data, uint32_t size, uint8_t flags, uint32_t *handle)
2897 uint8_t *req, *rsp, *p;
2898 uint32_t reqsize, rspsize;
2899 sdp_pdu_hdr_t *reqhdr, *rsphdr;
2904 if (!session->local) {
2908 req = malloc(SDP_REQ_BUFFER_SIZE);
2909 rsp = malloc(SDP_RSP_BUFFER_SIZE);
2910 if (req == NULL || rsp == NULL) {
2916 reqhdr = (sdp_pdu_hdr_t *)req;
2917 reqhdr->pdu_id = SDP_SVC_REGISTER_REQ;
2918 reqhdr->tid = htons(sdp_gen_tid(session));
2919 reqsize = sizeof(sdp_pdu_hdr_t) + 1;
2920 p = req + sizeof(sdp_pdu_hdr_t);
2922 if (bacmp(device, BDADDR_ANY)) {
2923 *p++ = flags | SDP_DEVICE_RECORD;
2924 bacpy((bdaddr_t *) p, device);
2925 p += sizeof(bdaddr_t);
2926 reqsize += sizeof(bdaddr_t);
2930 memcpy(p, data, size);
2932 reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
2934 status = sdp_send_req_w4_rsp(session, req, rsp, reqsize, &rspsize);
2938 if (rspsize < sizeof(sdp_pdu_hdr_t)) {
2939 SDPERR("Unexpected end of packet");
2945 rsphdr = (sdp_pdu_hdr_t *) rsp;
2946 p = rsp + sizeof(sdp_pdu_hdr_t);
2948 if (rsphdr->pdu_id == SDP_ERROR_RSP) {
2949 /* Invalid service record */
2952 } else if (rsphdr->pdu_id != SDP_SVC_REGISTER_RSP) {
2956 if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint32_t)) {
2957 SDPERR("Unexpected end of packet");
2963 *handle = bt_get_be32(p);
2973 int sdp_device_record_register(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec, uint8_t flags)
2981 if (rec->handle && rec->handle != 0xffffffff) {
2982 uint32_t handle = rec->handle;
2983 sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle);
2984 sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data);
2987 if (sdp_gen_record_pdu(rec, &pdu) < 0) {
2992 err = sdp_device_record_register_binary(session, device,
2993 pdu.data, pdu.data_size, flags, &handle);
2998 sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle);
2999 rec->handle = handle;
3000 sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data);
3006 int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags)
3008 return sdp_device_record_register(session, BDADDR_ANY, rec, flags);
3012 * unregister a service record
3014 int sdp_device_record_unregister_binary(sdp_session_t *session, bdaddr_t *device, uint32_t handle)
3016 uint8_t *reqbuf, *rspbuf, *p;
3017 uint32_t reqsize = 0, rspsize = 0;
3018 sdp_pdu_hdr_t *reqhdr, *rsphdr;
3023 if (handle == SDP_SERVER_RECORD_HANDLE) {
3028 if (!session->local) {
3033 reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
3034 rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
3035 if (!reqbuf || !rspbuf) {
3040 reqhdr = (sdp_pdu_hdr_t *) reqbuf;
3041 reqhdr->pdu_id = SDP_SVC_REMOVE_REQ;
3042 reqhdr->tid = htons(sdp_gen_tid(session));
3044 p = reqbuf + sizeof(sdp_pdu_hdr_t);
3045 reqsize = sizeof(sdp_pdu_hdr_t);
3046 bt_put_be32(handle, p);
3047 reqsize += sizeof(uint32_t);
3049 reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
3050 status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
3054 if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint16_t)) {
3055 SDPERR("Unexpected end of packet");
3061 rsphdr = (sdp_pdu_hdr_t *) rspbuf;
3062 p = rspbuf + sizeof(sdp_pdu_hdr_t);
3064 if (rsphdr->pdu_id == SDP_ERROR_RSP) {
3065 /* For this case the status always is invalid record handle */
3068 } else if (rsphdr->pdu_id != SDP_SVC_REMOVE_RSP) {
3074 memcpy(&tmp, p, sizeof(tmp));
3085 int sdp_device_record_unregister(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec)
3089 err = sdp_device_record_unregister_binary(session, device, rec->handle);
3091 sdp_record_free(rec);
3096 int sdp_record_unregister(sdp_session_t *session, sdp_record_t *rec)
3098 return sdp_device_record_unregister(session, BDADDR_ANY, rec);
3102 * modify an existing service record
3104 int sdp_device_record_update_binary(sdp_session_t *session, bdaddr_t *device, uint32_t handle, uint8_t *data, uint32_t size)
3109 int sdp_device_record_update(sdp_session_t *session, bdaddr_t *device, const sdp_record_t *rec)
3111 uint8_t *reqbuf, *rspbuf, *p;
3112 uint32_t reqsize, rspsize;
3113 sdp_pdu_hdr_t *reqhdr, *rsphdr;
3120 handle = rec->handle;
3122 if (handle == SDP_SERVER_RECORD_HANDLE) {
3126 if (!session->local) {
3130 reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
3131 rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
3132 if (!reqbuf || !rspbuf) {
3137 reqhdr = (sdp_pdu_hdr_t *) reqbuf;
3138 reqhdr->pdu_id = SDP_SVC_UPDATE_REQ;
3139 reqhdr->tid = htons(sdp_gen_tid(session));
3141 p = reqbuf + sizeof(sdp_pdu_hdr_t);
3142 reqsize = sizeof(sdp_pdu_hdr_t);
3144 bt_put_be32(handle, p);
3145 reqsize += sizeof(uint32_t);
3146 p += sizeof(uint32_t);
3148 if (sdp_gen_record_pdu(rec, &pdu) < 0) {
3153 memcpy(p, pdu.data, pdu.data_size);
3154 reqsize += pdu.data_size;
3157 reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
3158 status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
3162 if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint16_t)) {
3163 SDPERR("Unexpected end of packet");
3169 SDPDBG("Send req status : %d", status);
3171 rsphdr = (sdp_pdu_hdr_t *) rspbuf;
3172 p = rspbuf + sizeof(sdp_pdu_hdr_t);
3174 if (rsphdr->pdu_id == SDP_ERROR_RSP) {
3175 /* The status can be invalid sintax or invalid record handle */
3178 } else if (rsphdr->pdu_id != SDP_SVC_UPDATE_RSP) {
3184 memcpy(&tmp, p, sizeof(tmp));
3194 int sdp_record_update(sdp_session_t *session, const sdp_record_t *rec)
3196 return sdp_device_record_update(session, BDADDR_ANY, rec);
3199 sdp_record_t *sdp_record_alloc(void)
3201 sdp_record_t *rec = malloc(sizeof(sdp_record_t));
3206 memset(rec, 0, sizeof(sdp_record_t));
3207 rec->handle = 0xffffffff;
3212 * Free the contents of a service record
3214 void sdp_record_free(sdp_record_t *rec)
3216 sdp_list_free(rec->attrlist, (sdp_free_func_t) sdp_data_free);
3217 sdp_list_free(rec->pattern, free);
3221 void sdp_pattern_add_uuid(sdp_record_t *rec, uuid_t *uuid)
3223 uuid_t *uuid128 = sdp_uuid_to_uuid128(uuid);
3225 SDPDBG("Elements in target pattern : %d", sdp_list_len(rec->pattern));
3226 SDPDBG("Trying to add : 0x%lx", (unsigned long) uuid128);
3228 if (sdp_list_find(rec->pattern, uuid128, sdp_uuid128_cmp) == NULL)
3229 rec->pattern = sdp_list_insert_sorted(rec->pattern, uuid128, sdp_uuid128_cmp);
3233 SDPDBG("Elements in target pattern : %d", sdp_list_len(rec->pattern));
3236 void sdp_pattern_add_uuidseq(sdp_record_t *rec, sdp_list_t *seq)
3238 for (; seq; seq = seq->next) {
3239 uuid_t *uuid = (uuid_t *)seq->data;
3240 sdp_pattern_add_uuid(rec, uuid);
3245 * Extract a sequence of service record handles from a PDU buffer
3246 * and add the entries to a sdp_list_t. Note that the service record
3247 * handles are not in "data element sequence" form, but just like
3248 * an array of service handles
3250 static void extract_record_handle_seq(uint8_t *pdu, int bufsize, sdp_list_t **seq, int count, unsigned int *scanned)
3252 sdp_list_t *pSeq = *seq;
3253 uint8_t *pdata = pdu;
3256 for (n = 0; n < count; n++) {
3258 if (bufsize < (int) sizeof(uint32_t)) {
3259 SDPERR("Unexpected end of packet");
3262 pSvcRec = malloc(sizeof(uint32_t));
3265 *pSvcRec = bt_get_be32(pdata);
3266 pSeq = sdp_list_append(pSeq, pSvcRec);
3267 pdata += sizeof(uint32_t);
3268 *scanned += sizeof(uint32_t);
3269 bufsize -= sizeof(uint32_t);
3274 * Generate the attribute sequence pdu form
3275 * from sdp_list_t elements. Return length of attr seq
3277 static int gen_dataseq_pdu(uint8_t *dst, const sdp_list_t *seq, uint8_t dtd)
3279 sdp_data_t *dataseq;
3280 void **types, **values;
3282 int i, seqlen = sdp_list_len(seq);
3284 /* Fill up the value and the dtd arrays */
3287 SDPDBG("Seq length : %d", seqlen);
3289 types = malloc(seqlen * sizeof(void *));
3293 values = malloc(seqlen * sizeof(void *));
3299 for (i = 0; i < seqlen; i++) {
3300 void *data = seq->data;
3302 if (SDP_IS_UUID(dtd))
3303 data = &((uuid_t *)data)->value;
3308 dataseq = sdp_seq_alloc(types, values, seqlen);
3315 memset(&buf, 0, sizeof(sdp_buf_t));
3316 sdp_gen_buffer(&buf, dataseq);
3317 buf.data = malloc(buf.buf_size);
3320 sdp_data_free(dataseq);
3326 SDPDBG("Data Seq : 0x%p", seq);
3327 seqlen = sdp_gen_pdu(&buf, dataseq);
3328 SDPDBG("Copying : %d", buf.data_size);
3329 memcpy(dst, buf.data, buf.data_size);
3331 sdp_data_free(dataseq);
3339 static int gen_searchseq_pdu(uint8_t *dst, const sdp_list_t *seq)
3341 uuid_t *uuid = seq->data;
3342 return gen_dataseq_pdu(dst, seq, uuid->type);
3345 static int gen_attridseq_pdu(uint8_t *dst, const sdp_list_t *seq, uint8_t dataType)
3347 return gen_dataseq_pdu(dst, seq, dataType);
3352 unsigned char data[16];
3353 } __attribute__ ((packed)) sdp_cstate_t;
3355 static int copy_cstate(uint8_t *pdata, int pdata_len, const sdp_cstate_t *cstate)
3358 uint8_t len = cstate->length;
3359 if (len >= pdata_len) {
3360 SDPERR("Continuation state size exceeds internal buffer");
3361 len = pdata_len - 1;
3364 memcpy(pdata, cstate->data, len);
3372 * This is a service search request.
3376 * sdp_list_t *search
3377 * Singly linked list containing elements of the search
3378 * pattern. Each entry in the list is a UUID (DataTypeSDP_UUID16)
3379 * of the service to be searched
3381 * uint16_t max_rec_num
3382 * A 16 bit integer which tells the service, the maximum
3383 * entries that the client can handle in the response. The
3384 * server is obliged not to return > max_rec_num entries
3390 * The request completed successfully. This does not
3391 * mean the requested services were found
3393 * On any failure and sets errno
3395 * sdp_list_t **rsp_list
3396 * This variable is set on a successful return if there are
3397 * non-zero service handles. It is a singly linked list of
3398 * service record handles (uint16_t)
3400 int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search,
3401 uint16_t max_rec_num, sdp_list_t **rsp)
3404 uint32_t reqsize = 0, _reqsize;
3405 uint32_t rspsize = 0, rsplen;
3408 unsigned scanned, pdata_len;
3409 uint8_t *pdata, *_pdata;
3410 uint8_t *reqbuf, *rspbuf;
3411 sdp_pdu_hdr_t *reqhdr, *rsphdr;
3412 sdp_cstate_t *cstate = NULL;
3414 reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
3415 rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
3416 if (!reqbuf || !rspbuf) {
3421 reqhdr = (sdp_pdu_hdr_t *) reqbuf;
3422 reqhdr->pdu_id = SDP_SVC_SEARCH_REQ;
3423 pdata = reqbuf + sizeof(sdp_pdu_hdr_t);
3424 reqsize = sizeof(sdp_pdu_hdr_t);
3426 /* add service class IDs for search */
3427 seqlen = gen_searchseq_pdu(pdata, search);
3429 SDPDBG("Data seq added : %d", seqlen);
3431 /* set the length and increment the pointer */
3435 /* specify the maximum svc rec count that client expects */
3436 bt_put_be16(max_rec_num, pdata);
3437 reqsize += sizeof(uint16_t);
3438 pdata += sizeof(uint16_t);
3445 /* Add continuation state or NULL (first time) */
3446 reqsize = _reqsize + copy_cstate(_pdata,
3447 SDP_REQ_BUFFER_SIZE - _reqsize, cstate);
3449 /* Set the request header's param length */
3450 reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
3452 reqhdr->tid = htons(sdp_gen_tid(session));
3454 * Send the request, wait for response and if
3455 * no error, set the appropriate values and return
3457 status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
3461 if (rspsize < sizeof(sdp_pdu_hdr_t)) {
3462 SDPERR("Unexpected end of packet");
3467 rsphdr = (sdp_pdu_hdr_t *) rspbuf;
3468 rsplen = ntohs(rsphdr->plen);
3470 if (rsphdr->pdu_id == SDP_ERROR_RSP) {
3471 SDPDBG("Status : 0x%x", rsphdr->pdu_id);
3476 pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
3477 pdata_len = rspsize - sizeof(sdp_pdu_hdr_t);
3479 if (pdata_len < sizeof(uint16_t) + sizeof(uint16_t)) {
3480 SDPERR("Unexpected end of packet");
3485 /* net service record match count */
3486 pdata += sizeof(uint16_t);
3487 scanned += sizeof(uint16_t);
3488 pdata_len -= sizeof(uint16_t);
3489 rec_count = bt_get_be16(pdata);
3490 pdata += sizeof(uint16_t);
3491 scanned += sizeof(uint16_t);
3492 pdata_len -= sizeof(uint16_t);
3494 SDPDBG("Current svc count: %d", rec_count);
3495 SDPDBG("ResponseLength: %d", rsplen);
3501 extract_record_handle_seq(pdata, pdata_len, rsp, rec_count, &scanned);
3502 SDPDBG("BytesScanned : %d", scanned);
3504 if (rsplen > scanned) {
3507 if (rspsize < sizeof(sdp_pdu_hdr_t) + scanned + sizeof(uint8_t)) {
3508 SDPERR("Unexpected end of packet: continuation state data missing");
3513 pdata = rspbuf + sizeof(sdp_pdu_hdr_t) + scanned;
3514 cstate_len = *(uint8_t *) pdata;
3515 if (cstate_len > 0) {
3516 cstate = (sdp_cstate_t *)pdata;
3517 SDPDBG("Cont state length: %d", cstate_len);
3531 * This is a service attribute request.
3536 * The handle of the service for which the attribute(s) are
3539 * sdp_attrreq_type_t reqtype
3540 * Attribute identifiers are 16 bit unsigned integers specified
3541 * in one of 2 ways described below :
3542 * SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
3543 * They are the actual attribute identifiers in ascending order
3545 * SDP_ATTR_REQ_RANGE - 32bit identifier range
3546 * The high-order 16bits is the start of range
3547 * the low-order 16bits are the end of range
3548 * 0x0000 to 0xFFFF gets all attributes
3550 * sdp_list_t *attrid
3551 * Singly linked list containing attribute identifiers desired.
3552 * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
3553 * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
3556 * return sdp_record_t *
3558 * On any error and sets errno
3560 * The service record
3562 sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle,
3563 sdp_attrreq_type_t reqtype, const sdp_list_t *attrids)
3565 uint32_t reqsize = 0, _reqsize;
3566 uint32_t rspsize = 0, rsp_count;
3567 int attr_list_len = 0;
3569 unsigned int pdata_len;
3570 uint8_t *pdata, *_pdata;
3571 uint8_t *reqbuf, *rspbuf;
3572 sdp_pdu_hdr_t *reqhdr, *rsphdr;
3573 sdp_cstate_t *cstate = NULL;
3574 uint8_t cstate_len = 0;
3575 sdp_buf_t rsp_concat_buf;
3576 sdp_record_t *rec = 0;
3578 if (reqtype != SDP_ATTR_REQ_INDIVIDUAL && reqtype != SDP_ATTR_REQ_RANGE) {
3583 memset(&rsp_concat_buf, 0, sizeof(sdp_buf_t));
3585 reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
3586 rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
3587 if (!reqbuf || !rspbuf) {
3591 reqhdr = (sdp_pdu_hdr_t *) reqbuf;
3592 reqhdr->pdu_id = SDP_SVC_ATTR_REQ;
3594 pdata = reqbuf + sizeof(sdp_pdu_hdr_t);
3595 reqsize = sizeof(sdp_pdu_hdr_t);
3597 /* add the service record handle */
3598 bt_put_be32(handle, pdata);
3599 reqsize += sizeof(uint32_t);
3600 pdata += sizeof(uint32_t);
3602 /* specify the response limit */
3603 bt_put_be16(65535, pdata);
3604 reqsize += sizeof(uint16_t);
3605 pdata += sizeof(uint16_t);
3607 /* get attr seq PDU form */
3608 seqlen = gen_attridseq_pdu(pdata, attrids,
3609 reqtype == SDP_ATTR_REQ_INDIVIDUAL? SDP_UINT16 : SDP_UINT32);
3616 SDPDBG("Attr list length : %d", seqlen);
3618 /* save before Continuation State */
3625 /* add NULL continuation state */
3626 reqsize = _reqsize + copy_cstate(_pdata,
3627 SDP_REQ_BUFFER_SIZE - _reqsize, cstate);
3629 /* set the request header's param length */
3630 reqhdr->tid = htons(sdp_gen_tid(session));
3631 reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
3633 status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
3637 if (rspsize < sizeof(sdp_pdu_hdr_t)) {
3638 SDPERR("Unexpected end of packet");
3642 rsphdr = (sdp_pdu_hdr_t *) rspbuf;
3643 if (rsphdr->pdu_id == SDP_ERROR_RSP) {
3644 SDPDBG("PDU ID : 0x%x", rsphdr->pdu_id);
3647 pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
3648 pdata_len = rspsize - sizeof(sdp_pdu_hdr_t);
3650 if (pdata_len < sizeof(uint16_t)) {
3651 SDPERR("Unexpected end of packet");
3655 rsp_count = bt_get_be16(pdata);
3656 attr_list_len += rsp_count;
3657 pdata += sizeof(uint16_t);
3658 pdata_len -= sizeof(uint16_t);
3661 * if continuation state set need to re-issue request before
3664 if (pdata_len < rsp_count + sizeof(uint8_t)) {
3665 SDPERR("Unexpected end of packet: continuation state data missing");
3668 cstate_len = *(uint8_t *) (pdata + rsp_count);
3670 SDPDBG("Response id : %d", rsphdr->pdu_id);
3671 SDPDBG("Attrlist byte count : %d", rsp_count);
3672 SDPDBG("sdp_cstate_t length : %d", cstate_len);
3675 * a split response: concatenate intermediate responses
3676 * and the last one (which has cstate_len == 0)
3678 if (cstate_len > 0 || rsp_concat_buf.data_size != 0) {
3679 uint8_t *targetPtr = NULL;
3681 cstate = cstate_len > 0 ? (sdp_cstate_t *) (pdata + rsp_count) : 0;
3683 /* build concatenated response buffer */
3684 rsp_concat_buf.data = realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count);
3685 rsp_concat_buf.buf_size = rsp_concat_buf.data_size + rsp_count;
3686 targetPtr = rsp_concat_buf.data + rsp_concat_buf.data_size;
3687 memcpy(targetPtr, pdata, rsp_count);
3688 rsp_concat_buf.data_size += rsp_count;
3692 if (attr_list_len > 0) {
3694 if (rsp_concat_buf.data_size != 0) {
3695 pdata = rsp_concat_buf.data;
3696 pdata_len = rsp_concat_buf.data_size;
3698 rec = sdp_extract_pdu(pdata, pdata_len, &scanned);
3703 free(rsp_concat_buf.data);
3709 * SDP transaction structure for asynchronous search
3711 struct sdp_transaction {
3712 sdp_callback_t *cb; /* called when the transaction finishes */
3713 void *udata; /* client user data */
3714 uint8_t *reqbuf; /* pointer to request PDU */
3715 sdp_buf_t rsp_concat_buf;
3716 uint32_t reqsize; /* without cstate */
3717 int err; /* ZERO if success or the errno if failed */
3721 * Creates a new sdp session for asynchronous search
3724 * non-blocking L2CAP socket
3728 * NULL - On memory allocation failure
3730 sdp_session_t *sdp_create(int sk, uint32_t flags)
3732 sdp_session_t *session;
3733 struct sdp_transaction *t;
3735 session = malloc(sizeof(sdp_session_t));
3740 memset(session, 0, sizeof(*session));
3742 session->flags = flags;
3745 t = malloc(sizeof(struct sdp_transaction));
3751 memset(t, 0, sizeof(*t));
3759 * Sets the callback function/user data used to notify the application
3760 * that the asynchronous transaction finished. This function must be
3761 * called before request an asynchronous search.
3764 * sdp_session_t *session
3765 * Current sdp session to be handled
3766 * sdp_callback_t *cb
3767 * callback to be called when the transaction finishes
3769 * user data passed to callback
3774 int sdp_set_notify(sdp_session_t *session, sdp_callback_t *func, void *udata)
3776 struct sdp_transaction *t;
3778 if (!session || !session->priv)
3789 * This function starts an asynchronous service search request.
3790 * The incoming and outgoing data are stored in the transaction structure
3791 * buffers. When there is incoming data the sdp_process function must be
3792 * called to get the data and handle the continuation state.
3795 * sdp_session_t *session
3796 * Current sdp session to be handled
3798 * sdp_list_t *search
3799 * Singly linked list containing elements of the search
3800 * pattern. Each entry in the list is a UUID (DataTypeSDP_UUID16)
3801 * of the service to be searched
3803 * uint16_t max_rec_num
3804 * A 16 bit integer which tells the service, the maximum
3805 * entries that the client can handle in the response. The
3806 * server is obliged not to return > max_rec_num entries
3811 * 0 - if the request has been sent properly
3812 * -1 - On any failure and sets errno
3815 int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search, uint16_t max_rec_num)
3817 struct sdp_transaction *t;
3818 sdp_pdu_hdr_t *reqhdr;
3820 int cstate_len, seqlen = 0;
3822 if (!session || !session->priv)
3827 /* clean possible allocated buffer */
3828 free(t->rsp_concat_buf.data);
3829 memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t));
3832 t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
3838 memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE);
3840 reqhdr = (sdp_pdu_hdr_t *) t->reqbuf;
3841 reqhdr->tid = htons(sdp_gen_tid(session));
3842 reqhdr->pdu_id = SDP_SVC_SEARCH_REQ;
3845 pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t);
3846 t->reqsize = sizeof(sdp_pdu_hdr_t);
3848 /* add service class IDs for search */
3849 seqlen = gen_searchseq_pdu(pdata, search);
3851 SDPDBG("Data seq added : %d", seqlen);
3853 /* now set the length and increment the pointer */
3854 t->reqsize += seqlen;
3857 bt_put_be16(max_rec_num, pdata);
3858 t->reqsize += sizeof(uint16_t);
3859 pdata += sizeof(uint16_t);
3861 /* set the request header's param length */
3862 cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL);
3863 reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t));
3865 if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) {
3866 SDPERR("Error sending data:%m");
3881 * This function starts an asynchronous service attribute request.
3882 * The incoming and outgoing data are stored in the transaction structure
3883 * buffers. When there is incoming data the sdp_process function must be
3884 * called to get the data and handle the continuation state.
3887 * sdp_session_t *session
3888 * Current sdp session to be handled
3891 * The handle of the service for which the attribute(s) are
3894 * sdp_attrreq_type_t reqtype
3895 * Attribute identifiers are 16 bit unsigned integers specified
3896 * in one of 2 ways described below :
3897 * SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
3898 * They are the actual attribute identifiers in ascending order
3900 * SDP_ATTR_REQ_RANGE - 32bit identifier range
3901 * The high-order 16bits is the start of range
3902 * the low-order 16bits are the end of range
3903 * 0x0000 to 0xFFFF gets all attributes
3905 * sdp_list_t *attrid_list
3906 * Singly linked list containing attribute identifiers desired.
3907 * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
3908 * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
3912 * 0 - if the request has been sent properly
3913 * -1 - On any failure and sets errno
3916 int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list)
3918 struct sdp_transaction *t;
3919 sdp_pdu_hdr_t *reqhdr;
3921 int cstate_len, seqlen = 0;
3923 if (!session || !session->priv)
3928 /* clean possible allocated buffer */
3929 free(t->rsp_concat_buf.data);
3930 memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t));
3933 t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
3939 memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE);
3941 reqhdr = (sdp_pdu_hdr_t *) t->reqbuf;
3942 reqhdr->tid = htons(sdp_gen_tid(session));
3943 reqhdr->pdu_id = SDP_SVC_ATTR_REQ;
3946 pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t);
3947 t->reqsize = sizeof(sdp_pdu_hdr_t);
3949 /* add the service record handle */
3950 bt_put_be32(handle, pdata);
3951 t->reqsize += sizeof(uint32_t);
3952 pdata += sizeof(uint32_t);
3954 /* specify the response limit */
3955 bt_put_be16(65535, pdata);
3956 t->reqsize += sizeof(uint16_t);
3957 pdata += sizeof(uint16_t);
3959 /* get attr seq PDU form */
3960 seqlen = gen_attridseq_pdu(pdata, attrid_list,
3961 reqtype == SDP_ATTR_REQ_INDIVIDUAL? SDP_UINT16 : SDP_UINT32);
3967 /* now set the length and increment the pointer */
3968 t->reqsize += seqlen;
3970 SDPDBG("Attr list length : %d", seqlen);
3972 /* set the request header's param length */
3973 cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL);
3974 reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t));
3976 if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) {
3977 SDPERR("Error sending data:%m");
3992 * This function starts an asynchronous service search attributes.
3993 * It is a service search request combined with attribute request. The incoming
3994 * and outgoing data are stored in the transaction structure buffers. When there
3995 * is incoming data the sdp_process function must be called to get the data
3996 * and handle the continuation state.
3999 * sdp_session_t *session
4000 * Current sdp session to be handled
4002 * sdp_list_t *search
4003 * Singly linked list containing elements of the search
4004 * pattern. Each entry in the list is a UUID(DataTypeSDP_UUID16)
4005 * of the service to be searched
4007 * AttributeSpecification attrSpec
4008 * Attribute identifiers are 16 bit unsigned integers specified
4009 * in one of 2 ways described below :
4010 * SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
4011 * They are the actual attribute identifiers in ascending order
4013 * SDP_ATTR_REQ_RANGE - 32bit identifier range
4014 * The high-order 16bits is the start of range
4015 * the low-order 16bits are the end of range
4016 * 0x0000 to 0xFFFF gets all attributes
4018 * sdp_list_t *attrid_list
4019 * Singly linked list containing attribute identifiers desired.
4020 * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
4021 * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
4025 * 0 - if the request has been sent properly
4026 * -1 - On any failure
4028 int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *search, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list)
4030 struct sdp_transaction *t;
4031 sdp_pdu_hdr_t *reqhdr;
4033 int cstate_len, seqlen = 0;
4035 if (!session || !session->priv)
4040 /* clean possible allocated buffer */
4041 free(t->rsp_concat_buf.data);
4042 memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t));
4045 t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
4051 memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE);
4053 reqhdr = (sdp_pdu_hdr_t *) t->reqbuf;
4054 reqhdr->tid = htons(sdp_gen_tid(session));
4055 reqhdr->pdu_id = SDP_SVC_SEARCH_ATTR_REQ;
4058 pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t);
4059 t->reqsize = sizeof(sdp_pdu_hdr_t);
4061 /* add service class IDs for search */
4062 seqlen = gen_searchseq_pdu(pdata, search);
4064 SDPDBG("Data seq added : %d", seqlen);
4066 /* now set the length and increment the pointer */
4067 t->reqsize += seqlen;
4070 bt_put_be16(SDP_MAX_ATTR_LEN, pdata);
4071 t->reqsize += sizeof(uint16_t);
4072 pdata += sizeof(uint16_t);
4074 SDPDBG("Max attr byte count : %d", SDP_MAX_ATTR_LEN);
4076 /* get attr seq PDU form */
4077 seqlen = gen_attridseq_pdu(pdata, attrid_list,
4078 reqtype == SDP_ATTR_REQ_INDIVIDUAL ? SDP_UINT16 : SDP_UINT32);
4085 SDPDBG("Attr list length : %d", seqlen);
4086 t->reqsize += seqlen;
4088 /* set the request header's param length */
4089 cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL);
4090 reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t));
4092 if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) {
4093 SDPERR("Error sending data:%m");
4108 * Function used to get the error reason after sdp_callback_t function has been called
4109 * and the status is 0xffff or if sdp_service_{search, attr, search_attr}_async returns -1.
4110 * It indicates that an error NOT related to SDP_ErrorResponse happened. Get errno directly
4111 * is not safe because multiple transactions can be triggered.
4112 * This function must be used with asynchronous sdp functions only.
4115 * sdp_session_t *session
4116 * Current sdp session to be handled
4118 * 0 = No error in the current transaction
4119 * -1 - if the session is invalid
4120 * positive value - the errno value
4123 int sdp_get_error(sdp_session_t *session)
4125 struct sdp_transaction *t;
4127 if (!session || !session->priv) {
4128 SDPERR("Invalid session");
4138 * Receive the incoming SDP PDU. This function must be called when there is data
4139 * available to be read. On continuation state, the original request (with a new
4140 * transaction ID) and the continuation state data will be appended in the initial PDU.
4141 * If an error happens or the transaction finishes the callback function will be called.
4144 * sdp_session_t *session
4145 * Current sdp session to be handled
4147 * 0 - if the transaction is on continuation state
4148 * -1 - On any failure or the transaction finished
4150 int sdp_process(sdp_session_t *session)
4152 struct sdp_transaction *t;
4153 sdp_pdu_hdr_t *reqhdr, *rsphdr;
4154 sdp_cstate_t *pcstate;
4155 uint8_t *pdata, *rspbuf, *targetPtr;
4156 int rsp_count, err = -1;
4159 uint16_t status = 0xffff;
4160 uint8_t pdu_id = 0x00;
4162 if (!session || !session->priv) {
4163 SDPERR("Invalid session");
4167 rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
4169 SDPERR("Response buffer alloc failure:%m (%d)", errno);
4173 memset(rspbuf, 0, SDP_RSP_BUFFER_SIZE);
4176 reqhdr = (sdp_pdu_hdr_t *)t->reqbuf;
4177 rsphdr = (sdp_pdu_hdr_t *)rspbuf;
4179 pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
4181 n = sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE);
4183 SDPERR("Read response:%m (%d)", errno);
4188 if (reqhdr->tid != rsphdr->tid) {
4190 SDPERR("Protocol error: transaction id does not match");
4194 if (n != (int) (ntohs(rsphdr->plen) + sizeof(sdp_pdu_hdr_t))) {
4196 SDPERR("Protocol error: invalid length");
4200 pdu_id = rsphdr->pdu_id;
4201 switch (rsphdr->pdu_id) {
4203 uint16_t tsrc, csrc;
4204 case SDP_SVC_SEARCH_RSP:
4206 * TSRC: Total Service Record Count (2 bytes)
4207 * CSRC: Current Service Record Count (2 bytes)
4210 tsrc = bt_get_be16(ssr_pdata);
4211 ssr_pdata += sizeof(uint16_t);
4212 csrc = bt_get_be16(ssr_pdata);
4214 /* csrc should never be larger than tsrc */
4217 SDPERR("Protocol error: wrong current service record count value.");
4221 SDPDBG("Total svc count: %d", tsrc);
4222 SDPDBG("Current svc count: %d", csrc);
4224 /* parameter length without continuation state */
4225 plen = sizeof(tsrc) + sizeof(csrc) + csrc * 4;
4227 if (t->rsp_concat_buf.data_size == 0) {
4228 /* first fragment */
4229 rsp_count = sizeof(tsrc) + sizeof(csrc) + csrc * 4;
4230 } else if (t->rsp_concat_buf.data_size >= sizeof(uint16_t) * 2) {
4231 /* point to the first csrc */
4232 uint8_t *pcsrc = t->rsp_concat_buf.data + 2;
4233 uint16_t tcsrc, tcsrc2;
4235 /* FIXME: update the interface later. csrc doesn't need be passed to clients */
4237 pdata += sizeof(uint16_t); /* point to csrc */
4239 /* the first csrc contains the sum of partial csrc responses */
4240 memcpy(&tcsrc, pcsrc, sizeof(tcsrc));
4241 memcpy(&tcsrc2, pdata, sizeof(tcsrc2));
4243 memcpy(pcsrc, &tcsrc, sizeof(tcsrc));
4245 pdata += sizeof(uint16_t); /* point to the first handle */
4246 rsp_count = csrc * 4;
4249 SDPERR("Protocol error: invalid PDU size");
4250 status = SDP_INVALID_PDU_SIZE;
4255 case SDP_SVC_ATTR_RSP:
4256 case SDP_SVC_SEARCH_ATTR_RSP:
4257 rsp_count = bt_get_be16(pdata);
4258 SDPDBG("Attrlist byte count : %d", rsp_count);
4260 /* Valid range for rsp_count is 0x0002-0xFFFF */
4261 if (t->rsp_concat_buf.data_size == 0 && rsp_count < 0x0002) {
4263 SDPERR("Protocol error: invalid AttrList size");
4264 status = SDP_INVALID_PDU_SIZE;
4269 * Number of bytes in the AttributeLists parameter(without
4270 * continuation state) + AttributeListsByteCount field size.
4272 plen = sizeof(uint16_t) + rsp_count;
4274 pdata += sizeof(uint16_t); /* points to attribute list */
4278 status = bt_get_be16(pdata);
4279 size = ntohs(rsphdr->plen);
4284 SDPERR("Illegal PDU ID: 0x%x", rsphdr->pdu_id);
4288 /* Out of bound check before using rsp_count as offset for
4289 * continuation state, which has at least a one byte size
4292 if ((n - (int) sizeof(sdp_pdu_hdr_t)) < plen + 1) {
4294 SDPERR("Protocol error: invalid PDU size");
4295 status = SDP_INVALID_PDU_SIZE;
4299 pcstate = (sdp_cstate_t *) (pdata + rsp_count);
4301 SDPDBG("Cstate length : %d", pcstate->length);
4304 * Check out of bound. Continuation state must have at least
4305 * 1 byte: ZERO to indicate that it is not a partial response.
4307 if ((n - (int) sizeof(sdp_pdu_hdr_t)) != (plen + pcstate->length + 1)) {
4309 SDPERR("Protocol error: wrong PDU size.");
4315 * This is a split response, need to concatenate intermediate
4316 * responses and the last one which will have cstate length == 0
4318 t->rsp_concat_buf.data = realloc(t->rsp_concat_buf.data, t->rsp_concat_buf.data_size + rsp_count);
4319 targetPtr = t->rsp_concat_buf.data + t->rsp_concat_buf.data_size;
4320 t->rsp_concat_buf.buf_size = t->rsp_concat_buf.data_size + rsp_count;
4321 memcpy(targetPtr, pdata, rsp_count);
4322 t->rsp_concat_buf.data_size += rsp_count;
4324 if (pcstate->length > 0) {
4325 int reqsize, cstate_len;
4327 reqhdr->tid = htons(sdp_gen_tid(session));
4329 /* add continuation state */
4330 cstate_len = copy_cstate(t->reqbuf + t->reqsize,
4331 SDP_REQ_BUFFER_SIZE - t->reqsize, pcstate);
4333 reqsize = t->reqsize + cstate_len;
4335 /* set the request header's param length */
4336 reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
4338 if (sdp_send_req(session, t->reqbuf, reqsize) < 0) {
4339 SDPERR("Error sending data:%m(%d)", errno);
4349 if (t->rsp_concat_buf.data_size != 0) {
4350 pdata = t->rsp_concat_buf.data;
4351 size = t->rsp_concat_buf.data_size;
4354 t->cb(pdu_id, status, pdata, size, t->udata);
4363 * This is a service search request combined with the service
4364 * attribute request. First a service class match is done and
4365 * for matching service, requested attributes are extracted
4369 * sdp_list_t *search
4370 * Singly linked list containing elements of the search
4371 * pattern. Each entry in the list is a UUID(DataTypeSDP_UUID16)
4372 * of the service to be searched
4374 * AttributeSpecification attrSpec
4375 * Attribute identifiers are 16 bit unsigned integers specified
4376 * in one of 2 ways described below :
4377 * SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
4378 * They are the actual attribute identifiers in ascending order
4380 * SDP_ATTR_REQ_RANGE - 32bit identifier range
4381 * The high-order 16bits is the start of range
4382 * the low-order 16bits are the end of range
4383 * 0x0000 to 0xFFFF gets all attributes
4385 * sdp_list_t *attrids
4386 * Singly linked list containing attribute identifiers desired.
4387 * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
4388 * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
4393 * The request completed successfully. This does not
4394 * mean the requested services were found
4396 * On any error and sets errno
4399 * This variable is set on a successful return to point to
4400 * service(s) found. Each element of this list is of type
4401 * sdp_record_t* (of the services which matched the search list)
4403 int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search, sdp_attrreq_type_t reqtype, const sdp_list_t *attrids, sdp_list_t **rsp)
4406 uint32_t reqsize = 0, _reqsize;
4407 uint32_t rspsize = 0;
4408 int seqlen = 0, attr_list_len = 0;
4409 int rsp_count = 0, cstate_len = 0;
4410 unsigned int pdata_len;
4411 uint8_t *pdata, *_pdata;
4412 uint8_t *reqbuf, *rspbuf;
4413 sdp_pdu_hdr_t *reqhdr, *rsphdr;
4415 sdp_list_t *rec_list = NULL;
4416 sdp_buf_t rsp_concat_buf;
4417 sdp_cstate_t *cstate = NULL;
4419 if (reqtype != SDP_ATTR_REQ_INDIVIDUAL && reqtype != SDP_ATTR_REQ_RANGE) {
4424 memset(&rsp_concat_buf, 0, sizeof(sdp_buf_t));
4426 reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
4427 rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
4428 if (!reqbuf || !rspbuf) {
4434 reqhdr = (sdp_pdu_hdr_t *) reqbuf;
4435 reqhdr->pdu_id = SDP_SVC_SEARCH_ATTR_REQ;
4438 pdata = reqbuf + sizeof(sdp_pdu_hdr_t);
4439 reqsize = sizeof(sdp_pdu_hdr_t);
4441 /* add service class IDs for search */
4442 seqlen = gen_searchseq_pdu(pdata, search);
4449 SDPDBG("Data seq added : %d", seqlen);
4451 /* now set the length and increment the pointer */
4455 bt_put_be16(SDP_MAX_ATTR_LEN, pdata);
4456 reqsize += sizeof(uint16_t);
4457 pdata += sizeof(uint16_t);
4459 SDPDBG("Max attr byte count : %d", SDP_MAX_ATTR_LEN);
4461 /* get attr seq PDU form */
4462 seqlen = gen_attridseq_pdu(pdata, attrids,
4463 reqtype == SDP_ATTR_REQ_INDIVIDUAL ? SDP_UINT16 : SDP_UINT32);
4470 SDPDBG("Attr list length : %d", seqlen);
4474 /* save before Continuation State */
4479 reqhdr->tid = htons(sdp_gen_tid(session));
4481 /* add continuation state (can be null) */
4482 reqsize = _reqsize + copy_cstate(_pdata,
4483 SDP_REQ_BUFFER_SIZE - _reqsize, cstate);
4485 /* set the request header's param length */
4486 reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
4487 rsphdr = (sdp_pdu_hdr_t *) rspbuf;
4488 status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
4489 if (rspsize < sizeof(sdp_pdu_hdr_t)) {
4490 SDPERR("Unexpected end of packet");
4496 SDPDBG("Status : 0x%x", rsphdr->pdu_id);
4500 if (rsphdr->pdu_id == SDP_ERROR_RSP) {
4505 pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
4506 pdata_len = rspsize - sizeof(sdp_pdu_hdr_t);
4508 if (pdata_len < sizeof(uint16_t)) {
4509 SDPERR("Unexpected end of packet");
4514 rsp_count = bt_get_be16(pdata);
4515 attr_list_len += rsp_count;
4516 pdata += sizeof(uint16_t); /* pdata points to attribute list */
4517 pdata_len -= sizeof(uint16_t);
4519 if (pdata_len < rsp_count + sizeof(uint8_t)) {
4520 SDPERR("Unexpected end of packet: continuation state data missing");
4525 cstate_len = *(uint8_t *) (pdata + rsp_count);
4527 SDPDBG("Attrlist byte count : %d", attr_list_len);
4528 SDPDBG("Response byte count : %d", rsp_count);
4529 SDPDBG("Cstate length : %d", cstate_len);
4531 * This is a split response, need to concatenate intermediate
4532 * responses and the last one which will have cstate_len == 0
4534 if (cstate_len > 0 || rsp_concat_buf.data_size != 0) {
4535 uint8_t *targetPtr = NULL;
4537 cstate = cstate_len > 0 ? (sdp_cstate_t *) (pdata + rsp_count) : 0;
4539 /* build concatenated response buffer */
4540 rsp_concat_buf.data = realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count);
4541 targetPtr = rsp_concat_buf.data + rsp_concat_buf.data_size;
4542 rsp_concat_buf.buf_size = rsp_concat_buf.data_size + rsp_count;
4543 memcpy(targetPtr, pdata, rsp_count);
4544 rsp_concat_buf.data_size += rsp_count;
4548 if (attr_list_len > 0) {
4551 if (rsp_concat_buf.data_size != 0) {
4552 pdata = rsp_concat_buf.data;
4553 pdata_len = rsp_concat_buf.data_size;
4557 * Response is a sequence of sequence(s) for one or
4558 * more data element sequence(s) representing services
4559 * for which attributes are returned
4561 scanned = sdp_extract_seqtype(pdata, pdata_len, &dataType, &seqlen);
4563 SDPDBG("Bytes scanned : %d", scanned);
4564 SDPDBG("Seq length : %d", seqlen);
4566 if (scanned && seqlen) {
4568 pdata_len -= scanned;
4571 sdp_record_t *rec = sdp_extract_pdu(pdata, pdata_len, &recsize);
4573 SDPERR("SVC REC is null");
4578 sdp_record_free(rec);
4583 pdata_len -= recsize;
4585 SDPDBG("Loc seq length : %d", recsize);
4586 SDPDBG("Svc Rec Handle : 0x%x", rec->handle);
4587 SDPDBG("Bytes scanned : %d", scanned);
4588 SDPDBG("Attrlist byte count : %d", attr_list_len);
4589 rec_list = sdp_list_append(rec_list, rec);
4590 } while (scanned < attr_list_len && pdata_len > 0);
4592 SDPDBG("Successful scan of service attr lists");
4597 free(rsp_concat_buf.data);
4604 * Find devices in the piconet.
4606 int sdp_general_inquiry(inquiry_info *ii, int num_dev, int duration, uint8_t *found)
4608 int n = hci_inquiry(-1, 10, num_dev, NULL, &ii, 0);
4610 SDPERR("Inquiry failed:%m");
4617 int sdp_close(sdp_session_t *session)
4619 struct sdp_transaction *t;
4625 ret = close(session->sock);
4632 free(t->rsp_concat_buf.data);
4640 static inline int sdp_is_local(const bdaddr_t *device)
4642 return memcmp(device, BDADDR_LOCAL, sizeof(bdaddr_t)) == 0;
4645 static int sdp_connect_local(sdp_session_t *session)
4647 struct sockaddr_un sa;
4649 session->sock = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
4650 if (session->sock < 0)
4654 sa.sun_family = AF_UNIX;
4655 strcpy(sa.sun_path, SDP_UNIX_PATH);
4657 return connect(session->sock, (struct sockaddr *) &sa, sizeof(sa));
4660 static int set_l2cap_mtu(int sk, uint16_t mtu)
4662 struct l2cap_options l2o;
4665 memset(&l2o, 0, sizeof(l2o));
4668 if (getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &len) < 0)
4674 if (setsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, sizeof(l2o)) < 0)
4680 static int sdp_connect_l2cap(const bdaddr_t *src,
4681 const bdaddr_t *dst, sdp_session_t *session)
4683 uint32_t flags = session->flags;
4684 struct sockaddr_l2 sa;
4686 int sockflags = SOCK_SEQPACKET | SOCK_CLOEXEC;
4688 if (flags & SDP_NON_BLOCKING)
4689 sockflags |= SOCK_NONBLOCK;
4691 session->sock = socket(PF_BLUETOOTH, sockflags, BTPROTO_L2CAP);
4692 if (session->sock < 0)
4698 memset(&sa, 0, sizeof(sa));
4700 sa.l2_family = AF_BLUETOOTH;
4703 if (bacmp(src, BDADDR_ANY)) {
4704 sa.l2_bdaddr = *src;
4705 if (bind(sk, (struct sockaddr *) &sa, sizeof(sa)) < 0)
4709 if (flags & SDP_WAIT_ON_CLOSE) {
4710 struct linger l = { .l_onoff = 1, .l_linger = 1 };
4711 setsockopt(sk, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
4714 if ((flags & SDP_LARGE_MTU) &&
4715 set_l2cap_mtu(sk, SDP_LARGE_L2CAP_MTU) < 0)
4718 sa.l2_psm = htobs(SDP_PSM);
4719 sa.l2_bdaddr = *dst;
4722 int ret = connect(sk, (struct sockaddr *) &sa, sizeof(sa));
4725 if (ret < 0 && (flags & SDP_NON_BLOCKING) &&
4726 (errno == EAGAIN || errno == EINPROGRESS))
4728 } while (errno == EBUSY && (flags & SDP_RETRY_IF_BUSY));
4733 sdp_session_t *sdp_connect(const bdaddr_t *src,
4734 const bdaddr_t *dst, uint32_t flags)
4736 sdp_session_t *session;
4739 if ((flags & SDP_RETRY_IF_BUSY) && (flags & SDP_NON_BLOCKING)) {
4744 session = sdp_create(-1, flags);
4748 if (sdp_is_local(dst)) {
4749 if (sdp_connect_local(session) < 0)
4752 if (sdp_connect_l2cap(src, dst, session) < 0)
4760 if (session->sock >= 0)
4761 close(session->sock);
4762 free(session->priv);
4769 int sdp_get_socket(const sdp_session_t *session)
4771 return session->sock;
4774 uint16_t sdp_gen_tid(sdp_session_t *session)
4776 return session->tid++;
4780 * Set the supported features
4782 int sdp_set_supp_feat(sdp_record_t *rec, const sdp_list_t *sf)
4784 const sdp_list_t *p, *r;
4785 sdp_data_t *feat, *seq_feat;
4787 void **seqDTDs, **seqVals;
4789 seqlen = sdp_list_len(sf);
4790 seqDTDs = malloc(seqlen * sizeof(void *));
4793 seqVals = malloc(seqlen * sizeof(void *));
4799 for (p = sf, i = 0; p; p = p->next, i++) {
4801 void **dtds, **vals;
4804 plen = sdp_list_len(p->data);
4805 dtds = malloc(plen * sizeof(void *));
4808 vals = malloc(plen * sizeof(void *));
4813 lengths = malloc(plen * sizeof(int));
4819 for (r = p->data, j = 0; r; r = r->next, j++) {
4820 sdp_data_t *data = (sdp_data_t *) r->data;
4821 dtds[j] = &data->dtd;
4822 switch (data->dtd) {
4826 case SDP_TEXT_STR16:
4827 vals[j] = data->val.str;
4828 lengths[j] = data->unitSize - sizeof(uint8_t);
4836 vals[j] = data->val.dataseq;
4840 vals[j] = &data->val;
4845 feat = sdp_seq_alloc_with_length(dtds, vals, lengths, plen);
4851 seqDTDs[i] = &feat->dtd;
4854 seq_feat = sdp_seq_alloc(seqDTDs, seqVals, seqlen);
4857 sdp_attr_replace(rec, SDP_ATTR_SUPPORTED_FEATURES_LIST, seq_feat);
4870 * Get the supported features
4871 * If an error occurred -1 is returned and errno is set
4873 int sdp_get_supp_feat(const sdp_record_t *rec, sdp_list_t **seqp)
4875 sdp_data_t *sdpdata, *d;
4879 sdpdata = sdp_data_get(rec, SDP_ATTR_SUPPORTED_FEATURES_LIST);
4881 if (!sdpdata || !SDP_IS_SEQ(sdpdata->dtd))
4882 return sdp_get_uuidseq_attr(rec,
4883 SDP_ATTR_SUPPORTED_FEATURES_LIST, seqp);
4885 for (d = sdpdata->val.dataseq; d; d = d->next) {
4889 if (!SDP_IS_SEQ(d->dtd))
4894 for (dd = d->val.dataseq; dd; dd = dd->next) {
4903 case SDP_TEXT_STR16:
4905 length = dd->unitSize - sizeof(uint8_t);
4913 sdp_list_free(subseq, free);
4917 data = sdp_data_alloc_with_length(dd->dtd, val, length);
4919 subseq = sdp_list_append(subseq, data);
4921 tseq = sdp_list_append(tseq, subseq);
4931 sdp_list_free(tseq, free);
4938 void sdp_add_lang_attr(sdp_record_t *rec)
4940 sdp_lang_attr_t base_lang;
4943 base_lang.code_ISO639 = (0x65 << 8) | 0x6e;
4944 base_lang.encoding = 106;
4945 base_lang.base_offset = SDP_PRIMARY_LANG_BASE;
4947 langs = sdp_list_append(0, &base_lang);
4948 sdp_set_lang_attr(rec, langs);
4949 sdp_list_free(langs, NULL);