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
static sdp_data_t *sdp_copy_seq(sdp_data_t *data);
71
static int sdp_attr_add_new_with_length(sdp_record_t *rec,
72
uint16_t attr, uint8_t dtd, const void *value, uint32_t len);
73
static int sdp_gen_buffer(sdp_buf_t *buf, sdp_data_t *d);
75
/* Message structure. */
81
static struct tupla Protocol[] = {
84
{ RFCOMM_UUID, "RFCOMM" },
86
{ TCS_BIN_UUID, "TCS-BIN" },
87
{ TCS_AT_UUID, "TCS-AT" },
88
{ OBEX_UUID, "OBEX" },
91
{ HTTP_UUID, "HTTP" },
93
{ BNEP_UUID, "BNEP" },
94
{ UPNP_UUID, "UPNP" },
95
{ HIDP_UUID, "HIDP" },
96
{ HCRP_CTRL_UUID, "HCRP-Ctrl" },
97
{ HCRP_DATA_UUID, "HCRP-Data" },
98
{ HCRP_NOTE_UUID, "HCRP-Notify" },
99
{ AVCTP_UUID, "AVCTP" },
100
{ AVDTP_UUID, "AVDTP" },
101
{ CMTP_UUID, "CMTP" },
103
{ MCAP_CTRL_UUID, "MCAP-Ctrl" },
104
{ MCAP_DATA_UUID, "MCAP-Data" },
105
{ L2CAP_UUID, "L2CAP" },
110
static struct tupla ServiceClass[] = {
111
{ SDP_SERVER_SVCLASS_ID, "SDP Server" },
112
{ BROWSE_GRP_DESC_SVCLASS_ID, "Browse Group Descriptor" },
113
{ PUBLIC_BROWSE_GROUP, "Public Browse Group" },
114
{ SERIAL_PORT_SVCLASS_ID, "Serial Port" },
115
{ LAN_ACCESS_SVCLASS_ID, "LAN Access Using PPP" },
116
{ DIALUP_NET_SVCLASS_ID, "Dialup Networking" },
117
{ IRMC_SYNC_SVCLASS_ID, "IrMC Sync" },
118
{ OBEX_OBJPUSH_SVCLASS_ID, "OBEX Object Push" },
119
{ OBEX_FILETRANS_SVCLASS_ID, "OBEX File Transfer" },
120
{ IRMC_SYNC_CMD_SVCLASS_ID, "IrMC Sync Command" },
121
{ HEADSET_SVCLASS_ID, "Headset" },
122
{ CORDLESS_TELEPHONY_SVCLASS_ID, "Cordless Telephony" },
123
{ AUDIO_SOURCE_SVCLASS_ID, "Audio Source" },
124
{ AUDIO_SINK_SVCLASS_ID, "Audio Sink" },
125
{ AV_REMOTE_TARGET_SVCLASS_ID, "AV Remote Target" },
126
{ ADVANCED_AUDIO_SVCLASS_ID, "Advanced Audio" },
127
{ AV_REMOTE_SVCLASS_ID, "AV Remote" },
128
{ VIDEO_CONF_SVCLASS_ID, "Video Conferencing" },
129
{ INTERCOM_SVCLASS_ID, "Intercom" },
130
{ FAX_SVCLASS_ID, "Fax" },
131
{ HEADSET_AGW_SVCLASS_ID, "Headset Audio Gateway" },
132
{ WAP_SVCLASS_ID, "WAP" },
133
{ WAP_CLIENT_SVCLASS_ID, "WAP Client" },
134
{ PANU_SVCLASS_ID, "PAN User" },
135
{ NAP_SVCLASS_ID, "Network Access Point" },
136
{ GN_SVCLASS_ID, "PAN Group Network" },
137
{ DIRECT_PRINTING_SVCLASS_ID, "Direct Printing" },
138
{ REFERENCE_PRINTING_SVCLASS_ID, "Reference Printing" },
139
{ IMAGING_SVCLASS_ID, "Imaging" },
140
{ IMAGING_RESPONDER_SVCLASS_ID, "Imaging Responder" },
141
{ IMAGING_ARCHIVE_SVCLASS_ID, "Imaging Automatic Archive" },
142
{ IMAGING_REFOBJS_SVCLASS_ID, "Imaging Referenced Objects" },
143
{ HANDSFREE_SVCLASS_ID, "Handsfree" },
144
{ HANDSFREE_AGW_SVCLASS_ID, "Handsfree Audio Gateway" },
145
{ DIRECT_PRT_REFOBJS_SVCLASS_ID, "Direct Printing Ref. Objects" },
146
{ REFLECTED_UI_SVCLASS_ID, "Reflected UI" },
147
{ BASIC_PRINTING_SVCLASS_ID, "Basic Printing" },
148
{ PRINTING_STATUS_SVCLASS_ID, "Printing Status" },
149
{ HID_SVCLASS_ID, "Human Interface Device" },
150
{ HCR_SVCLASS_ID, "Hardcopy Cable Replacement" },
151
{ HCR_PRINT_SVCLASS_ID, "HCR Print" },
152
{ HCR_SCAN_SVCLASS_ID, "HCR Scan" },
153
{ CIP_SVCLASS_ID, "Common ISDN Access" },
154
{ VIDEO_CONF_GW_SVCLASS_ID, "Video Conferencing Gateway" },
155
{ UDI_MT_SVCLASS_ID, "UDI MT" },
156
{ UDI_TA_SVCLASS_ID, "UDI TA" },
157
{ AV_SVCLASS_ID, "Audio/Video" },
158
{ SAP_SVCLASS_ID, "SIM Access" },
159
{ PBAP_PCE_SVCLASS_ID, "Phonebook Access - PCE" },
160
{ PBAP_PSE_SVCLASS_ID, "Phonebook Access - PSE" },
161
{ PBAP_SVCLASS_ID, "Phonebook Access" },
162
{ PNP_INFO_SVCLASS_ID, "PnP Information" },
163
{ GENERIC_NETWORKING_SVCLASS_ID, "Generic Networking" },
164
{ GENERIC_FILETRANS_SVCLASS_ID, "Generic File Transfer" },
165
{ GENERIC_AUDIO_SVCLASS_ID, "Generic Audio" },
166
{ GENERIC_TELEPHONY_SVCLASS_ID, "Generic Telephony" },
167
{ UPNP_SVCLASS_ID, "UPnP" },
168
{ UPNP_IP_SVCLASS_ID, "UPnP IP" },
169
{ UPNP_PAN_SVCLASS_ID, "UPnP PAN" },
170
{ UPNP_LAP_SVCLASS_ID, "UPnP LAP" },
171
{ UPNP_L2CAP_SVCLASS_ID, "UPnP L2CAP" },
172
{ VIDEO_SOURCE_SVCLASS_ID, "Video Source" },
173
{ VIDEO_SINK_SVCLASS_ID, "Video Sink" },
174
{ VIDEO_DISTRIBUTION_SVCLASS_ID, "Video Distribution" },
175
{ HDP_SVCLASS_ID, "HDP" },
176
{ HDP_SOURCE_SVCLASS_ID, "HDP Source" },
177
{ HDP_SINK_SVCLASS_ID, "HDP Sink" },
178
{ APPLE_AGENT_SVCLASS_ID, "Apple Agent" },
179
{ GENERIC_ATTRIB_SVCLASS_ID, "Generic Attribute" },
183
#define Profile ServiceClass
185
static char *string_lookup(struct tupla *pt0, int index)
189
for (pt = pt0; pt->index; pt++)
190
if (pt->index == index)
196
static char *string_lookup_uuid(struct tupla *pt0, const uuid_t* uuid)
200
memcpy(&tmp_uuid, uuid, sizeof(tmp_uuid));
202
if (sdp_uuid128_to_uuid(&tmp_uuid)) {
203
switch (tmp_uuid.type) {
205
return string_lookup(pt0, tmp_uuid.value.uuid16);
207
return string_lookup(pt0, tmp_uuid.value.uuid32);
215
* Prints into a string the Protocol UUID
216
* coping a maximum of n characters.
218
static int uuid2str(struct tupla *message, const uuid_t *uuid, char *str, size_t n)
223
snprintf(str, n, "NULL");
227
switch (uuid->type) {
229
str2 = string_lookup(message, uuid->value.uuid16);
230
snprintf(str, n, "%s", str2);
233
str2 = string_lookup(message, uuid->value.uuid32);
234
snprintf(str, n, "%s", str2);
237
str2 = string_lookup_uuid(message, uuid);
238
snprintf(str, n, "%s", str2);
241
snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type);
248
int sdp_proto_uuid2strn(const uuid_t *uuid, char *str, size_t n)
250
return uuid2str(Protocol, uuid, str, n);
253
int sdp_svclass_uuid2strn(const uuid_t *uuid, char *str, size_t n)
255
return uuid2str(ServiceClass, uuid, str, n);
258
int sdp_profile_uuid2strn(const uuid_t *uuid, char *str, size_t n)
260
return uuid2str(Profile, uuid, str, n);
264
* convert the UUID to string, copying a maximum of n characters.
266
int sdp_uuid2strn(const uuid_t *uuid, char *str, size_t n)
269
snprintf(str, n, "NULL");
272
switch (uuid->type) {
274
snprintf(str, n, "%.4x", uuid->value.uuid16);
277
snprintf(str, n, "%.8x", uuid->value.uuid32);
281
unsigned short data1;
282
unsigned short data2;
283
unsigned short data3;
285
unsigned short data5;
287
memcpy(&data0, &uuid->value.uuid128.data[0], 4);
288
memcpy(&data1, &uuid->value.uuid128.data[4], 2);
289
memcpy(&data2, &uuid->value.uuid128.data[6], 2);
290
memcpy(&data3, &uuid->value.uuid128.data[8], 2);
291
memcpy(&data4, &uuid->value.uuid128.data[10], 4);
292
memcpy(&data5, &uuid->value.uuid128.data[14], 2);
294
snprintf(str, n, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
295
ntohl(data0), ntohs(data1),
296
ntohs(data2), ntohs(data3),
297
ntohl(data4), ntohs(data5));
301
snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type);
302
return -1; /* Enum type of UUID not set */
309
* Function prints the UUID in hex as per defined syntax -
311
* 4bytes-2bytes-2bytes-2bytes-6bytes
313
* There is some ugly code, including hardcoding, but
314
* that is just the way it is converting 16 and 32 bit
315
* UUIDs to 128 bit as defined in the SDP doc
317
void sdp_uuid_print(const uuid_t *uuid)
320
SDPERR("Null passed to print UUID\n");
323
if (uuid->type == SDP_UUID16) {
324
SDPDBG(" uint16_t : 0x%.4x\n", uuid->value.uuid16);
325
} else if (uuid->type == SDP_UUID32) {
326
SDPDBG(" uint32_t : 0x%.8x\n", uuid->value.uuid32);
327
} else if (uuid->type == SDP_UUID128) {
329
unsigned short data1;
330
unsigned short data2;
331
unsigned short data3;
333
unsigned short data5;
335
memcpy(&data0, &uuid->value.uuid128.data[0], 4);
336
memcpy(&data1, &uuid->value.uuid128.data[4], 2);
337
memcpy(&data2, &uuid->value.uuid128.data[6], 2);
338
memcpy(&data3, &uuid->value.uuid128.data[8], 2);
339
memcpy(&data4, &uuid->value.uuid128.data[10], 4);
340
memcpy(&data5, &uuid->value.uuid128.data[14], 2);
342
SDPDBG(" uint128_t : 0x%.8x-", ntohl(data0));
343
SDPDBG("%.4x-", ntohs(data1));
344
SDPDBG("%.4x-", ntohs(data2));
345
SDPDBG("%.4x-", ntohs(data3));
346
SDPDBG("%.8x", ntohl(data4));
347
SDPDBG("%.4x\n", ntohs(data5));
349
SDPERR("Enum type of UUID not set\n");
353
sdp_data_t *sdp_data_alloc_with_length(uint8_t dtd, const void *value,
357
sdp_data_t *d = malloc(sizeof(sdp_data_t));
362
memset(d, 0, sizeof(sdp_data_t));
364
d->unitSize = sizeof(uint8_t);
370
d->val.uint8 = *(uint8_t *) value;
371
d->unitSize += sizeof(uint8_t);
375
d->val.int8 = *(int8_t *) value;
376
d->unitSize += sizeof(int8_t);
379
d->val.uint16 = bt_get_unaligned((uint16_t *) value);
380
d->unitSize += sizeof(uint16_t);
383
d->val.int16 = bt_get_unaligned((int16_t *) value);
384
d->unitSize += sizeof(int16_t);
387
d->val.uint32 = bt_get_unaligned((uint32_t *) value);
388
d->unitSize += sizeof(uint32_t);
391
d->val.int32 = bt_get_unaligned((int32_t *) value);
392
d->unitSize += sizeof(int32_t);
395
d->val.int64 = bt_get_unaligned((int64_t *) value);
396
d->unitSize += sizeof(int64_t);
399
d->val.uint64 = bt_get_unaligned((uint64_t *) value);
400
d->unitSize += sizeof(uint64_t);
403
memcpy(&d->val.uint128.data, value, sizeof(uint128_t));
404
d->unitSize += sizeof(uint128_t);
407
memcpy(&d->val.int128.data, value, sizeof(uint128_t));
408
d->unitSize += sizeof(uint128_t);
411
sdp_uuid16_create(&d->val.uuid, bt_get_unaligned((uint16_t *) value));
412
d->unitSize += sizeof(uint16_t);
415
sdp_uuid32_create(&d->val.uuid, bt_get_unaligned((uint32_t *) value));
416
d->unitSize += sizeof(uint32_t);
419
sdp_uuid128_create(&d->val.uuid, value);
420
d->unitSize += sizeof(uint128_t);
431
d->unitSize += length;
432
if (length <= USHRT_MAX) {
433
d->val.str = malloc(length);
439
memcpy(d->val.str, value, length);
441
SDPERR("Strings of size > USHRT_MAX not supported\n");
448
SDPERR("Strings of size > USHRT_MAX not supported\n");
456
if (dtd == SDP_ALT8 || dtd == SDP_SEQ8)
457
d->unitSize += sizeof(uint8_t);
458
else if (dtd == SDP_ALT16 || dtd == SDP_SEQ16)
459
d->unitSize += sizeof(uint16_t);
460
else if (dtd == SDP_ALT32 || dtd == SDP_SEQ32)
461
d->unitSize += sizeof(uint32_t);
462
seq = (sdp_data_t *)value;
463
d->val.dataseq = seq;
464
for (; seq; seq = seq->next)
465
d->unitSize += seq->unitSize;
475
sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value)
487
length = strlen((char *) value);
494
return sdp_data_alloc_with_length(dtd, value, length);
497
sdp_data_t *sdp_seq_append(sdp_data_t *seq, sdp_data_t *d)
501
for (p = seq; p->next; p = p->next);
509
sdp_data_t *sdp_seq_alloc_with_length(void **dtds, void **values, int *length,
512
sdp_data_t *curr = NULL, *seq = NULL;
515
for (i = 0; i < len; i++) {
517
int8_t dtd = *(uint8_t *) dtds[i];
519
if (dtd >= SDP_SEQ8 && dtd <= SDP_ALT32)
520
data = (sdp_data_t *) values[i];
522
data = sdp_data_alloc_with_length(dtd, values[i], length[i]);
535
return sdp_data_alloc_with_length(SDP_SEQ8, seq, length[i]);
538
sdp_data_t *sdp_seq_alloc(void **dtds, void **values, int len)
540
sdp_data_t *curr = NULL, *seq = NULL;
543
for (i = 0; i < len; i++) {
545
uint8_t dtd = *(uint8_t *) dtds[i];
547
if (dtd >= SDP_SEQ8 && dtd <= SDP_ALT32)
548
data = (sdp_data_t *) values[i];
550
data = sdp_data_alloc(dtd, values[i]);
563
return sdp_data_alloc(SDP_SEQ8, seq);
566
static void extract_svclass_uuid(sdp_data_t *data, uuid_t *uuid)
570
if (!data || data->dtd < SDP_SEQ8 || data->dtd > SDP_SEQ32)
573
d = data->val.dataseq;
577
if (d->dtd < SDP_UUID16 || d->dtd > SDP_UUID128)
583
int sdp_attr_add(sdp_record_t *rec, uint16_t attr, sdp_data_t *d)
585
sdp_data_t *p = sdp_data_get(rec, attr);
591
rec->attrlist = sdp_list_insert_sorted(rec->attrlist, d, sdp_attrid_comp_func);
593
if (attr == SDP_ATTR_SVCLASS_ID_LIST)
594
extract_svclass_uuid(d, &rec->svclass);
599
void sdp_attr_remove(sdp_record_t *rec, uint16_t attr)
601
sdp_data_t *d = sdp_data_get(rec, attr);
604
rec->attrlist = sdp_list_remove(rec->attrlist, d);
606
if (attr == SDP_ATTR_SVCLASS_ID_LIST)
607
memset(&rec->svclass, 0, sizeof(rec->svclass));
610
void sdp_set_seq_len(uint8_t *ptr, uint32_t length)
612
uint8_t dtd = *ptr++;
619
*ptr = (uint8_t) length;
625
bt_put_unaligned(htons(length), (uint16_t *) ptr);
631
bt_put_unaligned(htonl(length), (uint32_t *) ptr);
636
static int sdp_get_data_type(sdp_buf_t *buf, uint8_t dtd)
640
data_type += sizeof(uint8_t);
647
data_type += sizeof(uint8_t);
653
data_type += sizeof(uint16_t);
659
data_type += sizeof(uint32_t);
664
buf->buf_size += data_type;
669
static int sdp_set_data_type(sdp_buf_t *buf, uint8_t dtd)
672
uint8_t *p = buf->data + buf->data_size;
675
data_type = sdp_get_data_type(buf, dtd);
676
buf->data_size += data_type;
681
void sdp_set_attrid(sdp_buf_t *buf, uint16_t attr)
683
uint8_t *p = buf->data;
685
/* data type for attr */
687
buf->data_size = sizeof(uint8_t);
688
bt_put_unaligned(htons(attr), (uint16_t *) p);
689
buf->data_size += sizeof(uint16_t);
692
static int get_data_size(sdp_buf_t *buf, sdp_data_t *sdpdata)
697
for (d = sdpdata->val.dataseq; d; d = d->next) {
699
n += sdp_gen_pdu(buf, d);
701
n += sdp_gen_buffer(buf, d);
707
static int sdp_get_data_size(sdp_buf_t *buf, sdp_data_t *d)
709
uint32_t data_size = 0;
710
uint8_t dtd = d->dtd;
716
data_size = sizeof(uint8_t);
719
data_size = sizeof(uint16_t);
722
data_size = sizeof(uint32_t);
725
data_size = sizeof(uint64_t);
728
data_size = sizeof(uint128_t);
732
data_size = sizeof(int8_t);
735
data_size = sizeof(int16_t);
738
data_size = sizeof(int32_t);
741
data_size = sizeof(int64_t);
744
data_size = sizeof(uint128_t);
752
data_size = d->unitSize - sizeof(uint8_t);
757
data_size = get_data_size(buf, d);
762
data_size = get_data_size(buf, d);
765
data_size = sizeof(uint16_t);
768
data_size = sizeof(uint32_t);
771
data_size = sizeof(uint128_t);
778
buf->buf_size += data_size;
783
static int sdp_gen_buffer(sdp_buf_t *buf, sdp_data_t *d)
785
int orig = buf->buf_size;
787
if (buf->buf_size == 0 && d->dtd == 0) {
788
/* create initial sequence */
789
buf->buf_size += sizeof(uint8_t);
791
/* reserve space for sequence size */
792
buf->buf_size += sizeof(uint8_t);
795
/* attribute length */
796
buf->buf_size += sizeof(uint8_t) + sizeof(uint16_t);
798
sdp_get_data_type(buf, d->dtd);
799
sdp_get_data_size(buf, d);
801
if (buf->buf_size > UCHAR_MAX && d->dtd == SDP_SEQ8)
802
buf->buf_size += sizeof(uint8_t);
804
return buf->buf_size - orig;
807
int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d)
809
uint32_t pdu_size = 0, data_size = 0;
810
unsigned char *src = NULL, is_seq = 0, is_alt = 0;
811
uint8_t dtd = d->dtd;
816
uint8_t *seqp = buf->data + buf->data_size;
818
pdu_size = sdp_set_data_type(buf, dtd);
819
data_size = sdp_get_data_size(buf, d);
828
u16 = htons(d->val.uint16);
829
src = (unsigned char *) &u16;
832
u32 = htonl(d->val.uint32);
833
src = (unsigned char *) &u32;
836
u64 = hton64(d->val.uint64);
837
src = (unsigned char *) &u64;
840
hton128(&d->val.uint128, &u128);
841
src = (unsigned char *) &u128;
845
src = (unsigned char *) &d->val.int8;
848
u16 = htons(d->val.int16);
849
src = (unsigned char *) &u16;
852
u32 = htonl(d->val.int32);
853
src = (unsigned char *) &u32;
856
u64 = hton64(d->val.int64);
857
src = (unsigned char *) &u64;
860
hton128(&d->val.int128, &u128);
861
src = (unsigned char *) &u128;
869
src = (unsigned char *) d->val.str;
870
sdp_set_seq_len(seqp, data_size);
876
sdp_set_seq_len(seqp, data_size);
882
sdp_set_seq_len(seqp, data_size);
885
u16 = htons(d->val.uuid.value.uuid16);
886
src = (unsigned char *) &u16;
889
u32 = htonl(d->val.uuid.value.uuid32);
890
src = (unsigned char *) &u32;
893
src = (unsigned char *) &d->val.uuid.value.uuid128;
899
if (!is_seq && !is_alt) {
900
if (src && buf->buf_size >= buf->data_size + data_size) {
901
memcpy(buf->data + buf->data_size, src, data_size);
902
buf->data_size += data_size;
903
} else if (dtd != SDP_DATA_NIL) {
904
SDPDBG("Gen PDU : Can't copy from invalid source or dest\n");
908
pdu_size += data_size;
913
static void sdp_attr_pdu(void *value, void *udata)
915
sdp_append_to_pdu((sdp_buf_t *)udata, (sdp_data_t *)value);
918
static void sdp_attr_size(void *value, void *udata)
920
sdp_gen_buffer((sdp_buf_t *)udata, (sdp_data_t *)value);
923
int sdp_gen_record_pdu(const sdp_record_t *rec, sdp_buf_t *buf)
925
memset(buf, 0, sizeof(sdp_buf_t));
926
sdp_list_foreach(rec->attrlist, sdp_attr_size, buf);
928
buf->data = malloc(buf->buf_size);
932
memset(buf->data, 0, buf->buf_size);
934
sdp_list_foreach(rec->attrlist, sdp_attr_pdu, buf);
939
void sdp_attr_replace(sdp_record_t *rec, uint16_t attr, sdp_data_t *d)
941
sdp_data_t *p = sdp_data_get(rec, attr);
944
rec->attrlist = sdp_list_remove(rec->attrlist, p);
949
rec->attrlist = sdp_list_insert_sorted(rec->attrlist, d, sdp_attrid_comp_func);
951
if (attr == SDP_ATTR_SVCLASS_ID_LIST)
952
extract_svclass_uuid(d, &rec->svclass);
955
int sdp_attrid_comp_func(const void *key1, const void *key2)
957
const sdp_data_t *d1 = (const sdp_data_t *)key1;
958
const sdp_data_t *d2 = (const sdp_data_t *)key2;
961
return d1->attrId - d2->attrId;
965
static void data_seq_free(sdp_data_t *seq)
967
sdp_data_t *d = seq->val.dataseq;
970
sdp_data_t *next = d->next;
976
void sdp_data_free(sdp_data_t *d)
996
int sdp_uuid_extract(const uint8_t *p, int bufsize, uuid_t *uuid, int *scanned)
1000
if (bufsize < (int) sizeof(uint8_t)) {
1001
SDPERR("Unexpected end of packet");
1005
type = *(const uint8_t *) p;
1007
if (!SDP_IS_UUID(type)) {
1008
SDPERR("Unknown data type : %d expecting a svc UUID\n", type);
1011
p += sizeof(uint8_t);
1012
*scanned += sizeof(uint8_t);
1013
bufsize -= sizeof(uint8_t);
1014
if (type == SDP_UUID16) {
1015
if (bufsize < (int) sizeof(uint16_t)) {
1016
SDPERR("Not enough room for 16-bit UUID");
1019
sdp_uuid16_create(uuid, ntohs(bt_get_unaligned((uint16_t *) p)));
1020
*scanned += sizeof(uint16_t);
1021
} else if (type == SDP_UUID32) {
1022
if (bufsize < (int) sizeof(uint32_t)) {
1023
SDPERR("Not enough room for 32-bit UUID");
1026
sdp_uuid32_create(uuid, ntohl(bt_get_unaligned((uint32_t *) p)));
1027
*scanned += sizeof(uint32_t);
1029
if (bufsize < (int) sizeof(uint128_t)) {
1030
SDPERR("Not enough room for 128-bit UUID");
1033
sdp_uuid128_create(uuid, p);
1034
*scanned += sizeof(uint128_t);
1039
static sdp_data_t *extract_int(const void *p, int bufsize, int *len)
1043
if (bufsize < (int) sizeof(uint8_t)) {
1044
SDPERR("Unexpected end of packet");
1048
d = malloc(sizeof(sdp_data_t));
1052
SDPDBG("Extracting integer\n");
1053
memset(d, 0, sizeof(sdp_data_t));
1054
d->dtd = *(uint8_t *) p;
1055
p += sizeof(uint8_t);
1056
*len += sizeof(uint8_t);
1057
bufsize -= sizeof(uint8_t);
1065
if (bufsize < (int) sizeof(uint8_t)) {
1066
SDPERR("Unexpected end of packet");
1070
*len += sizeof(uint8_t);
1071
d->val.uint8 = *(uint8_t *) p;
1075
if (bufsize < (int) sizeof(uint16_t)) {
1076
SDPERR("Unexpected end of packet");
1080
*len += sizeof(uint16_t);
1081
d->val.uint16 = ntohs(bt_get_unaligned((uint16_t *) p));
1085
if (bufsize < (int) sizeof(uint32_t)) {
1086
SDPERR("Unexpected end of packet");
1090
*len += sizeof(uint32_t);
1091
d->val.uint32 = ntohl(bt_get_unaligned((uint32_t *) p));
1095
if (bufsize < (int) sizeof(uint64_t)) {
1096
SDPERR("Unexpected end of packet");
1100
*len += sizeof(uint64_t);
1101
d->val.uint64 = ntoh64(bt_get_unaligned((uint64_t *) p));
1105
if (bufsize < (int) sizeof(uint128_t)) {
1106
SDPERR("Unexpected end of packet");
1110
*len += sizeof(uint128_t);
1111
ntoh128((uint128_t *) p, &d->val.uint128);
1120
static sdp_data_t *extract_uuid(const uint8_t *p, int bufsize, int *len,
1123
sdp_data_t *d = malloc(sizeof(sdp_data_t));
1128
SDPDBG("Extracting UUID");
1129
memset(d, 0, sizeof(sdp_data_t));
1130
if (sdp_uuid_extract(p, bufsize, &d->val.uuid, len) < 0) {
1136
sdp_pattern_add_uuid(rec, &d->val.uuid);
1141
* Extract strings from the PDU (could be service description and similar info)
1143
static sdp_data_t *extract_str(const void *p, int bufsize, int *len)
1149
if (bufsize < (int) sizeof(uint8_t)) {
1150
SDPERR("Unexpected end of packet");
1154
d = malloc(sizeof(sdp_data_t));
1158
memset(d, 0, sizeof(sdp_data_t));
1159
d->dtd = *(uint8_t *) p;
1160
p += sizeof(uint8_t);
1161
*len += sizeof(uint8_t);
1162
bufsize -= sizeof(uint8_t);
1167
if (bufsize < (int) sizeof(uint8_t)) {
1168
SDPERR("Unexpected end of packet");
1173
p += sizeof(uint8_t);
1174
*len += sizeof(uint8_t);
1175
bufsize -= sizeof(uint8_t);
1177
case SDP_TEXT_STR16:
1179
if (bufsize < (int) sizeof(uint16_t)) {
1180
SDPERR("Unexpected end of packet");
1184
n = ntohs(bt_get_unaligned((uint16_t *) p));
1185
p += sizeof(uint16_t);
1186
*len += sizeof(uint16_t) + n;
1187
bufsize -= sizeof(uint16_t);
1190
SDPERR("Sizeof text string > UINT16_MAX\n");
1196
SDPERR("String too long to fit in packet");
1203
SDPERR("Not enough memory for incoming string");
1207
memset(s, 0, n + 1);
1212
SDPDBG("Len : %d\n", n);
1213
SDPDBG("Str : %s\n", s);
1216
d->unitSize = n + sizeof(uint8_t);
1221
* Extract the sequence type and its length, and return offset into buf
1224
int sdp_extract_seqtype(const uint8_t *buf, int bufsize, uint8_t *dtdp, int *size)
1227
int scanned = sizeof(uint8_t);
1229
if (bufsize < (int) sizeof(uint8_t)) {
1230
SDPERR("Unexpected end of packet");
1234
dtd = *(uint8_t *) buf;
1235
buf += sizeof(uint8_t);
1236
bufsize -= sizeof(uint8_t);
1241
if (bufsize < (int) sizeof(uint8_t)) {
1242
SDPERR("Unexpected end of packet");
1245
*size = *(uint8_t *) buf;
1246
scanned += sizeof(uint8_t);
1250
if (bufsize < (int) sizeof(uint16_t)) {
1251
SDPERR("Unexpected end of packet");
1254
*size = ntohs(bt_get_unaligned((uint16_t *) buf));
1255
scanned += sizeof(uint16_t);
1259
if (bufsize < (int) sizeof(uint32_t)) {
1260
SDPERR("Unexpected end of packet");
1263
*size = ntohl(bt_get_unaligned((uint32_t *) buf));
1264
scanned += sizeof(uint32_t);
1267
SDPERR("Unknown sequence type, aborting\n");
1273
static sdp_data_t *extract_seq(const void *p, int bufsize, int *len,
1277
sdp_data_t *curr, *prev;
1278
sdp_data_t *d = malloc(sizeof(sdp_data_t));
1283
SDPDBG("Extracting SEQ");
1284
memset(d, 0, sizeof(sdp_data_t));
1285
*len = sdp_extract_seqtype(p, bufsize, &d->dtd, &seqlen);
1286
SDPDBG("Sequence Type : 0x%x length : 0x%x\n", d->dtd, seqlen);
1291
if (*len > bufsize) {
1292
SDPERR("Packet not big enough to hold sequence.");
1300
while (n < seqlen) {
1302
curr = sdp_extract_attr(p, bufsize, &attrlen, rec);
1309
d->val.dataseq = curr;
1315
SDPDBG("Extracted: %d SequenceLength: %d", n, seqlen);
1322
sdp_data_t *sdp_extract_attr(const uint8_t *p, int bufsize, int *size,
1329
if (bufsize < (int) sizeof(uint8_t)) {
1330
SDPERR("Unexpected end of packet");
1334
dtd = *(const uint8_t *)p;
1336
SDPDBG("extract_attr: dtd=0x%x", dtd);
1350
elem = extract_int(p, bufsize, &n);
1355
elem = extract_uuid(p, bufsize, &n, rec);
1358
case SDP_TEXT_STR16:
1359
case SDP_TEXT_STR32:
1363
elem = extract_str(p, bufsize, &n);
1371
elem = extract_seq(p, bufsize, &n, rec);
1374
SDPERR("Unknown data descriptor : 0x%x terminating\n", dtd);
1382
static void attr_print_func(void *value, void *userData)
1384
sdp_data_t *d = (sdp_data_t *)value;
1386
SDPDBG("=====================================\n");
1387
SDPDBG("ATTRIBUTE IDENTIFIER : 0x%x\n", d->attrId);
1388
SDPDBG("ATTRIBUTE VALUE PTR : %p\n", value);
1392
SDPDBG("NULL value\n");
1393
SDPDBG("=====================================\n");
1396
void sdp_print_service_attr(sdp_list_t *svcAttrList)
1398
SDPDBG("Printing service attr list %p\n", svcAttrList);
1399
sdp_list_foreach(svcAttrList, attr_print_func, NULL);
1400
SDPDBG("Printed service attr list %p\n", svcAttrList);
1404
sdp_record_t *sdp_extract_pdu(const uint8_t *buf, int bufsize, int *scanned)
1406
int extracted = 0, seqlen = 0;
1409
sdp_record_t *rec = sdp_record_alloc();
1410
const uint8_t *p = buf;
1412
*scanned = sdp_extract_seqtype(buf, bufsize, &dtd, &seqlen);
1414
bufsize -= *scanned;
1415
rec->attrlist = NULL;
1417
while (extracted < seqlen && bufsize > 0) {
1418
int n = sizeof(uint8_t), attrlen = 0;
1419
sdp_data_t *data = NULL;
1421
SDPDBG("Extract PDU, sequenceLength: %d localExtractedLength: %d",
1424
if (bufsize < n + (int) sizeof(uint16_t)) {
1425
SDPERR("Unexpected end of packet");
1429
dtd = *(uint8_t *) p;
1430
attr = ntohs(bt_get_unaligned((uint16_t *) (p + n)));
1431
n += sizeof(uint16_t);
1433
SDPDBG("DTD of attrId : %d Attr id : 0x%x \n", dtd, attr);
1435
data = sdp_extract_attr(p + n, bufsize - n, &attrlen, rec);
1437
SDPDBG("Attr id : 0x%x attrValueLength : %d\n", attr, attrlen);
1441
SDPDBG("Terminating extraction of attributes");
1445
if (attr == SDP_ATTR_RECORD_HANDLE)
1446
rec->handle = data->val.uint32;
1448
if (attr == SDP_ATTR_SVCLASS_ID_LIST)
1449
extract_svclass_uuid(data, &rec->svclass);
1454
sdp_attr_replace(rec, attr, data);
1456
SDPDBG("Extract PDU, seqLength: %d localExtractedLength: %d",
1460
SDPDBG("Successful extracting of Svc Rec attributes\n");
1461
sdp_print_service_attr(rec->attrlist);
1467
static void sdp_copy_pattern(void *value, void *udata)
1469
uuid_t *uuid = value;
1470
sdp_record_t *rec = udata;
1472
sdp_pattern_add_uuid(rec, uuid);
1475
static void *sdp_data_value(sdp_data_t *data, uint32_t *len)
1479
switch (data->dtd) {
1483
val = &data->val.uint8;
1487
val = &data->val.int8;
1490
val = &data->val.uint16;
1493
val = &data->val.int16;
1496
val = &data->val.uint32;
1499
val = &data->val.int32;
1502
val = &data->val.int64;
1505
val = &data->val.uint64;
1508
val = &data->val.uint128;
1511
val = &data->val.int128;
1514
val = &data->val.uuid.value.uuid16;
1517
val = &data->val.uuid.value.uuid32;
1520
val = &data->val.uuid.value.uuid128;
1525
case SDP_TEXT_STR16:
1527
case SDP_TEXT_STR32:
1528
val = data->val.str;
1530
*len = data->unitSize - 1;
1538
val = sdp_copy_seq(data->val.dataseq);
1545
static sdp_data_t *sdp_copy_seq(sdp_data_t *data)
1547
sdp_data_t *tmp, *seq = NULL, *cur = NULL;
1549
for (tmp = data; tmp; tmp = tmp->next) {
1550
sdp_data_t *datatmp;
1553
value = sdp_data_value(tmp, NULL);
1554
datatmp = sdp_data_alloc_with_length(tmp->dtd, value,
1558
cur->next = datatmp;
1568
static void sdp_copy_attrlist(void *value, void *udata)
1570
sdp_data_t *data = value;
1571
sdp_record_t *rec = udata;
1575
val = sdp_data_value(data, &len);
1578
sdp_attr_add_new(rec, data->attrId, data->dtd, val);
1580
sdp_attr_add_new_with_length(rec, data->attrId,
1581
data->dtd, val, len);
1584
sdp_record_t *sdp_copy_record(sdp_record_t *rec)
1588
cpy = sdp_record_alloc();
1590
cpy->handle = rec->handle;
1592
sdp_list_foreach(rec->pattern, sdp_copy_pattern, cpy);
1593
sdp_list_foreach(rec->attrlist, sdp_copy_attrlist, cpy);
1595
cpy->svclass = rec->svclass;
1601
static void print_dataseq(sdp_data_t *p)
1605
for (d = p; d; d = d->next)
1610
void sdp_record_print(const sdp_record_t *rec)
1612
sdp_data_t *d = sdp_data_get(rec, SDP_ATTR_SVCNAME_PRIMARY);
1614
printf("Service Name: %.*s\n", d->unitSize, d->val.str);
1615
d = sdp_data_get(rec, SDP_ATTR_SVCDESC_PRIMARY);
1617
printf("Service Description: %.*s\n", d->unitSize, d->val.str);
1618
d = sdp_data_get(rec, SDP_ATTR_PROVNAME_PRIMARY);
1620
printf("Service Provider: %.*s\n", d->unitSize, d->val.str);
1624
void sdp_data_print(sdp_data_t *d)
1641
SDPDBG("Integer : 0x%x\n", d->val.uint32);
1647
sdp_uuid_print(&d->val.uuid);
1650
case SDP_TEXT_STR16:
1651
case SDP_TEXT_STR32:
1652
SDPDBG("Text : %s\n", d->val.str);
1657
SDPDBG("URL : %s\n", d->val.str);
1662
print_dataseq(d->val.dataseq);
1667
SDPDBG("Data Sequence Alternates\n");
1668
print_dataseq(d->val.dataseq);
1674
sdp_data_t *sdp_data_get(const sdp_record_t *rec, uint16_t attrId)
1676
if (rec->attrlist) {
1677
sdp_data_t sdpTemplate;
1680
sdpTemplate.attrId = attrId;
1681
p = sdp_list_find(rec->attrlist, &sdpTemplate, sdp_attrid_comp_func);
1688
static int sdp_send_req(sdp_session_t *session, uint8_t *buf, uint32_t size)
1692
while (sent < size) {
1693
int n = send(session->sock, buf + sent, size - sent, 0);
1701
static int sdp_read_rsp(sdp_session_t *session, uint8_t *buf, uint32_t size)
1704
struct timeval timeout = { SDP_RESPONSE_TIMEOUT, 0 };
1707
FD_SET(session->sock, &readFds);
1708
SDPDBG("Waiting for response\n");
1709
if (select(session->sock + 1, &readFds, NULL, NULL, &timeout) == 0) {
1710
SDPERR("Client timed out\n");
1714
return recv(session->sock, buf, size, 0);
1718
* generic send request, wait for response method.
1720
int sdp_send_req_w4_rsp(sdp_session_t *session, uint8_t *reqbuf,
1721
uint8_t *rspbuf, uint32_t reqsize, uint32_t *rspsize)
1724
sdp_pdu_hdr_t *reqhdr = (sdp_pdu_hdr_t *) reqbuf;
1725
sdp_pdu_hdr_t *rsphdr = (sdp_pdu_hdr_t *) rspbuf;
1728
if (0 > sdp_send_req(session, reqbuf, reqsize)) {
1729
SDPERR("Error sending data:%s", strerror(errno));
1732
n = sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE);
1735
SDPDBG("Read : %d\n", n);
1736
if (n == 0 || reqhdr->tid != rsphdr->tid) {
1745
* singly-linked lists (after openobex implementation)
1747
sdp_list_t *sdp_list_append(sdp_list_t *p, void *d)
1749
sdp_list_t *q, *n = malloc(sizeof(sdp_list_t));
1760
for (q = p; q->next; q = q->next);
1766
sdp_list_t *sdp_list_remove(sdp_list_t *list, void *d)
1770
for (q = 0, p = list; p; q = p, p = p->next)
1783
sdp_list_t *sdp_list_insert_sorted(sdp_list_t *list, void *d,
1786
sdp_list_t *q, *p, *n;
1788
n = malloc(sizeof(sdp_list_t));
1792
for (q = 0, p = list; p; q = p, p = p->next)
1793
if (f(p->data, d) >= 0)
1795
/* insert between q and p; if !q insert at head */
1805
* Every element of the list points to things which need
1806
* to be free()'d. This method frees the list's contents
1808
void sdp_list_free(sdp_list_t *list, sdp_free_func_t f)
1820
static inline int __find_port(sdp_data_t *seq, int proto)
1822
if (!seq || !seq->next)
1825
if (SDP_IS_UUID(seq->dtd) && sdp_uuid_to_proto(&seq->val.uuid) == proto) {
1829
return seq->val.uint8;
1831
return seq->val.uint16;
1837
int sdp_get_proto_port(const sdp_list_t *list, int proto)
1839
if (proto != L2CAP_UUID && proto != RFCOMM_UUID) {
1844
for (; list; list = list->next) {
1846
for (p = list->data; p; p = p->next) {
1847
sdp_data_t *seq = p->data;
1848
int port = __find_port(seq, proto);
1856
sdp_data_t *sdp_get_proto_desc(sdp_list_t *list, int proto)
1858
for (; list; list = list->next) {
1860
for (p = list->data; p; p = p->next) {
1861
sdp_data_t *seq = p->data;
1862
if (SDP_IS_UUID(seq->dtd) &&
1863
sdp_uuid_to_proto(&seq->val.uuid) == proto)
1870
int sdp_get_access_protos(const sdp_record_t *rec, sdp_list_t **pap)
1872
sdp_data_t *pdlist, *curr;
1875
pdlist = sdp_data_get(rec, SDP_ATTR_PROTO_DESC_LIST);
1876
if (pdlist == NULL) {
1880
SDPDBG("AP type : 0%x\n", pdlist->dtd);
1882
for (; pdlist; pdlist = pdlist->next) {
1883
sdp_list_t *pds = 0;
1884
for (curr = pdlist->val.dataseq; curr; curr = curr->next)
1885
pds = sdp_list_append(pds, curr->val.dataseq);
1886
ap = sdp_list_append(ap, pds);
1892
int sdp_get_add_access_protos(const sdp_record_t *rec, sdp_list_t **pap)
1894
sdp_data_t *pdlist, *curr;
1897
pdlist = sdp_data_get(rec, SDP_ATTR_ADD_PROTO_DESC_LIST);
1898
if (pdlist == NULL) {
1902
SDPDBG("AP type : 0%x\n", pdlist->dtd);
1904
pdlist = pdlist->val.dataseq;
1906
for (; pdlist; pdlist = pdlist->next) {
1907
sdp_list_t *pds = 0;
1908
for (curr = pdlist->val.dataseq; curr; curr = curr->next)
1909
pds = sdp_list_append(pds, curr->val.dataseq);
1910
ap = sdp_list_append(ap, pds);
1916
int sdp_get_uuidseq_attr(const sdp_record_t *rec, uint16_t attr,
1919
sdp_data_t *sdpdata = sdp_data_get(rec, attr);
1922
if (sdpdata && sdpdata->dtd >= SDP_SEQ8 && sdpdata->dtd <= SDP_SEQ32) {
1924
for (d = sdpdata->val.dataseq; d; d = d->next) {
1926
if (d->dtd < SDP_UUID16 || d->dtd > SDP_UUID128) {
1931
u = malloc(sizeof(uuid_t));
1936
*seqp = sdp_list_append(*seqp, u);
1941
sdp_list_free(*seqp, free);
1946
int sdp_set_uuidseq_attr(sdp_record_t *rec, uint16_t aid, sdp_list_t *seq)
1948
int status = 0, i, len;
1949
void **dtds, **values;
1950
uint8_t uuid16 = SDP_UUID16;
1951
uint8_t uuid32 = SDP_UUID32;
1952
uint8_t uuid128 = SDP_UUID128;
1955
len = sdp_list_len(seq);
1956
if (!seq || len == 0)
1958
dtds = malloc(len * sizeof(void *));
1962
values = malloc(len * sizeof(void *));
1968
for (p = seq, i = 0; i < len; i++, p = p->next) {
1969
uuid_t *uuid = p->data;
1971
switch (uuid->type) {
1974
values[i] = &uuid->value.uuid16;
1978
values[i] = &uuid->value.uuid32;
1982
values[i] = &uuid->value.uuid128;
1994
sdp_data_t *data = sdp_seq_alloc(dtds, values, len);
1995
sdp_attr_replace(rec, aid, data);
1996
sdp_pattern_add_uuidseq(rec, seq);
2003
int sdp_get_lang_attr(const sdp_record_t *rec, sdp_list_t **langSeq)
2005
sdp_lang_attr_t *lang;
2006
sdp_data_t *sdpdata, *curr_data;
2009
sdpdata = sdp_data_get(rec, SDP_ATTR_LANG_BASE_ATTR_ID_LIST);
2010
if (sdpdata == NULL) {
2014
curr_data = sdpdata->val.dataseq;
2016
sdp_data_t *pCode = curr_data;
2017
sdp_data_t *pEncoding;
2018
sdp_data_t *pOffset;
2020
pEncoding = pCode->next;
2024
pOffset = pEncoding->next;
2028
lang = malloc(sizeof(sdp_lang_attr_t));
2030
sdp_list_free(*langSeq, free);
2034
lang->code_ISO639 = pCode->val.uint16;
2035
lang->encoding = pEncoding->val.uint16;
2036
lang->base_offset = pOffset->val.uint16;
2037
SDPDBG("code_ISO639 : 0x%02x\n", lang->code_ISO639);
2038
SDPDBG("encoding : 0x%02x\n", lang->encoding);
2039
SDPDBG("base_offfset : 0x%02x\n", lang->base_offset);
2040
*langSeq = sdp_list_append(*langSeq, lang);
2042
curr_data = pOffset->next;
2048
int sdp_get_profile_descs(const sdp_record_t *rec, sdp_list_t **profDescSeq)
2050
sdp_profile_desc_t *profDesc;
2051
sdp_data_t *sdpdata, *seq;
2053
*profDescSeq = NULL;
2054
sdpdata = sdp_data_get(rec, SDP_ATTR_PFILE_DESC_LIST);
2055
if (!sdpdata || !sdpdata->val.dataseq) {
2059
for (seq = sdpdata->val.dataseq; seq && seq->val.dataseq; seq = seq->next) {
2060
uuid_t *uuid = NULL;
2061
uint16_t version = 0x100;
2063
if (SDP_IS_UUID(seq->dtd)) {
2064
sdp_data_t *next = seq->next;
2065
uuid = &seq->val.uuid;
2066
if (next && next->dtd == SDP_UINT16) {
2067
version = next->val.uint16;
2070
} else if (SDP_IS_SEQ(seq->dtd)) {
2071
sdp_data_t *puuid = seq->val.dataseq;
2072
sdp_data_t *pVnum = seq->val.dataseq->next;
2073
if (puuid && pVnum) {
2074
uuid = &puuid->val.uuid;
2075
version = pVnum->val.uint16;
2080
profDesc = malloc(sizeof(sdp_profile_desc_t));
2082
sdp_list_free(*profDescSeq, free);
2083
*profDescSeq = NULL;
2086
profDesc->uuid = *uuid;
2087
profDesc->version = version;
2089
sdp_uuid_print(&profDesc->uuid);
2090
SDPDBG("Vnum : 0x%04x\n", profDesc->version);
2092
*profDescSeq = sdp_list_append(*profDescSeq, profDesc);
2098
int sdp_get_server_ver(const sdp_record_t *rec, sdp_list_t **u16)
2100
sdp_data_t *d, *curr;
2103
d = sdp_data_get(rec, SDP_ATTR_VERSION_NUM_LIST);
2108
for (curr = d->val.dataseq; curr; curr = curr->next)
2109
*u16 = sdp_list_append(*u16, &curr->val.uint16);
2113
/* flexible extraction of basic attributes - Jean II */
2114
/* How do we expect caller to extract predefined data sequences? */
2115
int sdp_get_int_attr(const sdp_record_t *rec, uint16_t attrid, int *value)
2117
sdp_data_t *sdpdata = sdp_data_get(rec, attrid);
2120
/* Verify that it is what the caller expects */
2121
if (sdpdata->dtd == SDP_BOOL || sdpdata->dtd == SDP_UINT8 ||
2122
sdpdata->dtd == SDP_UINT16 || sdpdata->dtd == SDP_UINT32 ||
2123
sdpdata->dtd == SDP_INT8 || sdpdata->dtd == SDP_INT16 ||
2124
sdpdata->dtd == SDP_INT32) {
2125
*value = sdpdata->val.uint32;
2132
int sdp_get_string_attr(const sdp_record_t *rec, uint16_t attrid, char *value,
2135
sdp_data_t *sdpdata = sdp_data_get(rec, attrid);
2137
/* Verify that it is what the caller expects */
2138
if (sdpdata->dtd == SDP_TEXT_STR8 ||
2139
sdpdata->dtd == SDP_TEXT_STR16 ||
2140
sdpdata->dtd == SDP_TEXT_STR32)
2141
if ((int) strlen(sdpdata->val.str) < valuelen) {
2142
strcpy(value, sdpdata->val.str);
2149
#define get_basic_attr(attrID, pAttrValue, fieldName) \
2150
sdp_data_t *data = sdp_data_get(rec, attrID); \
2152
*pAttrValue = data->val.fieldName; \
2158
int sdp_get_service_id(const sdp_record_t *rec, uuid_t *uuid)
2160
get_basic_attr(SDP_ATTR_SERVICE_ID, uuid, uuid);
2163
int sdp_get_group_id(const sdp_record_t *rec, uuid_t *uuid)
2165
get_basic_attr(SDP_ATTR_GROUP_ID, uuid, uuid);
2168
int sdp_get_record_state(const sdp_record_t *rec, uint32_t *svcRecState)
2170
get_basic_attr(SDP_ATTR_RECORD_STATE, svcRecState, uint32);
2173
int sdp_get_service_avail(const sdp_record_t *rec, uint8_t *svcAvail)
2175
get_basic_attr(SDP_ATTR_SERVICE_AVAILABILITY, svcAvail, uint8);
2178
int sdp_get_service_ttl(const sdp_record_t *rec, uint32_t *svcTTLInfo)
2180
get_basic_attr(SDP_ATTR_SVCINFO_TTL, svcTTLInfo, uint32);
2183
int sdp_get_database_state(const sdp_record_t *rec, uint32_t *svcDBState)
2185
get_basic_attr(SDP_ATTR_SVCDB_STATE, svcDBState, uint32);
2189
* NOTE that none of the setXXX() functions below will
2190
* actually update the SDP server, unless the
2191
* {register, update}sdp_record_t() function is invoked.
2194
int sdp_attr_add_new(sdp_record_t *rec, uint16_t attr, uint8_t dtd,
2197
sdp_data_t *d = sdp_data_alloc(dtd, value);
2199
sdp_attr_replace(rec, attr, d);
2205
static int sdp_attr_add_new_with_length(sdp_record_t *rec,
2206
uint16_t attr, uint8_t dtd, const void *value, uint32_t len)
2210
d = sdp_data_alloc_with_length(dtd, value, len);
2214
sdp_attr_replace(rec, attr, d);
2220
* Set the information attributes of the service
2221
* pointed to by rec. The attributes are
2222
* service name, description and provider name
2224
void sdp_set_info_attr(sdp_record_t *rec, const char *name, const char *prov,
2228
sdp_attr_add_new(rec, SDP_ATTR_SVCNAME_PRIMARY,
2229
SDP_TEXT_STR8, name);
2231
sdp_attr_add_new(rec, SDP_ATTR_PROVNAME_PRIMARY,
2232
SDP_TEXT_STR8, prov);
2234
sdp_attr_add_new(rec, SDP_ATTR_SVCDESC_PRIMARY,
2235
SDP_TEXT_STR8, desc);
2238
static sdp_data_t *access_proto_to_dataseq(sdp_record_t *rec, sdp_list_t *proto)
2240
sdp_data_t *seq = NULL;
2241
void *dtds[10], *values[10];
2242
void **seqDTDs, **seqs;
2246
seqlen = sdp_list_len(proto);
2247
seqDTDs = malloc(seqlen * sizeof(void *));
2251
seqs = malloc(seqlen * sizeof(void *));
2257
for (i = 0, p = proto; p; p = p->next, i++) {
2258
sdp_list_t *elt = p->data;
2260
uuid_t *uuid = NULL;
2261
unsigned int pslen = 0;
2262
for (; elt && pslen < ARRAY_SIZE(dtds); elt = elt->next, pslen++) {
2263
sdp_data_t *d = elt->data;
2264
dtds[pslen] = &d->dtd;
2267
uuid = (uuid_t *) d;
2268
values[pslen] = &uuid->value.uuid16;
2271
uuid = (uuid_t *) d;
2272
values[pslen] = &uuid->value.uuid32;
2275
uuid = (uuid_t *) d;
2276
values[pslen] = &uuid->value.uuid128;
2279
values[pslen] = &d->val.uint8;
2282
values[pslen] = &d->val.uint16;
2292
s = sdp_seq_alloc(dtds, values, pslen);
2294
seqDTDs[i] = &s->dtd;
2297
sdp_pattern_add_uuid(rec, uuid);
2300
seq = sdp_seq_alloc(seqDTDs, seqs, seqlen);
2307
* sets the access protocols of the service specified
2308
* to the value specified in "access_proto"
2310
* Note that if there are alternate mechanisms by
2311
* which the service is accessed, then they should
2312
* be specified as sequences
2314
* Using a value of NULL for accessProtocols has
2315
* effect of removing this attribute (if previously set)
2317
* This function replaces the existing sdp_access_proto_t
2318
* structure (if any) with the new one specified.
2320
* returns 0 if successful or -1 if there is a failure.
2322
int sdp_set_access_protos(sdp_record_t *rec, const sdp_list_t *ap)
2324
const sdp_list_t *p;
2325
sdp_data_t *protos = NULL;
2327
for (p = ap; p; p = p->next) {
2328
sdp_data_t *seq = access_proto_to_dataseq(rec, p->data);
2329
protos = sdp_seq_append(protos, seq);
2332
sdp_attr_add(rec, SDP_ATTR_PROTO_DESC_LIST, protos);
2337
int sdp_set_add_access_protos(sdp_record_t *rec, const sdp_list_t *ap)
2339
const sdp_list_t *p;
2340
sdp_data_t *protos = NULL;
2342
for (p = ap; p; p = p->next) {
2343
sdp_data_t *seq = access_proto_to_dataseq(rec, p->data);
2344
protos = sdp_seq_append(protos, seq);
2347
sdp_attr_add(rec, SDP_ATTR_ADD_PROTO_DESC_LIST,
2348
protos ? sdp_data_alloc(SDP_SEQ8, protos) : NULL);
2354
* set the "LanguageBase" attributes of the service record
2355
* record to the value specified in "langAttrList".
2357
* "langAttrList" is a linked list of "sdp_lang_attr_t"
2358
* objects, one for each language in which user visible
2359
* attributes are present in the service record.
2361
* Using a value of NULL for langAttrList has
2362
* effect of removing this attribute (if previously set)
2364
* This function replaces the exisiting sdp_lang_attr_t
2365
* structure (if any) with the new one specified.
2367
* returns 0 if successful or -1 if there is a failure.
2369
int sdp_set_lang_attr(sdp_record_t *rec, const sdp_list_t *seq)
2371
uint8_t uint16 = SDP_UINT16;
2372
int status = 0, i = 0, seqlen = sdp_list_len(seq);
2373
void **dtds, **values;
2374
const sdp_list_t *p;
2376
dtds = malloc(3 * seqlen * sizeof(void *));
2380
values = malloc(3 * seqlen * sizeof(void *));
2386
for (p = seq; p; p = p->next) {
2387
sdp_lang_attr_t *lang = p->data;
2393
values[i] = &lang->code_ISO639;
2396
values[i] = &lang->encoding;
2399
values[i] = &lang->base_offset;
2403
sdp_data_t *seq = sdp_seq_alloc(dtds, values, 3 * seqlen);
2404
sdp_attr_add(rec, SDP_ATTR_LANG_BASE_ATTR_ID_LIST, seq);
2412
* set the "ServiceID" attribute of the service.
2414
* This is the UUID of the service.
2416
* returns 0 if successful or -1 if there is a failure.
2418
void sdp_set_service_id(sdp_record_t *rec, uuid_t uuid)
2420
switch (uuid.type) {
2422
sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID16,
2423
&uuid.value.uuid16);
2426
sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID32,
2427
&uuid.value.uuid32);
2430
sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID128,
2431
&uuid.value.uuid128);
2434
sdp_pattern_add_uuid(rec, &uuid);
2438
* set the GroupID attribute of the service record defining a group.
2440
* This is the UUID of the group.
2442
* returns 0 if successful or -1 if there is a failure.
2444
void sdp_set_group_id(sdp_record_t *rec, uuid_t uuid)
2446
switch (uuid.type) {
2448
sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID16,
2449
&uuid.value.uuid16);
2452
sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID32,
2453
&uuid.value.uuid32);
2456
sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID128,
2457
&uuid.value.uuid128);
2460
sdp_pattern_add_uuid(rec, &uuid);
2464
* set the ProfileDescriptorList attribute of the service record
2465
* pointed to by record to the value specified in "profileDesc".
2467
* Each element in the list is an object of type
2468
* sdp_profile_desc_t which is a definition of the
2469
* Bluetooth profile that this service conforms to.
2471
* Using a value of NULL for profileDesc has
2472
* effect of removing this attribute (if previously set)
2474
* This function replaces the exisiting ProfileDescriptorList
2475
* structure (if any) with the new one specified.
2477
* returns 0 if successful or -1 if there is a failure.
2479
int sdp_set_profile_descs(sdp_record_t *rec, const sdp_list_t *profiles)
2482
uint8_t uuid16 = SDP_UUID16;
2483
uint8_t uuid32 = SDP_UUID32;
2484
uint8_t uuid128 = SDP_UUID128;
2485
uint8_t uint16 = SDP_UINT16;
2486
int i = 0, seqlen = sdp_list_len(profiles);
2487
void **seqDTDs, **seqs;
2488
const sdp_list_t *p;
2490
seqDTDs = malloc(seqlen * sizeof(void *));
2494
seqs = malloc(seqlen * sizeof(void *));
2500
for (p = profiles; p; p = p->next) {
2502
void *dtds[2], *values[2];
2503
sdp_profile_desc_t *profile = p->data;
2508
switch (profile->uuid.type) {
2511
values[0] = &profile->uuid.value.uuid16;
2515
values[0] = &profile->uuid.value.uuid32;
2519
values[0] = &profile->uuid.value.uuid128;
2526
values[1] = &profile->version;
2527
seq = sdp_seq_alloc(dtds, values, 2);
2529
seqDTDs[i] = &seq->dtd;
2531
sdp_pattern_add_uuid(rec, &profile->uuid);
2536
sdp_data_t *pAPSeq = sdp_seq_alloc(seqDTDs, seqs, seqlen);
2537
sdp_attr_add(rec, SDP_ATTR_PFILE_DESC_LIST, pAPSeq);
2545
* sets various URL attributes of the service
2546
* pointed to by record. The URL include
2548
* client: a URL to the client's
2549
* platform specific (WinCE, PalmOS) executable
2550
* code that can be used to access this service.
2552
* doc: a URL pointing to service documentation
2554
* icon: a URL to an icon that can be used to represent
2557
* Note that you need to pass NULL for any URLs
2558
* that you don't want to set or remove
2560
void sdp_set_url_attr(sdp_record_t *rec, const char *client, const char *doc,
2563
sdp_attr_add_new(rec, SDP_ATTR_CLNT_EXEC_URL, SDP_URL_STR8, client);
2564
sdp_attr_add_new(rec, SDP_ATTR_DOC_URL, SDP_URL_STR8, doc);
2565
sdp_attr_add_new(rec, SDP_ATTR_ICON_URL, SDP_URL_STR8, icon);
2568
uuid_t *sdp_uuid16_create(uuid_t *u, uint16_t val)
2570
memset(u, 0, sizeof(uuid_t));
2571
u->type = SDP_UUID16;
2572
u->value.uuid16 = val;
2576
uuid_t *sdp_uuid32_create(uuid_t *u, uint32_t val)
2578
memset(u, 0, sizeof(uuid_t));
2579
u->type = SDP_UUID32;
2580
u->value.uuid32 = val;
2584
uuid_t *sdp_uuid128_create(uuid_t *u, const void *val)
2586
memset(u, 0, sizeof(uuid_t));
2587
u->type = SDP_UUID128;
2588
memcpy(&u->value.uuid128, val, sizeof(uint128_t));
2593
* UUID comparison function
2594
* returns 0 if uuidValue1 == uuidValue2 else -1
2596
int sdp_uuid_cmp(const void *p1, const void *p2)
2598
uuid_t *u1 = sdp_uuid_to_uuid128(p1);
2599
uuid_t *u2 = sdp_uuid_to_uuid128(p2);
2602
ret = sdp_uuid128_cmp(u1, u2);
2611
* UUID comparison function
2612
* returns 0 if uuidValue1 == uuidValue2 else -1
2614
int sdp_uuid16_cmp(const void *p1, const void *p2)
2616
const uuid_t *u1 = p1;
2617
const uuid_t *u2 = p2;
2618
return memcmp(&u1->value.uuid16, &u2->value.uuid16, sizeof(uint16_t));
2622
* UUID comparison function
2623
* returns 0 if uuidValue1 == uuidValue2 else -1
2625
int sdp_uuid128_cmp(const void *p1, const void *p2)
2627
const uuid_t *u1 = p1;
2628
const uuid_t *u2 = p2;
2629
return memcmp(&u1->value.uuid128, &u2->value.uuid128, sizeof(uint128_t));
2633
* 128 to 16 bit and 32 to 16 bit UUID conversion functions
2634
* yet to be implemented. Note that the input is in NBO in
2635
* both 32 and 128 bit UUIDs and conversion is needed
2637
void sdp_uuid16_to_uuid128(uuid_t *uuid128, const uuid_t *uuid16)
2640
* We have a 16 bit value, which needs to be added to
2641
* bytes 3 and 4 (at indices 2 and 3) of the Bluetooth base
2643
unsigned short data1;
2645
/* allocate a 128bit UUID and init to the Bluetooth base UUID */
2646
uuid128->value.uuid128 = bluetooth_base_uuid;
2647
uuid128->type = SDP_UUID128;
2649
/* extract bytes 2 and 3 of 128bit BT base UUID */
2650
memcpy(&data1, &bluetooth_base_uuid.data[2], 2);
2652
/* add the given UUID (16 bits) */
2653
data1 += htons(uuid16->value.uuid16);
2655
/* set bytes 2 and 3 of the 128 bit value */
2656
memcpy(&uuid128->value.uuid128.data[2], &data1, 2);
2659
void sdp_uuid32_to_uuid128(uuid_t *uuid128, const uuid_t *uuid32)
2662
* We have a 32 bit value, which needs to be added to
2663
* bytes 1->4 (at indices 0 thru 3) of the Bluetooth base
2667
/* allocate a 128bit UUID and init to the Bluetooth base UUID */
2668
uuid128->value.uuid128 = bluetooth_base_uuid;
2669
uuid128->type = SDP_UUID128;
2671
/* extract first 4 bytes */
2672
memcpy(&data0, &bluetooth_base_uuid.data[0], 4);
2674
/* add the given UUID (32bits) */
2675
data0 += htonl(uuid32->value.uuid32);
2677
/* set the 4 bytes of the 128 bit value */
2678
memcpy(&uuid128->value.uuid128.data[0], &data0, 4);
2681
uuid_t *sdp_uuid_to_uuid128(const uuid_t *uuid)
2683
uuid_t *uuid128 = bt_malloc(sizeof(uuid_t));
2688
memset(uuid128, 0, sizeof(uuid_t));
2689
switch (uuid->type) {
2694
sdp_uuid32_to_uuid128(uuid128, uuid);
2697
sdp_uuid16_to_uuid128(uuid128, uuid);
2704
* converts a 128-bit uuid to a 16/32-bit one if possible
2705
* returns true if uuid contains a 16/32-bit UUID at exit
2707
int sdp_uuid128_to_uuid(uuid_t *uuid)
2709
uint128_t *b = &bluetooth_base_uuid;
2710
uint128_t *u = &uuid->value.uuid128;
2714
if (uuid->type != SDP_UUID128)
2717
for (i = 4; i < sizeof(b->data); i++)
2718
if (b->data[i] != u->data[i])
2721
memcpy(&data, u->data, 4);
2723
if (data <= 0xffff) {
2724
uuid->type = SDP_UUID16;
2725
uuid->value.uuid16 = (uint16_t) data;
2727
uuid->type = SDP_UUID32;
2728
uuid->value.uuid32 = data;
2734
* convert a UUID to the 16-bit short-form
2736
int sdp_uuid_to_proto(uuid_t *uuid)
2739
if (sdp_uuid128_to_uuid(&u)) {
2742
return u.value.uuid16;
2744
return u.value.uuid32;
2751
* This function appends data to the PDU buffer "dst" from source "src".
2752
* The data length is also computed and set.
2753
* Should the PDU length exceed 2^8, then sequence type is
2754
* set accordingly and the data is memmove()'d.
2756
void sdp_append_to_buf(sdp_buf_t *dst, uint8_t *data, uint32_t len)
2758
uint8_t *p = dst->data;
2761
SDPDBG("Append src size: %d\n", len);
2762
SDPDBG("Append dst size: %d\n", dst->data_size);
2763
SDPDBG("Dst buffer size: %d\n", dst->buf_size);
2764
if (dst->data_size == 0 && dtd == 0) {
2765
/* create initial sequence */
2767
dst->data_size += sizeof(uint8_t);
2768
/* reserve space for sequence size */
2769
dst->data_size += sizeof(uint8_t);
2772
memcpy(dst->data + dst->data_size, data, len);
2773
dst->data_size += len;
2775
dtd = *(uint8_t *) dst->data;
2776
if (dst->data_size > UCHAR_MAX && dtd == SDP_SEQ8) {
2777
short offset = sizeof(uint8_t) + sizeof(uint8_t);
2778
memmove(dst->data + offset + 1, dst->data + offset,
2779
dst->data_size - offset);
2781
dst->data_size += 1;
2783
dtd = *(uint8_t *) p;
2784
p += sizeof(uint8_t);
2787
*(uint8_t *) p = dst->data_size - sizeof(uint8_t) - sizeof(uint8_t);
2790
bt_put_unaligned(htons(dst->data_size - sizeof(uint8_t) - sizeof(uint16_t)), (uint16_t *) p);
2793
bt_put_unaligned(htonl(dst->data_size - sizeof(uint8_t) - sizeof(uint32_t)), (uint32_t *) p);
2798
void sdp_append_to_pdu(sdp_buf_t *pdu, sdp_data_t *d)
2802
memset(&append, 0, sizeof(sdp_buf_t));
2803
sdp_gen_buffer(&append, d);
2804
append.data = malloc(append.buf_size);
2808
sdp_set_attrid(&append, d->attrId);
2809
sdp_gen_pdu(&append, d);
2810
sdp_append_to_buf(pdu, append.data, append.data_size);
2815
* Registers an sdp record.
2817
* It is incorrect to call this method on a record that
2818
* has been already registered with the server.
2820
* Returns zero on success, otherwise -1 (and sets errno).
2822
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)
2824
uint8_t *req, *rsp, *p;
2825
uint32_t reqsize, rspsize;
2826
sdp_pdu_hdr_t *reqhdr, *rsphdr;
2831
if (!session->local) {
2835
req = malloc(SDP_REQ_BUFFER_SIZE);
2836
rsp = malloc(SDP_RSP_BUFFER_SIZE);
2837
if (req == NULL || rsp == NULL) {
2843
reqhdr = (sdp_pdu_hdr_t *)req;
2844
reqhdr->pdu_id = SDP_SVC_REGISTER_REQ;
2845
reqhdr->tid = htons(sdp_gen_tid(session));
2846
reqsize = sizeof(sdp_pdu_hdr_t) + 1;
2847
p = req + sizeof(sdp_pdu_hdr_t);
2849
if (bacmp(device, BDADDR_ANY)) {
2850
*p++ = flags | SDP_DEVICE_RECORD;
2851
bacpy((bdaddr_t *) p, device);
2852
p += sizeof(bdaddr_t);
2853
reqsize += sizeof(bdaddr_t);
2857
memcpy(p, data, size);
2859
reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
2861
status = sdp_send_req_w4_rsp(session, req, rsp, reqsize, &rspsize);
2865
if (rspsize < sizeof(sdp_pdu_hdr_t)) {
2866
SDPERR("Unexpected end of packet");
2872
rsphdr = (sdp_pdu_hdr_t *) rsp;
2873
p = rsp + sizeof(sdp_pdu_hdr_t);
2875
if (rsphdr->pdu_id == SDP_ERROR_RSP) {
2876
/* Invalid service record */
2879
} else if (rsphdr->pdu_id != SDP_SVC_REGISTER_RSP) {
2883
if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint32_t)) {
2884
SDPERR("Unexpected end of packet");
2890
*handle = ntohl(bt_get_unaligned((uint32_t *) p));
2900
int sdp_device_record_register(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec, uint8_t flags)
2908
if (rec->handle && rec->handle != 0xffffffff) {
2909
uint32_t handle = rec->handle;
2910
sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle);
2911
sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data);
2914
if (sdp_gen_record_pdu(rec, &pdu) < 0) {
2919
err = sdp_device_record_register_binary(session, device,
2920
pdu.data, pdu.data_size, flags, &handle);
2925
sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle);
2926
rec->handle = handle;
2927
sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data);
2933
int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags)
2935
return sdp_device_record_register(session, BDADDR_ANY, rec, flags);
2939
* unregister a service record
2941
int sdp_device_record_unregister_binary(sdp_session_t *session, bdaddr_t *device, uint32_t handle)
2943
uint8_t *reqbuf, *rspbuf, *p;
2944
uint32_t reqsize = 0, rspsize = 0;
2945
sdp_pdu_hdr_t *reqhdr, *rsphdr;
2950
if (handle == SDP_SERVER_RECORD_HANDLE) {
2955
if (!session->local) {
2960
reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
2961
rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
2962
if (!reqbuf || !rspbuf) {
2967
reqhdr = (sdp_pdu_hdr_t *) reqbuf;
2968
reqhdr->pdu_id = SDP_SVC_REMOVE_REQ;
2969
reqhdr->tid = htons(sdp_gen_tid(session));
2971
p = reqbuf + sizeof(sdp_pdu_hdr_t);
2972
reqsize = sizeof(sdp_pdu_hdr_t);
2973
bt_put_unaligned(htonl(handle), (uint32_t *) p);
2974
reqsize += sizeof(uint32_t);
2976
reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
2977
status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
2981
if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint16_t)) {
2982
SDPERR("Unexpected end of packet");
2988
rsphdr = (sdp_pdu_hdr_t *) rspbuf;
2989
p = rspbuf + sizeof(sdp_pdu_hdr_t);
2990
status = bt_get_unaligned((uint16_t *) p);
2992
if (rsphdr->pdu_id == SDP_ERROR_RSP) {
2993
/* For this case the status always is invalid record handle */
2996
} else if (rsphdr->pdu_id != SDP_SVC_REMOVE_RSP) {
3007
int sdp_device_record_unregister(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec)
3011
err = sdp_device_record_unregister_binary(session, device, rec->handle);
3013
sdp_record_free(rec);
3018
int sdp_record_unregister(sdp_session_t *session, sdp_record_t *rec)
3020
return sdp_device_record_unregister(session, BDADDR_ANY, rec);
3024
* modify an existing service record
3026
int sdp_device_record_update_binary(sdp_session_t *session, bdaddr_t *device, uint32_t handle, uint8_t *data, uint32_t size)
3031
int sdp_device_record_update(sdp_session_t *session, bdaddr_t *device, const sdp_record_t *rec)
3033
uint8_t *reqbuf, *rspbuf, *p;
3034
uint32_t reqsize, rspsize;
3035
sdp_pdu_hdr_t *reqhdr, *rsphdr;
3042
handle = rec->handle;
3044
if (handle == SDP_SERVER_RECORD_HANDLE) {
3048
if (!session->local) {
3052
reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
3053
rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
3054
if (!reqbuf || !rspbuf) {
3059
reqhdr = (sdp_pdu_hdr_t *) reqbuf;
3060
reqhdr->pdu_id = SDP_SVC_UPDATE_REQ;
3061
reqhdr->tid = htons(sdp_gen_tid(session));
3063
p = reqbuf + sizeof(sdp_pdu_hdr_t);
3064
reqsize = sizeof(sdp_pdu_hdr_t);
3066
bt_put_unaligned(htonl(handle), (uint32_t *) p);
3067
reqsize += sizeof(uint32_t);
3068
p += sizeof(uint32_t);
3070
if (sdp_gen_record_pdu(rec, &pdu) < 0) {
3075
memcpy(p, pdu.data, pdu.data_size);
3076
reqsize += pdu.data_size;
3079
reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
3080
status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
3084
if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint16_t)) {
3085
SDPERR("Unexpected end of packet");
3091
SDPDBG("Send req status : %d\n", status);
3093
rsphdr = (sdp_pdu_hdr_t *) rspbuf;
3094
p = rspbuf + sizeof(sdp_pdu_hdr_t);
3095
status = bt_get_unaligned((uint16_t *) p);
3097
if (rsphdr->pdu_id == SDP_ERROR_RSP) {
3098
/* The status can be invalid sintax or invalid record handle */
3101
} else if (rsphdr->pdu_id != SDP_SVC_UPDATE_RSP) {
3111
int sdp_record_update(sdp_session_t *session, const sdp_record_t *rec)
3113
return sdp_device_record_update(session, BDADDR_ANY, rec);
3116
sdp_record_t *sdp_record_alloc(void)
3118
sdp_record_t *rec = malloc(sizeof(sdp_record_t));
3123
memset(rec, 0, sizeof(sdp_record_t));
3124
rec->handle = 0xffffffff;
3129
* Free the contents of a service record
3131
void sdp_record_free(sdp_record_t *rec)
3133
sdp_list_free(rec->attrlist, (sdp_free_func_t) sdp_data_free);
3134
sdp_list_free(rec->pattern, free);
3138
void sdp_pattern_add_uuid(sdp_record_t *rec, uuid_t *uuid)
3140
uuid_t *uuid128 = sdp_uuid_to_uuid128(uuid);
3142
SDPDBG("Elements in target pattern : %d\n", sdp_list_len(rec->pattern));
3143
SDPDBG("Trying to add : 0x%lx\n", (unsigned long) uuid128);
3145
if (sdp_list_find(rec->pattern, uuid128, sdp_uuid128_cmp) == NULL)
3146
rec->pattern = sdp_list_insert_sorted(rec->pattern, uuid128, sdp_uuid128_cmp);
3150
SDPDBG("Elements in target pattern : %d\n", sdp_list_len(rec->pattern));
3153
void sdp_pattern_add_uuidseq(sdp_record_t *rec, sdp_list_t *seq)
3155
for (; seq; seq = seq->next) {
3156
uuid_t *uuid = (uuid_t *)seq->data;
3157
sdp_pattern_add_uuid(rec, uuid);
3162
* Extract a sequence of service record handles from a PDU buffer
3163
* and add the entries to a sdp_list_t. Note that the service record
3164
* handles are not in "data element sequence" form, but just like
3165
* an array of service handles
3167
static void extract_record_handle_seq(uint8_t *pdu, int bufsize, sdp_list_t **seq, int count, unsigned int *scanned)
3169
sdp_list_t *pSeq = *seq;
3170
uint8_t *pdata = pdu;
3173
for (n = 0; n < count; n++) {
3175
if (bufsize < (int) sizeof(uint32_t)) {
3176
SDPERR("Unexpected end of packet");
3179
pSvcRec = malloc(sizeof(uint32_t));
3182
*pSvcRec = ntohl(bt_get_unaligned((uint32_t *) pdata));
3183
pSeq = sdp_list_append(pSeq, pSvcRec);
3184
pdata += sizeof(uint32_t);
3185
*scanned += sizeof(uint32_t);
3186
bufsize -= sizeof(uint32_t);
3191
* Generate the attribute sequence pdu form
3192
* from sdp_list_t elements. Return length of attr seq
3194
static int gen_dataseq_pdu(uint8_t *dst, const sdp_list_t *seq, uint8_t dtd)
3196
sdp_data_t *dataseq;
3197
void **types, **values;
3199
int i, seqlen = sdp_list_len(seq);
3201
/* Fill up the value and the dtd arrays */
3204
SDPDBG("Seq length : %d\n", seqlen);
3206
types = malloc(seqlen * sizeof(void *));
3210
values = malloc(seqlen * sizeof(void *));
3216
for (i = 0; i < seqlen; i++) {
3217
void *data = seq->data;
3219
if (SDP_IS_UUID(dtd))
3220
data = &((uuid_t *)data)->value;
3225
dataseq = sdp_seq_alloc(types, values, seqlen);
3232
memset(&buf, 0, sizeof(sdp_buf_t));
3233
sdp_gen_buffer(&buf, dataseq);
3234
buf.data = malloc(buf.buf_size);
3237
sdp_data_free(dataseq);
3243
SDPDBG("Data Seq : 0x%p\n", seq);
3244
seqlen = sdp_gen_pdu(&buf, dataseq);
3245
SDPDBG("Copying : %d\n", buf.data_size);
3246
memcpy(dst, buf.data, buf.data_size);
3248
sdp_data_free(dataseq);
3256
static int gen_searchseq_pdu(uint8_t *dst, const sdp_list_t *seq)
3258
uuid_t *uuid = seq->data;
3259
return gen_dataseq_pdu(dst, seq, uuid->type);
3262
static int gen_attridseq_pdu(uint8_t *dst, const sdp_list_t *seq, uint8_t dataType)
3264
return gen_dataseq_pdu(dst, seq, dataType);
3269
unsigned char data[16];
3270
} __attribute__ ((packed)) sdp_cstate_t;
3272
static int copy_cstate(uint8_t *pdata, int pdata_len, const sdp_cstate_t *cstate)
3275
uint8_t len = cstate->length;
3276
if (len >= pdata_len) {
3277
SDPERR("Continuation state size exceeds internal buffer");
3278
len = pdata_len - 1;
3281
memcpy(pdata, cstate->data, len);
3289
* This is a service search request.
3293
* sdp_list_t *search
3294
* Singly linked list containing elements of the search
3295
* pattern. Each entry in the list is a UUID (DataTypeSDP_UUID16)
3296
* of the service to be searched
3298
* uint16_t max_rec_num
3299
* A 16 bit integer which tells the service, the maximum
3300
* entries that the client can handle in the response. The
3301
* server is obliged not to return > max_rec_num entries
3307
* The request completed successfully. This does not
3308
* mean the requested services were found
3310
* On any failure and sets errno
3312
* sdp_list_t **rsp_list
3313
* This variable is set on a successful return if there are
3314
* non-zero service handles. It is a singly linked list of
3315
* service record handles (uint16_t)
3317
int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search,
3318
uint16_t max_rec_num, sdp_list_t **rsp)
3321
uint32_t reqsize = 0, _reqsize;
3322
uint32_t rspsize = 0, rsplen;
3325
unsigned scanned, pdata_len;
3326
uint8_t *pdata, *_pdata;
3327
uint8_t *reqbuf, *rspbuf;
3328
sdp_pdu_hdr_t *reqhdr, *rsphdr;
3329
sdp_cstate_t *cstate = NULL;
3331
reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
3332
rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
3333
if (!reqbuf || !rspbuf) {
3338
reqhdr = (sdp_pdu_hdr_t *) reqbuf;
3339
reqhdr->pdu_id = SDP_SVC_SEARCH_REQ;
3340
pdata = reqbuf + sizeof(sdp_pdu_hdr_t);
3341
reqsize = sizeof(sdp_pdu_hdr_t);
3343
/* add service class IDs for search */
3344
seqlen = gen_searchseq_pdu(pdata, search);
3346
SDPDBG("Data seq added : %d\n", seqlen);
3348
/* set the length and increment the pointer */
3352
/* specify the maximum svc rec count that client expects */
3353
bt_put_unaligned(htons(max_rec_num), (uint16_t *) pdata);
3354
reqsize += sizeof(uint16_t);
3355
pdata += sizeof(uint16_t);
3362
/* Add continuation state or NULL (first time) */
3363
reqsize = _reqsize + copy_cstate(_pdata,
3364
SDP_REQ_BUFFER_SIZE - _reqsize, cstate);
3366
/* Set the request header's param length */
3367
reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
3369
reqhdr->tid = htons(sdp_gen_tid(session));
3371
* Send the request, wait for response and if
3372
* no error, set the appropriate values and return
3374
status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
3378
if (rspsize < sizeof(sdp_pdu_hdr_t)) {
3379
SDPERR("Unexpected end of packet");
3384
rsphdr = (sdp_pdu_hdr_t *) rspbuf;
3385
rsplen = ntohs(rsphdr->plen);
3387
if (rsphdr->pdu_id == SDP_ERROR_RSP) {
3388
SDPDBG("Status : 0x%x\n", rsphdr->pdu_id);
3393
pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
3394
pdata_len = rspsize - sizeof(sdp_pdu_hdr_t);
3396
if (pdata_len < sizeof(uint16_t) + sizeof(uint16_t)) {
3397
SDPERR("Unexpected end of packet");
3402
/* net service record match count */
3403
pdata += sizeof(uint16_t);
3404
scanned += sizeof(uint16_t);
3405
pdata_len -= sizeof(uint16_t);
3406
rec_count = ntohs(bt_get_unaligned((uint16_t *) pdata));
3407
pdata += sizeof(uint16_t);
3408
scanned += sizeof(uint16_t);
3409
pdata_len -= sizeof(uint16_t);
3411
SDPDBG("Current svc count: %d\n", rec_count);
3412
SDPDBG("ResponseLength: %d\n", rsplen);
3418
extract_record_handle_seq(pdata, pdata_len, rsp, rec_count, &scanned);
3419
SDPDBG("BytesScanned : %d\n", scanned);
3421
if (rsplen > scanned) {
3424
if (rspsize < sizeof(sdp_pdu_hdr_t) + scanned + sizeof(uint8_t)) {
3425
SDPERR("Unexpected end of packet: continuation state data missing");
3430
pdata = rspbuf + sizeof(sdp_pdu_hdr_t) + scanned;
3431
cstate_len = *(uint8_t *) pdata;
3432
if (cstate_len > 0) {
3433
cstate = (sdp_cstate_t *)pdata;
3434
SDPDBG("Cont state length: %d\n", cstate_len);
3448
* This is a service attribute request.
3453
* The handle of the service for which the attribute(s) are
3456
* sdp_attrreq_type_t reqtype
3457
* Attribute identifiers are 16 bit unsigned integers specified
3458
* in one of 2 ways described below :
3459
* SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
3460
* They are the actual attribute identifiers in ascending order
3462
* SDP_ATTR_REQ_RANGE - 32bit identifier range
3463
* The high-order 16bits is the start of range
3464
* the low-order 16bits are the end of range
3465
* 0x0000 to 0xFFFF gets all attributes
3467
* sdp_list_t *attrid
3468
* Singly linked list containing attribute identifiers desired.
3469
* Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
3470
* or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
3473
* return sdp_record_t *
3475
* On any error and sets errno
3477
* The service record
3479
sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle,
3480
sdp_attrreq_type_t reqtype, const sdp_list_t *attrids)
3482
uint32_t reqsize = 0, _reqsize;
3483
uint32_t rspsize = 0, rsp_count;
3484
int attr_list_len = 0;
3486
unsigned int pdata_len;
3487
uint8_t *pdata, *_pdata;
3488
uint8_t *reqbuf, *rspbuf;
3489
sdp_pdu_hdr_t *reqhdr, *rsphdr;
3490
sdp_cstate_t *cstate = NULL;
3491
uint8_t cstate_len = 0;
3492
sdp_buf_t rsp_concat_buf;
3493
sdp_record_t *rec = 0;
3495
if (reqtype != SDP_ATTR_REQ_INDIVIDUAL && reqtype != SDP_ATTR_REQ_RANGE) {
3500
memset(&rsp_concat_buf, 0, sizeof(sdp_buf_t));
3502
reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
3503
rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
3504
if (!reqbuf || !rspbuf) {
3508
reqhdr = (sdp_pdu_hdr_t *) reqbuf;
3509
reqhdr->pdu_id = SDP_SVC_ATTR_REQ;
3511
pdata = reqbuf + sizeof(sdp_pdu_hdr_t);
3512
reqsize = sizeof(sdp_pdu_hdr_t);
3514
/* add the service record handle */
3515
bt_put_unaligned(htonl(handle), (uint32_t *) pdata);
3516
reqsize += sizeof(uint32_t);
3517
pdata += sizeof(uint32_t);
3519
/* specify the response limit */
3520
bt_put_unaligned(htons(65535), (uint16_t *) pdata);
3521
reqsize += sizeof(uint16_t);
3522
pdata += sizeof(uint16_t);
3524
/* get attr seq PDU form */
3525
seqlen = gen_attridseq_pdu(pdata, attrids,
3526
reqtype == SDP_ATTR_REQ_INDIVIDUAL? SDP_UINT16 : SDP_UINT32);
3533
SDPDBG("Attr list length : %d\n", seqlen);
3535
/* save before Continuation State */
3542
/* add NULL continuation state */
3543
reqsize = _reqsize + copy_cstate(_pdata,
3544
SDP_REQ_BUFFER_SIZE - _reqsize, cstate);
3546
/* set the request header's param length */
3547
reqhdr->tid = htons(sdp_gen_tid(session));
3548
reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
3550
status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
3554
if (rspsize < sizeof(sdp_pdu_hdr_t)) {
3555
SDPERR("Unexpected end of packet");
3559
rsphdr = (sdp_pdu_hdr_t *) rspbuf;
3560
if (rsphdr->pdu_id == SDP_ERROR_RSP) {
3561
SDPDBG("PDU ID : 0x%x\n", rsphdr->pdu_id);
3564
pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
3565
pdata_len = rspsize - sizeof(sdp_pdu_hdr_t);
3567
if (pdata_len < sizeof(uint16_t)) {
3568
SDPERR("Unexpected end of packet");
3572
rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata));
3573
attr_list_len += rsp_count;
3574
pdata += sizeof(uint16_t);
3575
pdata_len -= sizeof(uint16_t);
3578
* if continuation state set need to re-issue request before
3581
if (pdata_len < rsp_count + sizeof(uint8_t)) {
3582
SDPERR("Unexpected end of packet: continuation state data missing");
3585
cstate_len = *(uint8_t *) (pdata + rsp_count);
3587
SDPDBG("Response id : %d\n", rsphdr->pdu_id);
3588
SDPDBG("Attrlist byte count : %d\n", rsp_count);
3589
SDPDBG("sdp_cstate_t length : %d\n", cstate_len);
3592
* a split response: concatenate intermediate responses
3593
* and the last one (which has cstate_len == 0)
3595
if (cstate_len > 0 || rsp_concat_buf.data_size != 0) {
3596
uint8_t *targetPtr = NULL;
3598
cstate = cstate_len > 0 ? (sdp_cstate_t *) (pdata + rsp_count) : 0;
3600
/* build concatenated response buffer */
3601
rsp_concat_buf.data = realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count);
3602
rsp_concat_buf.buf_size = rsp_concat_buf.data_size + rsp_count;
3603
targetPtr = rsp_concat_buf.data + rsp_concat_buf.data_size;
3604
memcpy(targetPtr, pdata, rsp_count);
3605
rsp_concat_buf.data_size += rsp_count;
3609
if (attr_list_len > 0) {
3611
if (rsp_concat_buf.data_size != 0) {
3612
pdata = rsp_concat_buf.data;
3613
pdata_len = rsp_concat_buf.data_size;
3615
rec = sdp_extract_pdu(pdata, pdata_len, &scanned);
3620
free(rsp_concat_buf.data);
3626
* SDP transaction structure for asynchronous search
3628
struct sdp_transaction {
3629
sdp_callback_t *cb; /* called when the transaction finishes */
3630
void *udata; /* client user data */
3631
uint8_t *reqbuf; /* pointer to request PDU */
3632
sdp_buf_t rsp_concat_buf;
3633
uint32_t reqsize; /* without cstate */
3634
int err; /* ZERO if success or the errno if failed */
3638
* Creates a new sdp session for asynchronous search
3641
* non-blocking L2CAP socket
3645
* NULL - On memory allocation failure
3647
sdp_session_t *sdp_create(int sk, uint32_t flags)
3649
sdp_session_t *session;
3650
struct sdp_transaction *t;
3652
session = malloc(sizeof(sdp_session_t));
3657
memset(session, 0, sizeof(*session));
3659
session->flags = flags;
3662
t = malloc(sizeof(struct sdp_transaction));
3668
memset(t, 0, sizeof(*t));
3676
* Sets the callback function/user data used to notify the application
3677
* that the asynchronous transaction finished. This function must be
3678
* called before request an asynchronous search.
3681
* sdp_session_t *session
3682
* Current sdp session to be handled
3683
* sdp_callback_t *cb
3684
* callback to be called when the transaction finishes
3686
* user data passed to callback
3691
int sdp_set_notify(sdp_session_t *session, sdp_callback_t *func, void *udata)
3693
struct sdp_transaction *t;
3695
if (!session || !session->priv)
3706
* This function starts an asynchronous service search request.
3707
* The incoming and outgoing data are stored in the transaction structure
3708
* buffers. When there is incoming data the sdp_process function must be
3709
* called to get the data and handle the continuation state.
3712
* sdp_session_t *session
3713
* Current sdp session to be handled
3715
* sdp_list_t *search
3716
* Singly linked list containing elements of the search
3717
* pattern. Each entry in the list is a UUID (DataTypeSDP_UUID16)
3718
* of the service to be searched
3720
* uint16_t max_rec_num
3721
* A 16 bit integer which tells the service, the maximum
3722
* entries that the client can handle in the response. The
3723
* server is obliged not to return > max_rec_num entries
3728
* 0 - if the request has been sent properly
3729
* -1 - On any failure and sets errno
3732
int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search, uint16_t max_rec_num)
3734
struct sdp_transaction *t;
3735
sdp_pdu_hdr_t *reqhdr;
3737
int cstate_len, seqlen = 0;
3739
if (!session || !session->priv)
3744
/* clean possible allocated buffer */
3745
free(t->rsp_concat_buf.data);
3746
memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t));
3749
t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
3755
memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE);
3757
reqhdr = (sdp_pdu_hdr_t *) t->reqbuf;
3758
reqhdr->tid = htons(sdp_gen_tid(session));
3759
reqhdr->pdu_id = SDP_SVC_SEARCH_REQ;
3762
pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t);
3763
t->reqsize = sizeof(sdp_pdu_hdr_t);
3765
/* add service class IDs for search */
3766
seqlen = gen_searchseq_pdu(pdata, search);
3768
SDPDBG("Data seq added : %d\n", seqlen);
3770
/* now set the length and increment the pointer */
3771
t->reqsize += seqlen;
3774
bt_put_unaligned(htons(max_rec_num), (uint16_t *) pdata);
3775
t->reqsize += sizeof(uint16_t);
3776
pdata += sizeof(uint16_t);
3778
/* set the request header's param length */
3779
cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL);
3780
reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t));
3782
if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) {
3783
SDPERR("Error sendind data:%s", strerror(errno));
3798
* This function starts an asynchronous service attribute request.
3799
* The incoming and outgoing data are stored in the transaction structure
3800
* buffers. When there is incoming data the sdp_process function must be
3801
* called to get the data and handle the continuation state.
3804
* sdp_session_t *session
3805
* Current sdp session to be handled
3808
* The handle of the service for which the attribute(s) are
3811
* sdp_attrreq_type_t reqtype
3812
* Attribute identifiers are 16 bit unsigned integers specified
3813
* in one of 2 ways described below :
3814
* SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
3815
* They are the actual attribute identifiers in ascending order
3817
* SDP_ATTR_REQ_RANGE - 32bit identifier range
3818
* The high-order 16bits is the start of range
3819
* the low-order 16bits are the end of range
3820
* 0x0000 to 0xFFFF gets all attributes
3822
* sdp_list_t *attrid_list
3823
* Singly linked list containing attribute identifiers desired.
3824
* Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
3825
* or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
3829
* 0 - if the request has been sent properly
3830
* -1 - On any failure and sets errno
3833
int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list)
3835
struct sdp_transaction *t;
3836
sdp_pdu_hdr_t *reqhdr;
3838
int cstate_len, seqlen = 0;
3840
if (!session || !session->priv)
3845
/* clean possible allocated buffer */
3846
free(t->rsp_concat_buf.data);
3847
memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t));
3850
t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
3856
memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE);
3858
reqhdr = (sdp_pdu_hdr_t *) t->reqbuf;
3859
reqhdr->tid = htons(sdp_gen_tid(session));
3860
reqhdr->pdu_id = SDP_SVC_ATTR_REQ;
3863
pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t);
3864
t->reqsize = sizeof(sdp_pdu_hdr_t);
3866
/* add the service record handle */
3867
bt_put_unaligned(htonl(handle), (uint32_t *) pdata);
3868
t->reqsize += sizeof(uint32_t);
3869
pdata += sizeof(uint32_t);
3871
/* specify the response limit */
3872
bt_put_unaligned(htons(65535), (uint16_t *) pdata);
3873
t->reqsize += sizeof(uint16_t);
3874
pdata += sizeof(uint16_t);
3876
/* get attr seq PDU form */
3877
seqlen = gen_attridseq_pdu(pdata, attrid_list,
3878
reqtype == SDP_ATTR_REQ_INDIVIDUAL? SDP_UINT16 : SDP_UINT32);
3884
/* now set the length and increment the pointer */
3885
t->reqsize += seqlen;
3887
SDPDBG("Attr list length : %d\n", seqlen);
3889
/* set the request header's param length */
3890
cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL);
3891
reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t));
3893
if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) {
3894
SDPERR("Error sendind data:%s", strerror(errno));
3909
* This function starts an asynchronous service search attributes.
3910
* It is a service search request combined with attribute request. The incoming
3911
* and outgoing data are stored in the transaction structure buffers. When there
3912
* is incoming data the sdp_process function must be called to get the data
3913
* and handle the continuation state.
3916
* sdp_session_t *session
3917
* Current sdp session to be handled
3919
* sdp_list_t *search
3920
* Singly linked list containing elements of the search
3921
* pattern. Each entry in the list is a UUID(DataTypeSDP_UUID16)
3922
* of the service to be searched
3924
* AttributeSpecification attrSpec
3925
* Attribute identifiers are 16 bit unsigned integers specified
3926
* in one of 2 ways described below :
3927
* SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
3928
* They are the actual attribute identifiers in ascending order
3930
* SDP_ATTR_REQ_RANGE - 32bit identifier range
3931
* The high-order 16bits is the start of range
3932
* the low-order 16bits are the end of range
3933
* 0x0000 to 0xFFFF gets all attributes
3935
* sdp_list_t *attrid_list
3936
* Singly linked list containing attribute identifiers desired.
3937
* Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
3938
* or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
3942
* 0 - if the request has been sent properly
3943
* -1 - On any failure
3945
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)
3947
struct sdp_transaction *t;
3948
sdp_pdu_hdr_t *reqhdr;
3950
int cstate_len, seqlen = 0;
3952
if (!session || !session->priv)
3957
/* clean possible allocated buffer */
3958
free(t->rsp_concat_buf.data);
3959
memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t));
3962
t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
3968
memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE);
3970
reqhdr = (sdp_pdu_hdr_t *) t->reqbuf;
3971
reqhdr->tid = htons(sdp_gen_tid(session));
3972
reqhdr->pdu_id = SDP_SVC_SEARCH_ATTR_REQ;
3975
pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t);
3976
t->reqsize = sizeof(sdp_pdu_hdr_t);
3978
/* add service class IDs for search */
3979
seqlen = gen_searchseq_pdu(pdata, search);
3981
SDPDBG("Data seq added : %d\n", seqlen);
3983
/* now set the length and increment the pointer */
3984
t->reqsize += seqlen;
3987
bt_put_unaligned(htons(SDP_MAX_ATTR_LEN), (uint16_t *) pdata);
3988
t->reqsize += sizeof(uint16_t);
3989
pdata += sizeof(uint16_t);
3991
SDPDBG("Max attr byte count : %d\n", SDP_MAX_ATTR_LEN);
3993
/* get attr seq PDU form */
3994
seqlen = gen_attridseq_pdu(pdata, attrid_list,
3995
reqtype == SDP_ATTR_REQ_INDIVIDUAL ? SDP_UINT16 : SDP_UINT32);
4002
SDPDBG("Attr list length : %d\n", seqlen);
4003
t->reqsize += seqlen;
4005
/* set the request header's param length */
4006
cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL);
4007
reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t));
4009
if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) {
4010
SDPERR("Error sendind data:%s", strerror(errno));
4025
* Function used to get the error reason after sdp_callback_t function has been called
4026
* and the status is 0xffff or if sdp_service_{search, attr, search_attr}_async returns -1.
4027
* It indicates that an error NOT related to SDP_ErrorResponse happened. Get errno directly
4028
* is not safe because multiple transactions can be triggered.
4029
* This function must be used with asynchronous sdp functions only.
4032
* sdp_session_t *session
4033
* Current sdp session to be handled
4035
* 0 = No error in the current transaction
4036
* -1 - if the session is invalid
4037
* positive value - the errno value
4040
int sdp_get_error(sdp_session_t *session)
4042
struct sdp_transaction *t;
4044
if (!session || !session->priv) {
4045
SDPERR("Invalid session");
4055
* Receive the incoming SDP PDU. This function must be called when there is data
4056
* available to be read. On continuation state, the original request (with a new
4057
* transaction ID) and the continuation state data will be appended in the initial PDU.
4058
* If an error happens or the transaction finishes the callback function will be called.
4061
* sdp_session_t *session
4062
* Current sdp session to be handled
4064
* 0 - if the transaction is on continuation state
4065
* -1 - On any failure or the transaction finished
4067
int sdp_process(sdp_session_t *session)
4069
struct sdp_transaction *t;
4070
sdp_pdu_hdr_t *reqhdr, *rsphdr;
4071
sdp_cstate_t *pcstate;
4072
uint8_t *pdata, *rspbuf, *targetPtr;
4073
int rsp_count, err = -1;
4076
uint16_t status = 0xffff;
4077
uint8_t pdu_id = 0x00;
4079
if (!session || !session->priv) {
4080
SDPERR("Invalid session");
4084
rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
4086
SDPERR("Response buffer alloc failure:%s (%d)",
4087
strerror(errno), errno);
4091
memset(rspbuf, 0, SDP_RSP_BUFFER_SIZE);
4094
reqhdr = (sdp_pdu_hdr_t *)t->reqbuf;
4095
rsphdr = (sdp_pdu_hdr_t *)rspbuf;
4097
pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
4099
n = sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE);
4101
SDPERR("Read response:%s (%d)", strerror(errno), errno);
4106
if (n == 0 || reqhdr->tid != rsphdr->tid ||
4107
(n != (int) (ntohs(rsphdr->plen) + sizeof(sdp_pdu_hdr_t)))) {
4109
SDPERR("Protocol error.");
4113
pdu_id = rsphdr->pdu_id;
4114
switch (rsphdr->pdu_id) {
4116
uint16_t tsrc, csrc;
4117
case SDP_SVC_SEARCH_RSP:
4119
* TSRC: Total Service Record Count (2 bytes)
4120
* CSRC: Current Service Record Count (2 bytes)
4123
tsrc = ntohs(bt_get_unaligned((uint16_t *) ssr_pdata));
4124
ssr_pdata += sizeof(uint16_t);
4125
csrc = ntohs(bt_get_unaligned((uint16_t *) ssr_pdata));
4127
/* csrc should never be larger than tsrc */
4130
SDPERR("Protocol error: wrong current service record count value.");
4134
SDPDBG("Total svc count: %d\n", tsrc);
4135
SDPDBG("Current svc count: %d\n", csrc);
4137
/* parameter length without continuation state */
4138
plen = sizeof(tsrc) + sizeof(csrc) + csrc * 4;
4140
if (t->rsp_concat_buf.data_size == 0) {
4141
/* first fragment */
4142
rsp_count = sizeof(tsrc) + sizeof(csrc) + csrc * 4;
4144
/* point to the first csrc */
4145
uint16_t *pcsrc = (uint16_t *) (t->rsp_concat_buf.data + 2);
4147
/* FIXME: update the interface later. csrc doesn't need be passed to clients */
4149
pdata += sizeof(uint16_t); /* point to csrc */
4151
/* the first csrc contains the sum of partial csrc responses */
4152
*pcsrc += bt_get_unaligned((uint16_t *) pdata);
4154
pdata += sizeof(uint16_t); /* point to the first handle */
4155
rsp_count = csrc * 4;
4159
case SDP_SVC_ATTR_RSP:
4160
case SDP_SVC_SEARCH_ATTR_RSP:
4161
rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata));
4162
SDPDBG("Attrlist byte count : %d\n", rsp_count);
4165
* Number of bytes in the AttributeLists parameter(without
4166
* continuation state) + AttributeListsByteCount field size.
4168
plen = sizeof(uint16_t) + rsp_count;
4170
pdata += sizeof(uint16_t); /* points to attribute list */
4174
status = ntohs(bt_get_unaligned((uint16_t *) pdata));
4175
size = ntohs(rsphdr->plen);
4180
SDPERR("Illegal PDU ID: 0x%x", rsphdr->pdu_id);
4184
pcstate = (sdp_cstate_t *) (pdata + rsp_count);
4186
SDPDBG("Cstate length : %d\n", pcstate->length);
4189
* Check out of bound. Continuation state must have at least
4190
* 1 byte: ZERO to indicate that it is not a partial response.
4192
if ((n - (int) sizeof(sdp_pdu_hdr_t)) != (plen + pcstate->length + 1)) {
4194
SDPERR("Protocol error: wrong PDU size.");
4200
* This is a split response, need to concatenate intermediate
4201
* responses and the last one which will have cstate length == 0
4203
t->rsp_concat_buf.data = realloc(t->rsp_concat_buf.data, t->rsp_concat_buf.data_size + rsp_count);
4204
targetPtr = t->rsp_concat_buf.data + t->rsp_concat_buf.data_size;
4205
t->rsp_concat_buf.buf_size = t->rsp_concat_buf.data_size + rsp_count;
4206
memcpy(targetPtr, pdata, rsp_count);
4207
t->rsp_concat_buf.data_size += rsp_count;
4209
if (pcstate->length > 0) {
4210
int reqsize, cstate_len;
4212
reqhdr->tid = htons(sdp_gen_tid(session));
4214
/* add continuation state */
4215
cstate_len = copy_cstate(t->reqbuf + t->reqsize,
4216
SDP_REQ_BUFFER_SIZE - t->reqsize, pcstate);
4218
reqsize = t->reqsize + cstate_len;
4220
/* set the request header's param length */
4221
reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
4223
if (sdp_send_req(session, t->reqbuf, reqsize) < 0) {
4224
SDPERR("Error sendind data:%s(%d)", strerror(errno), errno);
4234
if (t->rsp_concat_buf.data_size != 0) {
4235
pdata = t->rsp_concat_buf.data;
4236
size = t->rsp_concat_buf.data_size;
4239
t->cb(pdu_id, status, pdata, size, t->udata);
4248
* This is a service search request combined with the service
4249
* attribute request. First a service class match is done and
4250
* for matching service, requested attributes are extracted
4254
* sdp_list_t *search
4255
* Singly linked list containing elements of the search
4256
* pattern. Each entry in the list is a UUID(DataTypeSDP_UUID16)
4257
* of the service to be searched
4259
* AttributeSpecification attrSpec
4260
* Attribute identifiers are 16 bit unsigned integers specified
4261
* in one of 2 ways described below :
4262
* SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
4263
* They are the actual attribute identifiers in ascending order
4265
* SDP_ATTR_REQ_RANGE - 32bit identifier range
4266
* The high-order 16bits is the start of range
4267
* the low-order 16bits are the end of range
4268
* 0x0000 to 0xFFFF gets all attributes
4270
* sdp_list_t *attrids
4271
* Singly linked list containing attribute identifiers desired.
4272
* Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
4273
* or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
4278
* The request completed successfully. This does not
4279
* mean the requested services were found
4281
* On any error and sets errno
4284
* This variable is set on a successful return to point to
4285
* service(s) found. Each element of this list is of type
4286
* sdp_record_t* (of the services which matched the search list)
4288
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)
4291
uint32_t reqsize = 0, _reqsize;
4292
uint32_t rspsize = 0;
4293
int seqlen = 0, attr_list_len = 0;
4294
int rsp_count = 0, cstate_len = 0;
4295
unsigned int pdata_len;
4296
uint8_t *pdata, *_pdata;
4297
uint8_t *reqbuf, *rspbuf;
4298
sdp_pdu_hdr_t *reqhdr, *rsphdr;
4300
sdp_list_t *rec_list = NULL;
4301
sdp_buf_t rsp_concat_buf;
4302
sdp_cstate_t *cstate = NULL;
4304
if (reqtype != SDP_ATTR_REQ_INDIVIDUAL && reqtype != SDP_ATTR_REQ_RANGE) {
4309
memset(&rsp_concat_buf, 0, sizeof(sdp_buf_t));
4311
reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
4312
rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
4313
if (!reqbuf || !rspbuf) {
4319
reqhdr = (sdp_pdu_hdr_t *) reqbuf;
4320
reqhdr->pdu_id = SDP_SVC_SEARCH_ATTR_REQ;
4323
pdata = reqbuf + sizeof(sdp_pdu_hdr_t);
4324
reqsize = sizeof(sdp_pdu_hdr_t);
4326
/* add service class IDs for search */
4327
seqlen = gen_searchseq_pdu(pdata, search);
4329
SDPDBG("Data seq added : %d\n", seqlen);
4331
/* now set the length and increment the pointer */
4335
bt_put_unaligned(htons(SDP_MAX_ATTR_LEN), (uint16_t *) pdata);
4336
reqsize += sizeof(uint16_t);
4337
pdata += sizeof(uint16_t);
4339
SDPDBG("Max attr byte count : %d\n", SDP_MAX_ATTR_LEN);
4341
/* get attr seq PDU form */
4342
seqlen = gen_attridseq_pdu(pdata, attrids,
4343
reqtype == SDP_ATTR_REQ_INDIVIDUAL ? SDP_UINT16 : SDP_UINT32);
4349
SDPDBG("Attr list length : %d\n", seqlen);
4353
/* save before Continuation State */
4358
reqhdr->tid = htons(sdp_gen_tid(session));
4360
/* add continuation state (can be null) */
4361
reqsize = _reqsize + copy_cstate(_pdata,
4362
SDP_REQ_BUFFER_SIZE - _reqsize, cstate);
4364
/* set the request header's param length */
4365
reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
4366
rsphdr = (sdp_pdu_hdr_t *) rspbuf;
4367
status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
4368
if (rspsize < sizeof(sdp_pdu_hdr_t)) {
4369
SDPERR("Unexpected end of packet");
4375
SDPDBG("Status : 0x%x\n", rsphdr->pdu_id);
4379
if (rsphdr->pdu_id == SDP_ERROR_RSP) {
4384
pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
4385
pdata_len = rspsize - sizeof(sdp_pdu_hdr_t);
4387
if (pdata_len < sizeof(uint16_t)) {
4388
SDPERR("Unexpected end of packet");
4393
rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata));
4394
attr_list_len += rsp_count;
4395
pdata += sizeof(uint16_t); /* pdata points to attribute list */
4396
pdata_len -= sizeof(uint16_t);
4398
if (pdata_len < rsp_count + sizeof(uint8_t)) {
4399
SDPERR("Unexpected end of packet: continuation state data missing");
4404
cstate_len = *(uint8_t *) (pdata + rsp_count);
4406
SDPDBG("Attrlist byte count : %d\n", attr_list_len);
4407
SDPDBG("Response byte count : %d\n", rsp_count);
4408
SDPDBG("Cstate length : %d\n", cstate_len);
4410
* This is a split response, need to concatenate intermediate
4411
* responses and the last one which will have cstate_len == 0
4413
if (cstate_len > 0 || rsp_concat_buf.data_size != 0) {
4414
uint8_t *targetPtr = NULL;
4416
cstate = cstate_len > 0 ? (sdp_cstate_t *) (pdata + rsp_count) : 0;
4418
/* build concatenated response buffer */
4419
rsp_concat_buf.data = realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count);
4420
targetPtr = rsp_concat_buf.data + rsp_concat_buf.data_size;
4421
rsp_concat_buf.buf_size = rsp_concat_buf.data_size + rsp_count;
4422
memcpy(targetPtr, pdata, rsp_count);
4423
rsp_concat_buf.data_size += rsp_count;
4427
if (attr_list_len > 0) {
4430
if (rsp_concat_buf.data_size != 0) {
4431
pdata = rsp_concat_buf.data;
4432
pdata_len = rsp_concat_buf.data_size;
4436
* Response is a sequence of sequence(s) for one or
4437
* more data element sequence(s) representing services
4438
* for which attributes are returned
4440
scanned = sdp_extract_seqtype(pdata, pdata_len, &dataType, &seqlen);
4442
SDPDBG("Bytes scanned : %d\n", scanned);
4443
SDPDBG("Seq length : %d\n", seqlen);
4445
if (scanned && seqlen) {
4447
pdata_len -= scanned;
4450
sdp_record_t *rec = sdp_extract_pdu(pdata, pdata_len, &recsize);
4452
SDPERR("SVC REC is null\n");
4457
sdp_record_free(rec);
4462
pdata_len -= recsize;
4464
SDPDBG("Loc seq length : %d\n", recsize);
4465
SDPDBG("Svc Rec Handle : 0x%x\n", rec->handle);
4466
SDPDBG("Bytes scanned : %d\n", scanned);
4467
SDPDBG("Attrlist byte count : %d\n", attr_list_len);
4468
rec_list = sdp_list_append(rec_list, rec);
4469
} while (scanned < attr_list_len && pdata_len > 0);
4471
SDPDBG("Successful scan of service attr lists\n");
4476
free(rsp_concat_buf.data);
4483
* Find devices in the piconet.
4485
int sdp_general_inquiry(inquiry_info *ii, int num_dev, int duration, uint8_t *found)
4487
int n = hci_inquiry(-1, 10, num_dev, NULL, &ii, 0);
4489
SDPERR("Inquiry failed:%s", strerror(errno));
4496
int sdp_close(sdp_session_t *session)
4498
struct sdp_transaction *t;
4504
ret = close(session->sock);
4511
free(t->rsp_concat_buf.data);
4519
static inline int sdp_is_local(const bdaddr_t *device)
4521
return memcmp(device, BDADDR_LOCAL, sizeof(bdaddr_t)) == 0;
4524
static int sdp_connect_local(sdp_session_t *session)
4526
struct sockaddr_un sa;
4528
session->sock = socket(PF_UNIX, SOCK_STREAM, 0);
4529
if (session->sock < 0)
4533
sa.sun_family = AF_UNIX;
4534
strcpy(sa.sun_path, SDP_UNIX_PATH);
4536
return connect(session->sock, (struct sockaddr *) &sa, sizeof(sa));
4539
static int sdp_connect_l2cap(const bdaddr_t *src,
4540
const bdaddr_t *dst, sdp_session_t *session)
4542
uint32_t flags = session->flags;
4543
struct sockaddr_l2 sa;
4546
session->sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
4547
if (session->sock < 0)
4553
if (flags & SDP_NON_BLOCKING) {
4554
long arg = fcntl(sk, F_GETFL, 0);
4555
fcntl(sk, F_SETFL, arg | O_NONBLOCK);
4558
memset(&sa, 0, sizeof(sa));
4560
sa.l2_family = AF_BLUETOOTH;
4563
if (bacmp(src, BDADDR_ANY)) {
4564
sa.l2_bdaddr = *src;
4565
if (bind(sk, (struct sockaddr *) &sa, sizeof(sa)) < 0)
4569
if (flags & SDP_WAIT_ON_CLOSE) {
4570
struct linger l = { .l_onoff = 1, .l_linger = 1 };
4571
setsockopt(sk, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
4574
sa.l2_psm = htobs(SDP_PSM);
4575
sa.l2_bdaddr = *dst;
4578
int ret = connect(sk, (struct sockaddr *) &sa, sizeof(sa));
4581
if (ret < 0 && (flags & SDP_NON_BLOCKING) &&
4582
(errno == EAGAIN || errno == EINPROGRESS))
4584
} while (errno == EBUSY && (flags & SDP_RETRY_IF_BUSY));
4589
sdp_session_t *sdp_connect(const bdaddr_t *src,
4590
const bdaddr_t *dst, uint32_t flags)
4592
sdp_session_t *session;
4595
if ((flags & SDP_RETRY_IF_BUSY) && (flags & SDP_NON_BLOCKING)) {
4600
session = sdp_create(-1, flags);
4604
if (sdp_is_local(dst)) {
4605
if (sdp_connect_local(session) < 0)
4608
if (sdp_connect_l2cap(src, dst, session) < 0)
4616
if (session->sock >= 0)
4617
close(session->sock);
4618
free(session->priv);
4625
int sdp_get_socket(const sdp_session_t *session)
4627
return session->sock;
4630
uint16_t sdp_gen_tid(sdp_session_t *session)
4632
return session->tid++;
4636
* Set the supported features
4638
int sdp_set_supp_feat(sdp_record_t *rec, const sdp_list_t *sf)
4640
const sdp_list_t *p, *r;
4641
sdp_data_t *feat, *seq_feat;
4643
void **seqDTDs, **seqVals;
4645
seqlen = sdp_list_len(sf);
4646
seqDTDs = malloc(seqlen * sizeof(void *));
4649
seqVals = malloc(seqlen * sizeof(void *));
4655
for (p = sf, i = 0; p; p = p->next, i++) {
4657
void **dtds, **vals;
4660
plen = sdp_list_len(p->data);
4661
dtds = malloc(plen * sizeof(void *));
4664
vals = malloc(plen * sizeof(void *));
4669
lengths = malloc(plen * sizeof(int *));
4675
for (r = p->data, j = 0; r; r = r->next, j++) {
4676
sdp_data_t *data = (sdp_data_t*)r->data;
4677
dtds[j] = &data->dtd;
4678
switch (data->dtd) {
4682
case SDP_TEXT_STR16:
4683
vals[j] = data->val.str;
4684
lengths[j] = data->unitSize - sizeof(uint8_t);
4692
vals[j] = data->val.dataseq;
4696
vals[j] = &data->val;
4701
feat = sdp_seq_alloc_with_length(dtds, vals, lengths, plen);
4707
seqDTDs[i] = &feat->dtd;
4710
seq_feat = sdp_seq_alloc(seqDTDs, seqVals, seqlen);
4713
sdp_attr_replace(rec, SDP_ATTR_SUPPORTED_FEATURES_LIST, seq_feat);
4726
* Get the supported features
4727
* If an error occurred -1 is returned and errno is set
4729
int sdp_get_supp_feat(const sdp_record_t *rec, sdp_list_t **seqp)
4731
sdp_data_t *sdpdata, *d;
4735
sdpdata = sdp_data_get(rec, SDP_ATTR_SUPPORTED_FEATURES_LIST);
4737
if (!sdpdata || sdpdata->dtd < SDP_SEQ8 || sdpdata->dtd > SDP_SEQ32)
4738
return sdp_get_uuidseq_attr(rec,
4739
SDP_ATTR_SUPPORTED_FEATURES_LIST, seqp);
4741
for (d = sdpdata->val.dataseq; d; d = d->next) {
4745
if (d->dtd < SDP_SEQ8 || d->dtd > SDP_SEQ32)
4750
for (dd = d->val.dataseq; dd; dd = dd->next) {
4759
case SDP_TEXT_STR16:
4761
length = dd->unitSize - sizeof(uint8_t);
4772
data = sdp_data_alloc_with_length(dd->dtd, val, length);
4774
subseq = sdp_list_append(subseq, data);
4776
tseq = sdp_list_append(tseq, subseq);
4786
sdp_list_free(tseq, free);
4793
void sdp_add_lang_attr(sdp_record_t *rec)
4795
sdp_lang_attr_t base_lang;
4798
base_lang.code_ISO639 = (0x65 << 8) | 0x6e;
4799
base_lang.encoding = 106;
4800
base_lang.base_offset = SDP_PRIMARY_LANG_BASE;
4802
langs = sdp_list_append(0, &base_lang);
4803
sdp_set_lang_attr(rec, langs);
4804
sdp_list_free(langs, NULL);