summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2008-07-26 18:45:23 +0200
committerMarcel Holtmann <marcel@holtmann.org>2008-07-26 18:45:23 +0200
commitb8e5fea8d31fbcd3d1c044385f8217dbf39892bb (patch)
treef9f464b189194eff5e3d95009c14987d9ac0cd45 /lib
parent9f6cffae412046530af84f6f751f3ff8bfb06af0 (diff)
Move library source to lib directory
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.am9
-rw-r--r--lib/bluetooth.c448
-rw-r--r--lib/hci.c2489
-rw-r--r--lib/sdp.c4421
4 files changed, 7367 insertions, 0 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
new file mode 100644
index 00000000..065bbe60
--- /dev/null
+++ b/lib/Makefile.am
@@ -0,0 +1,9 @@
+
+lib_LTLIBRARIES = libbluetooth.la
+
+libbluetooth_la_SOURCES = bluetooth.c hci.c sdp.c
+libbluetooth_la_LDFLAGS = -version-info 13:2:11
+
+INCLUDES = -I$(top_builddir)/include
+
+MAINTAINERCLEANFILES = Makefile.in
diff --git a/lib/bluetooth.c b/lib/bluetooth.c
new file mode 100644
index 00000000..f7a46bcf
--- /dev/null
+++ b/lib/bluetooth.c
@@ -0,0 +1,448 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2000-2001 Qualcomm Incorporated
+ * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
+ * Copyright (C) 2002-2008 Marcel Holtmann <marcel@holtmann.org>
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+#include <ctype.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/hci.h>
+
+void baswap(bdaddr_t *dst, const bdaddr_t *src)
+{
+ register unsigned char *d = (unsigned char *) dst;
+ register const unsigned char *s = (const unsigned char *) src;
+ register int i;
+ for (i = 0; i < 6; i++)
+ d[i] = s[5-i];
+}
+
+char *batostr(const bdaddr_t *ba)
+{
+ char *str = bt_malloc(18);
+ if (!str)
+ return NULL;
+
+ sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
+ ba->b[0], ba->b[1], ba->b[2],
+ ba->b[3], ba->b[4], ba->b[5]);
+ return str;
+}
+
+bdaddr_t *strtoba(const char *str)
+{
+ const char *ptr = str;
+ int i;
+
+ uint8_t *ba = bt_malloc(sizeof(bdaddr_t));
+ if (!ba)
+ return NULL;
+
+ for(i = 0; i < 6; i++) {
+ ba[i] = (uint8_t) strtol(ptr, NULL, 16);
+ if (i != 5 && !(ptr = strchr(ptr,':')))
+ ptr = ":00:00:00:00:00";
+ ptr++;
+ }
+ return (bdaddr_t *) ba;
+}
+
+int ba2str(const bdaddr_t *ba, char *str)
+{
+ uint8_t b[6];
+
+ baswap((bdaddr_t *) b, ba);
+ return sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
+ b[0], b[1], b[2], b[3], b[4], b[5]);
+}
+
+int str2ba(const char *str, bdaddr_t *ba)
+{
+ uint8_t b[6];
+ const char *ptr = str;
+ int i;
+
+ for (i = 0; i < 6; i++) {
+ b[i] = (uint8_t) strtol(ptr, NULL, 16);
+ if (i != 5 && !(ptr = strchr(ptr, ':')))
+ ptr = ":00:00:00:00:00";
+ ptr++;
+ }
+ baswap(ba, (bdaddr_t *) b);
+ return 0;
+}
+
+int ba2oui(const bdaddr_t *ba, char *str)
+{
+ uint8_t b[6];
+
+ baswap((bdaddr_t *) b, ba);
+ return sprintf(str, "%2.2X-%2.2X-%2.2X", b[0], b[1], b[2]);
+}
+
+int bachk(const char *str)
+{
+ char tmp[18], *ptr = tmp;
+
+ if (!str)
+ return -1;
+
+ if (strlen(str) != 17)
+ return -1;
+
+ memcpy(tmp, str, 18);
+
+ while (*ptr) {
+ *ptr = toupper(*ptr);
+ if (*ptr < '0'|| (*ptr > '9' && *ptr < 'A') || *ptr > 'F')
+ return -1;
+ ptr++;
+
+ *ptr = toupper(*ptr);
+ if (*ptr < '0'|| (*ptr > '9' && *ptr < 'A') || *ptr > 'F')
+ return -1;
+ ptr++;
+
+ *ptr = toupper(*ptr);
+ if (*ptr == 0)
+ break;
+ if (*ptr != ':')
+ return -1;
+ ptr++;
+ }
+
+ return 0;
+}
+
+int baprintf(const char *format, ...)
+{
+ va_list ap;
+ int len;
+
+ va_start(ap, format);
+ len = vprintf(format, ap);
+ va_end(ap);
+
+ return len;
+}
+
+int bafprintf(FILE *stream, const char *format, ...)
+{
+ va_list ap;
+ int len;
+
+ va_start(ap, format);
+ len = vfprintf(stream, format, ap);
+ va_end(ap);
+
+ return len;
+}
+
+int basprintf(char *str, const char *format, ...)
+{
+ va_list ap;
+ int len;
+
+ va_start(ap, format);
+ len = vsnprintf(str, (~0U) >> 1, format, ap);
+ va_end(ap);
+
+ return len;
+}
+
+int basnprintf(char *str, size_t size, const char *format, ...)
+{
+ va_list ap;
+ int len;
+
+ va_start(ap, format);
+ len = vsnprintf(str, size, format, ap);
+ va_end(ap);
+
+ return len;
+}
+
+void *bt_malloc(size_t size)
+{
+ return malloc(size);
+}
+
+void bt_free(void *ptr)
+{
+ free(ptr);
+}
+
+/* Bluetooth error codes to Unix errno mapping */
+int bt_error(uint16_t code)
+{
+ switch (code) {
+ case 0:
+ return 0;
+ case HCI_UNKNOWN_COMMAND:
+ return EBADRQC;
+ case HCI_NO_CONNECTION:
+ return ENOTCONN;
+ case HCI_HARDWARE_FAILURE:
+ return EIO;
+ case HCI_PAGE_TIMEOUT:
+ return EHOSTDOWN;
+ case HCI_AUTHENTICATION_FAILURE:
+ return EACCES;
+ case HCI_PIN_OR_KEY_MISSING:
+ return EINVAL;
+ case HCI_MEMORY_FULL:
+ return ENOMEM;
+ case HCI_CONNECTION_TIMEOUT:
+ return ETIMEDOUT;
+ case HCI_MAX_NUMBER_OF_CONNECTIONS:
+ case HCI_MAX_NUMBER_OF_SCO_CONNECTIONS:
+ return EMLINK;
+ case HCI_ACL_CONNECTION_EXISTS:
+ return EALREADY;
+ case HCI_COMMAND_DISALLOWED:
+ case HCI_TRANSACTION_COLLISION:
+ case HCI_ROLE_SWITCH_PENDING:
+ return EBUSY;
+ case HCI_REJECTED_LIMITED_RESOURCES:
+ case HCI_REJECTED_PERSONAL:
+ case HCI_QOS_REJECTED:
+ return ECONNREFUSED;
+ case HCI_HOST_TIMEOUT:
+ return ETIMEDOUT;
+ case HCI_UNSUPPORTED_FEATURE:
+ case HCI_QOS_NOT_SUPPORTED:
+ case HCI_PAIRING_NOT_SUPPORTED:
+ case HCI_CLASSIFICATION_NOT_SUPPORTED:
+ case HCI_UNSUPPORTED_LMP_PARAMETER_VALUE:
+ case HCI_PARAMETER_OUT_OF_RANGE:
+ case HCI_QOS_UNACCEPTABLE_PARAMETER:
+ return EOPNOTSUPP;
+ case HCI_INVALID_PARAMETERS:
+ case HCI_SLOT_VIOLATION:
+ return EINVAL;
+ case HCI_OE_USER_ENDED_CONNECTION:
+ case HCI_OE_LOW_RESOURCES:
+ case HCI_OE_POWER_OFF:
+ return ECONNRESET;
+ case HCI_CONNECTION_TERMINATED:
+ return ECONNABORTED;
+ case HCI_REPEATED_ATTEMPTS:
+ return ELOOP;
+ case HCI_REJECTED_SECURITY:
+ case HCI_PAIRING_NOT_ALLOWED:
+ case HCI_INSUFFICIENT_SECURITY:
+ return EACCES;
+ case HCI_UNSUPPORTED_REMOTE_FEATURE:
+ return EPROTONOSUPPORT;
+ case HCI_SCO_OFFSET_REJECTED:
+ return ECONNREFUSED;
+ case HCI_UNKNOWN_LMP_PDU:
+ case HCI_INVALID_LMP_PARAMETERS:
+ case HCI_LMP_ERROR_TRANSACTION_COLLISION:
+ case HCI_LMP_PDU_NOT_ALLOWED:
+ case HCI_ENCRYPTION_MODE_NOT_ACCEPTED:
+ return EPROTO;
+ default:
+ return ENOSYS;
+ }
+}
+
+char *bt_compidtostr(int compid)
+{
+ switch (compid) {
+ case 0:
+ return "Ericsson Technology Licensing";
+ case 1:
+ return "Nokia Mobile Phones";
+ case 2:
+ return "Intel Corp.";
+ case 3:
+ return "IBM Corp.";
+ case 4:
+ return "Toshiba Corp.";
+ case 5:
+ return "3Com";
+ case 6:
+ return "Microsoft";
+ case 7:
+ return "Lucent";
+ case 8:
+ return "Motorola";
+ case 9:
+ return "Infineon Technologies AG";
+ case 10:
+ return "Cambridge Silicon Radio";
+ case 11:
+ return "Silicon Wave";
+ case 12:
+ return "Digianswer A/S";
+ case 13:
+ return "Texas Instruments Inc.";
+ case 14:
+ return "Parthus Technologies Inc.";
+ case 15:
+ return "Broadcom Corporation";
+ case 16:
+ return "Mitel Semiconductor";
+ case 17:
+ return "Widcomm, Inc.";
+ case 18:
+ return "Zeevo, Inc.";
+ case 19:
+ return "Atmel Corporation";
+ case 20:
+ return "Mitsubishi Electric Corporation";
+ case 21:
+ return "RTX Telecom A/S";
+ case 22:
+ return "KC Technology Inc.";
+ case 23:
+ return "Newlogic";
+ case 24:
+ return "Transilica, Inc.";
+ case 25:
+ return "Rohde & Schwartz GmbH & Co. KG";
+ case 26:
+ return "TTPCom Limited";
+ case 27:
+ return "Signia Technologies, Inc.";
+ case 28:
+ return "Conexant Systems Inc.";
+ case 29:
+ return "Qualcomm";
+ case 30:
+ return "Inventel";
+ case 31:
+ return "AVM Berlin";
+ case 32:
+ return "BandSpeed, Inc.";
+ case 33:
+ return "Mansella Ltd";
+ case 34:
+ return "NEC Corporation";
+ case 35:
+ return "WavePlus Technology Co., Ltd.";
+ case 36:
+ return "Alcatel";
+ case 37:
+ return "Philips Semiconductors";
+ case 38:
+ return "C Technologies";
+ case 39:
+ return "Open Interface";
+ case 40:
+ return "R F Micro Devices";
+ case 41:
+ return "Hitachi Ltd";
+ case 42:
+ return "Symbol Technologies, Inc.";
+ case 43:
+ return "Tenovis";
+ case 44:
+ return "Macronix International Co. Ltd.";
+ case 45:
+ return "GCT Semiconductor";
+ case 46:
+ return "Norwood Systems";
+ case 47:
+ return "MewTel Technology Inc.";
+ case 48:
+ return "ST Microelectronics";
+ case 49:
+ return "Synopsys";
+ case 50:
+ return "Red-M (Communications) Ltd";
+ case 51:
+ return "Commil Ltd";
+ case 52:
+ return "Computer Access Technology Corporation (CATC)";
+ case 53:
+ return "Eclipse (HQ Espana) S.L.";
+ case 54:
+ return "Renesas Technology Corp.";
+ case 55:
+ return "Mobilian Corporation";
+ case 56:
+ return "Terax";
+ case 57:
+ return "Integrated System Solution Corp.";
+ case 58:
+ return "Matsushita Electric Industrial Co., Ltd.";
+ case 59:
+ return "Gennum Corporation";
+ case 60:
+ return "Research In Motion";
+ case 61:
+ return "IPextreme, Inc.";
+ case 62:
+ return "Systems and Chips, Inc";
+ case 63:
+ return "Bluetooth SIG, Inc";
+ case 64:
+ return "Seiko Epson Corporation";
+ case 65:
+ return "Integrated Silicon Solution Taiwain, Inc.";
+ case 66:
+ return "CONWISE Technology Corporation Ltd";
+ case 67:
+ return "PARROT SA";
+ case 68:
+ return "Socket Communications";
+ case 69:
+ return "Atheros Communications, Inc.";
+ case 70:
+ return "MediaTek, Inc.";
+ case 71:
+ return "Bluegiga"; /* (tentative) */
+ case 72:
+ return "Marvell Technology Group Ltd.";
+ case 73:
+ return "3DSP Corporation";
+ case 74:
+ return "Accel Semiconductor Ltd.";
+ case 75:
+ return "Continental Automotive Systems";
+ case 76:
+ return "Apple, Inc.";
+ case 77:
+ return "Staccato Communications, Inc.";
+ case 78:
+ return "Avago Technologies";
+ case 79:
+ return "APT Ltd.";
+ case 65535:
+ return "internal use";
+ default:
+ return "not assigned";
+ }
+}
diff --git a/lib/hci.c b/lib/hci.c
new file mode 100644
index 00000000..a72b0355
--- /dev/null
+++ b/lib/hci.c
@@ -0,0 +1,2489 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2000-2001 Qualcomm Incorporated
+ * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
+ * Copyright (C) 2002-2008 Marcel Holtmann <marcel@holtmann.org>
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/param.h>
+#include <sys/uio.h>
+#include <sys/poll.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/hci.h>
+#include <bluetooth/hci_lib.h>
+
+#ifndef MIN
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+#endif
+
+typedef struct {
+ char *str;
+ unsigned int val;
+} hci_map;
+
+static char *hci_bit2str(hci_map *m, unsigned int val)
+{
+ char *str = malloc(120);
+ char *ptr = str;
+
+ if (!str)
+ return NULL;
+
+ *ptr = 0;
+ while (m->str) {
+ if ((unsigned int) m->val & val)
+ ptr += sprintf(ptr, "%s ", m->str);
+ m++;
+ }
+ return str;
+}
+
+static int hci_str2bit(hci_map *map, char *str, unsigned int *val)
+{
+ char *t, *ptr;
+ hci_map *m;
+ int set;
+
+ if (!str || !(str = ptr = strdup(str)))
+ return 0;
+
+ *val = set = 0;
+
+ while ((t = strsep(&ptr, ","))) {
+ for (m = map; m->str; m++) {
+ if (!strcasecmp(m->str, t)) {
+ *val |= (unsigned int) m->val;
+ set = 1;
+ }
+ }
+ }
+ free(str);
+
+ return set;
+}
+
+static char *hci_uint2str(hci_map *m, unsigned int val)
+{
+ char *str = malloc(50);
+ char *ptr = str;
+
+ if (!str)
+ return NULL;
+
+ *ptr = 0;
+ while (m->str) {
+ if ((unsigned int) m->val == val) {
+ ptr += sprintf(ptr, "%s", m->str);
+ break;
+ }
+ m++;
+ }
+ return str;
+}
+
+static int hci_str2uint(hci_map *map, char *str, unsigned int *val)
+{
+ char *t, *ptr;
+ hci_map *m;
+ int set = 0;
+
+ if (!str)
+ return 0;
+
+ str = ptr = strdup(str);
+
+ while ((t = strsep(&ptr, ","))) {
+ for (m = map; m->str; m++) {
+ if (!strcasecmp(m->str,t)) {
+ *val = (unsigned int) m->val; set = 1;
+ break;
+ }
+ }
+ }
+ free(str);
+
+ return set;
+}
+
+char *hci_dtypetostr(int type)
+{
+ switch (type) {
+ case HCI_VIRTUAL:
+ return "VIRTUAL";
+ case HCI_USB:
+ return "USB";
+ case HCI_PCCARD:
+ return "PCCARD";
+ case HCI_UART:
+ return "UART";
+ case HCI_RS232:
+ return "RS232";
+ case HCI_PCI:
+ return "PCI";
+ case HCI_SDIO:
+ return "SDIO";
+ default:
+ return "UNKNOWN";
+ }
+}
+
+/* HCI dev flags mapping */
+static hci_map dev_flags_map[] = {
+ { "UP", HCI_UP },
+ { "INIT", HCI_INIT },
+ { "RUNNING", HCI_RUNNING },
+ { "RAW", HCI_RAW },
+ { "PSCAN", HCI_PSCAN },
+ { "ISCAN", HCI_ISCAN },
+ { "INQUIRY", HCI_INQUIRY },
+ { "AUTH", HCI_AUTH },
+ { "ENCRYPT", HCI_ENCRYPT },
+ { "SECMGR", HCI_SECMGR },
+ { NULL }
+};
+
+char *hci_dflagstostr(uint32_t flags)
+{
+ char *str = bt_malloc(50);
+ char *ptr = str;
+ hci_map *m = dev_flags_map;
+
+ if (!str)
+ return NULL;
+
+ *ptr = 0;
+
+ if (!hci_test_bit(HCI_UP, &flags))
+ ptr += sprintf(ptr, "DOWN ");
+
+ while (m->str) {
+ if (hci_test_bit(m->val, &flags))
+ ptr += sprintf(ptr, "%s ", m->str);
+ m++;
+ }
+ return str;
+}
+
+/* HCI packet type mapping */
+static hci_map pkt_type_map[] = {
+ { "DM1", HCI_DM1 },
+ { "DM3", HCI_DM3 },
+ { "DM5", HCI_DM5 },
+ { "DH1", HCI_DH1 },
+ { "DH3", HCI_DH3 },
+ { "DH5", HCI_DH5 },
+ { "HV1", HCI_HV1 },
+ { "HV2", HCI_HV2 },
+ { "HV3", HCI_HV3 },
+ { "2-DH1", HCI_2DH1 },
+ { "2-DH3", HCI_2DH3 },
+ { "2-DH5", HCI_2DH5 },
+ { "3-DH1", HCI_3DH1 },
+ { "3-DH3", HCI_3DH3 },
+ { "3-DH5", HCI_3DH5 },
+ { NULL }
+};
+
+static hci_map sco_ptype_map[] = {
+ { "HV1", 0x0001 },
+ { "HV2", 0x0002 },
+ { "HV3", 0x0004 },
+ { "EV3", HCI_EV3 },
+ { "EV4", HCI_EV4 },
+ { "EV5", HCI_EV5 },
+ { "2-EV3", HCI_2EV3 },
+ { "2-EV5", HCI_2EV5 },
+ { "3-EV3", HCI_3EV3 },
+ { "3-EV5", HCI_3EV5 },
+ { NULL }
+};
+
+char *hci_ptypetostr(unsigned int ptype)
+{
+ return hci_bit2str(pkt_type_map, ptype);
+}
+
+int hci_strtoptype(char *str, unsigned int *val)
+{
+ return hci_str2bit(pkt_type_map, str, val);
+}
+
+char *hci_scoptypetostr(unsigned int ptype)
+{
+ return hci_bit2str(sco_ptype_map, ptype);
+}
+
+int hci_strtoscoptype(char *str, unsigned int *val)
+{
+ return hci_str2bit(sco_ptype_map, str, val);
+}
+
+/* Link policy mapping */
+static hci_map link_policy_map[] = {
+ { "NONE", 0 },
+ { "RSWITCH", HCI_LP_RSWITCH },
+ { "HOLD", HCI_LP_HOLD },
+ { "SNIFF", HCI_LP_SNIFF },
+ { "PARK", HCI_LP_PARK },
+ { NULL }
+};
+
+char *hci_lptostr(unsigned int lp)
+{
+ return hci_bit2str(link_policy_map, lp);
+}
+
+int hci_strtolp(char *str, unsigned int *val)
+{
+ return hci_str2bit(link_policy_map, str, val);
+}
+
+/* Link mode mapping */
+static hci_map link_mode_map[] = {
+ { "NONE", 0 },
+ { "ACCEPT", HCI_LM_ACCEPT },
+ { "MASTER", HCI_LM_MASTER },
+ { "AUTH", HCI_LM_AUTH },
+ { "ENCRYPT", HCI_LM_ENCRYPT },
+ { "TRUSTED", HCI_LM_TRUSTED },
+ { "RELIABLE", HCI_LM_RELIABLE },
+ { "SECURE", HCI_LM_SECURE },
+ { NULL }
+};
+
+char *hci_lmtostr(unsigned int lm)
+{
+ char *s, *str = bt_malloc(50);
+ if (!str)
+ return NULL;
+
+ *str = 0;
+ if (!(lm & HCI_LM_MASTER))
+ strcpy(str, "SLAVE ");
+
+ s = hci_bit2str(link_mode_map, lm);
+ if (!s) {
+ bt_free(str);
+ return NULL;
+ }
+
+ strcat(str, s);
+ free(s);
+ return str;
+}
+
+int hci_strtolm(char *str, unsigned int *val)
+{
+ return hci_str2bit(link_mode_map, str, val);
+}
+
+/* Command mapping */
+static hci_map commands_map[] = {
+ { "Inquiry", 0 },
+ { "Inquiry Cancel", 1 },
+ { "Periodic Inquiry Mode", 2 },
+ { "Exit Periodic Inquiry Mode", 3 },
+ { "Create Connection", 4 },
+ { "Disconnect", 5 },
+ { "Add SCO Connection", 6 },
+ { "Cancel Create Connection", 7 },
+
+ { "Accept Connection Request", 8 },
+ { "Reject Connection Request", 9 },
+ { "Link Key Request Reply", 10 },
+ { "Link Key Request Negative Reply", 11 },
+ { "PIN Code Request Reply", 12 },
+ { "PIN Code Request Negative Reply", 13 },
+ { "Change Connection Packet Type", 14 },
+ { "Authentication Requested", 15 },
+
+ { "Set Connection Encryption", 16 },
+ { "Change Connection Link Key", 17 },
+ { "Master Link Key", 18 },
+ { "Remote Name Request", 19 },
+ { "Cancel Remote Name Request", 20 },
+ { "Read Remote Supported Features", 21 },
+ { "Read Remote Extended Features", 22 },
+ { "Read Remote Version Information", 23 },
+
+ { "Read Clock Offset", 24 },
+ { "Read LMP Handle", 25 },
+ { "Reserved", 26 },
+ { "Reserved", 27 },
+ { "Reserved", 28 },
+ { "Reserved", 29 },
+ { "Reserved", 30 },
+ { "Reserved", 31 },
+
+ { "Reserved", 32 },
+ { "Hold Mode", 33 },
+ { "Sniff Mode", 34 },
+ { "Exit Sniff Mode", 35 },
+ { "Park State", 36 },
+ { "Exit Park State", 37 },
+ { "QoS Setup", 38 },
+ { "Role Discovery", 39 },
+
+ { "Switch Role", 40 },
+ { "Read Link Policy Settings", 41 },
+ { "Write Link Policy Settings", 42 },
+ { "Read Default Link Policy Settings", 43 },
+ { "Write Default Link Policy Settings", 44 },
+ { "Flow Specification", 45 },
+ { "Set Event Mask", 46 },
+ { "Reset", 47 },
+
+ { "Set Event Filter", 48 },
+ { "Flush", 49 },
+ { "Read PIN Type", 50 },
+ { "Write PIN Type", 51 },
+ { "Create New Unit Key", 52 },
+ { "Read Stored Link Key", 53 },
+ { "Write Stored Link Key", 54 },
+ { "Delete Stored Link Key", 55 },
+
+ { "Write Local Name", 56 },
+ { "Read Local Name", 57 },
+ { "Read Connection Accept Timeout", 58 },
+ { "Write Connection Accept Timeout", 59 },
+ { "Read Page Timeout", 60 },
+ { "Write Page Timeout", 61 },
+ { "Read Scan Enable", 62 },
+ { "Write Scan Enable", 63 },
+
+ { "Read Page Scan Activity", 64 },
+ { "Write Page Scan Activity", 65 },
+ { "Read Inquiry Scan Activity", 66 },
+ { "Write Inquiry Scan Activity", 67 },
+ { "Read Authentication Enable", 68 },
+ { "Write Authentication Enable", 69 },
+ { "Read Encryption Mode", 70 },
+ { "Write Encryption Mode", 71 },
+
+ { "Read Class Of Device", 72 },
+ { "Write Class Of Device", 73 },
+ { "Read Voice Setting", 74 },
+ { "Write Voice Setting", 75 },
+ { "Read Automatic Flush Timeout", 76 },
+ { "Write Automatic Flush Timeout", 77 },
+ { "Read Num Broadcast Retransmissions", 78 },
+ { "Write Num Broadcast Retransmissions", 79 },
+
+ { "Read Hold Mode Activity", 80 },
+ { "Write Hold Mode Activity", 81 },
+ { "Read Transmit Power Level", 82 },
+ { "Read Synchronous Flow Control Enable", 83 },
+ { "Write Synchronous Flow Control Enable", 84 },
+ { "Set Host Controller To Host Flow Control", 85 },
+ { "Host Buffer Size", 86 },
+ { "Host Number Of Completed Packets", 87 },
+
+ { "Read Link Supervision Timeout", 88 },
+ { "Write Link Supervision Timeout", 89 },
+ { "Read Number of Supported IAC", 90 },
+ { "Read Current IAC LAP", 91 },
+ { "Write Current IAC LAP", 92 },
+ { "Read Page Scan Period Mode", 93 },
+ { "Write Page Scan Period Mode", 94 },
+ { "Read Page Scan Mode", 95 },
+
+ { "Write Page Scan Mode", 96 },
+ { "Set AFH Channel Classification", 97 },
+ { "Reserved", 98 },
+ { "Reserved", 99 },
+ { "Read Inquiry Scan Type", 100 },
+ { "Write Inquiry Scan Type", 101 },
+ { "Read Inquiry Mode", 102 },
+ { "Write Inquiry Mode", 103 },
+
+ { "Read Page Scan Type", 104 },
+ { "Write Page Scan Type", 105 },
+ { "Read AFH Channel Assessment Mode", 106 },
+ { "Write AFH Channel Assessment Mode", 107 },
+ { "Reserved", 108 },
+ { "Reserved", 109 },
+ { "Reserved", 110 },
+ { "Reserved", 111 },
+
+ { "Reserved", 112 },
+ { "Reserved", 113 },
+ { "Reserved", 114 },
+ { "Read Local Version Information", 115 },
+ { "Read Local Supported Commands", 116 },
+ { "Read Local Supported Features", 117 },
+ { "Read Local Extended Features", 118 },
+ { "Read Buffer Size", 119 },
+
+ { "Read Country Code", 120 },
+ { "Read BD ADDR", 121 },
+ { "Read Failed Contact Counter", 122 },
+ { "Reset Failed Contact Counter", 123 },
+ { "Get Link Quality", 124 },
+ { "Read RSSI", 125 },
+ { "Read AFH Channel Map", 126 },
+ { "Read BD Clock", 127 },
+
+ { "Read Loopback Mode", 128 },
+ { "Write Loopback Mode", 129 },
+ { "Enable Device Under Test Mode", 130 },
+ { "Setup Synchronous Connection", 131 },
+ { "Accept Synchronous Connection", 132 },
+ { "Reject Synchronous Connection", 133 },
+ { "Reserved", 134 },
+ { "Reserved", 135 },
+
+ { "Read Extended Inquiry Response", 136 },
+ { "Write Extended Inquiry Response", 137 },
+ { "Refresh Encryption Key", 138 },
+ { "Reserved", 139 },
+ { "Sniff Subrating", 140 },
+ { "Read Simple Pairing Mode", 141 },
+ { "Write Simple Pairing Mode", 142 },
+ { "Read Local OOB Data", 143 },
+
+ { "Read Inquiry Transmit Power Level", 144 },
+ { "Write Inquiry Transmit Power Level", 145 },
+ { "Read Default Erroneous Data Reporting", 146 },
+ { "Write Default Erroneous Data Reporting", 147 },
+ { "Reserved", 148 },
+ { "Reserved", 149 },
+ { "Reserved", 150 },
+ { "IO Capability Request Reply", 151 },
+
+ { "User Confirmation Request Reply", 152 },
+ { "User Confirmation Request Negative Reply", 153 },
+ { "User Passkey Request Reply", 154 },
+ { "User Passkey Request Negative Reply", 155 },
+ { "Remote OOB Data Request Reply", 156 },
+ { "Write Simple Pairing Debug Mode", 157 },
+ { "Enhanced Flush", 158 },
+ { "Remote OOB Data Request Negative Reply", 159 },
+
+ { "Reserved", 160 },
+ { "Reserved", 161 },
+ { "Send Keypress Notification", 162 },
+ { "IO Capabilities Response Negative Reply", 163 },
+ { "Reserved", 164 },
+ { "Reserved", 165 },
+ { "Reserved", 166 },
+ { "Reserved", 167 },
+
+ { NULL }
+};
+
+char *hci_cmdtostr(unsigned int cmd)
+{
+ return hci_uint2str(commands_map, cmd);
+}
+
+char *hci_commandstostr(uint8_t *commands, char *pref, int width)
+{
+ hci_map *m;
+ char *off, *ptr, *str;
+ int size = 10;
+
+ m = commands_map;
+
+ while (m->str) {
+ if (commands[m->val / 8] & (1 << (m->val % 8)))
+ size += strlen(m->str) + (pref ? strlen(pref) : 0) + 3;
+ m++;
+ }
+
+ str = bt_malloc(size);
+ if (!str)
+ return NULL;
+
+ ptr = str; *ptr = '\0';
+
+ if (pref)
+ ptr += sprintf(ptr, "%s", pref);
+
+ off = ptr;
+
+ m = commands_map;
+
+ while (m->str) {
+ if (commands[m->val / 8] & (1 << (m->val % 8))) {
+ if (strlen(off) + strlen(m->str) > width - 3) {
+ ptr += sprintf(ptr, "\n%s", pref ? pref : "");
+ off = ptr;
+ }
+ ptr += sprintf(ptr, "'%s' ", m->str);
+ }
+ m++;
+ }
+
+ return str;
+}
+
+/* Version mapping */
+static hci_map ver_map[] = {
+ { "1.0b", 0x00 },
+ { "1.1", 0x01 },
+ { "1.2", 0x02 },
+ { "2.0", 0x03 },
+ { "2.1", 0x04 },
+ { NULL }
+};
+
+char *hci_vertostr(unsigned int ver)
+{
+ return hci_uint2str(ver_map, ver);
+}
+
+int hci_strtover(char *str, unsigned int *ver)
+{
+ return hci_str2uint(ver_map, str, ver);
+}
+
+char *lmp_vertostr(unsigned int ver)
+{
+ return hci_uint2str(ver_map, ver);
+}
+
+int lmp_strtover(char *str, unsigned int *ver)
+{
+ return hci_str2uint(ver_map, str, ver);
+}
+
+/* LMP features mapping */
+static hci_map lmp_features_map[8][9] = {
+ { /* Byte 0 */
+ { "<3-slot packets>", LMP_3SLOT }, /* Bit 0 */
+ { "<5-slot packets>", LMP_5SLOT }, /* Bit 1 */
+ { "<encryption>", LMP_ENCRYPT }, /* Bit 2 */
+ { "<slot offset>", LMP_SOFFSET }, /* Bit 3 */
+ { "<timing accuracy>", LMP_TACCURACY }, /* Bit 4 */
+ { "<role switch>", LMP_RSWITCH }, /* Bit 5 */
+ { "<hold mode>", LMP_HOLD }, /* Bit 6 */
+ { "<sniff mode>", LMP_SNIFF }, /* Bit 7 */
+ { NULL }
+ },
+ { /* Byte 1 */
+ { "<park state>", LMP_PARK }, /* Bit 0 */
+ { "<RSSI>", LMP_RSSI }, /* Bit 1 */
+ { "<channel quality>", LMP_QUALITY }, /* Bit 2 */
+ { "<SCO link>", LMP_SCO }, /* Bit 3 */
+ { "<HV2 packets>", LMP_HV2 }, /* Bit 4 */
+ { "<HV3 packets>", LMP_HV3 }, /* Bit 5 */
+ { "<u-law log>", LMP_ULAW }, /* Bit 6 */
+ { "<A-law log>", LMP_ALAW }, /* Bit 7 */
+ { NULL }
+ },
+ { /* Byte 2 */
+ { "<CVSD>", LMP_CVSD }, /* Bit 0 */
+ { "<paging scheme>", LMP_PSCHEME }, /* Bit 1 */
+ { "<power control>", LMP_PCONTROL }, /* Bit 2 */
+ { "<transparent SCO>", LMP_TRSP_SCO }, /* Bit 3 */
+ { "<broadcast encrypt>",LMP_BCAST_ENC }, /* Bit 7 */
+ { NULL }
+ },
+ { /* Byte 3 */
+ { "<no. 24>", 0x01 }, /* Bit 0 */
+ { "<EDR ACL 2 Mbps>", LMP_EDR_ACL_2M }, /* Bit 1 */
+ { "<EDR ACL 3 Mbps>", LMP_EDR_ACL_3M }, /* Bit 2 */
+ { "<enhanced iscan>", LMP_ENH_ISCAN }, /* Bit 3 */
+ { "<interlaced iscan>", LMP_ILACE_ISCAN }, /* Bit 4 */
+ { "<interlaced pscan>", LMP_ILACE_PSCAN }, /* Bit 5 */
+ { "<inquiry with RSSI>",LMP_RSSI_INQ }, /* Bit 6 */
+ { "<extended SCO>", LMP_ESCO }, /* Bit 7 */
+ { NULL }
+ },
+ { /* Byte 4 */
+ { "<EV4 packets>", LMP_EV4 }, /* Bit 0 */
+ { "<EV5 packets>", LMP_EV5 }, /* Bit 1 */
+ { "<no. 34>", 0x04 }, /* Bit 2 */
+ { "<AFH cap. slave>", LMP_AFH_CAP_SLV }, /* Bit 3 */
+ { "<AFH class. slave>", LMP_AFH_CLS_SLV }, /* Bit 4 */
+ { "<no. 37>", 0x20 }, /* Bit 5 */
+ { "<no. 38>", 0x40 }, /* Bit 6 */
+ { "<3-slot EDR ACL>", LMP_EDR_3SLOT }, /* Bit 7 */
+ { NULL }
+ },
+ { /* Byte 5 */
+ { "<5-slot EDR ACL>", LMP_EDR_5SLOT }, /* Bit 0 */
+ { "<sniff subrating>", LMP_SNIFF_SUBR }, /* Bit 1 */
+ { "<pause encryption>", LMP_PAUSE_ENC }, /* Bit 2 */
+ { "<AFH cap. master>", LMP_AFH_CAP_MST }, /* Bit 3 */
+ { "<AFH class. master>",LMP_AFH_CLS_MST }, /* Bit 4 */
+ { "<EDR eSCO 2 Mbps>", LMP_EDR_ESCO_2M }, /* Bit 5 */
+ { "<EDR eSCO 3 Mbps>", LMP_EDR_ESCO_3M }, /* Bit 6 */
+ { "<3-slot EDR eSCO>", LMP_EDR_3S_ESCO }, /* Bit 7 */
+ { NULL }
+ },
+ { /* Byte 6 */
+ { "<extended inquiry>", LMP_EXT_INQ }, /* Bit 0 */
+ { "<no. 49>", 0x02 }, /* Bit 1 */
+ { "<no. 50>", 0x04 }, /* Bit 2 */
+ { "<simple pairing>", LMP_SIMPLE_PAIR }, /* Bit 3 */
+ { "<encapsulated PDU>", LMP_ENCAPS_PDU }, /* Bit 4 */
+ { "<err. data report>", LMP_ERR_DAT_REP }, /* Bit 5 */
+ { "<non-flush flag>", LMP_NFLUSH_PKTS }, /* Bit 6 */
+ { "<no. 55>", 0x80 }, /* Bit 7 */
+ { NULL }
+ },
+ { /* Byte 7 */
+ { "<LSTO>", LMP_LSTO }, /* Bit 1 */
+ { "<inquiry TX power>", LMP_INQ_TX_PWR }, /* Bit 1 */
+ { "<no. 58>", 0x04 }, /* Bit 2 */
+ { "<no. 59>", 0x08 }, /* Bit 3 */
+ { "<no. 60>", 0x10 }, /* Bit 4 */
+ { "<no. 61>", 0x20 }, /* Bit 5 */
+ { "<no. 62>", 0x40 }, /* Bit 6 */
+ { "<extended features>",LMP_EXT_FEAT }, /* Bit 7 */
+ { NULL }
+ },
+};
+
+char *lmp_featurestostr(uint8_t *features, char *pref, int width)
+{
+ char *off, *ptr, *str;
+ int i, size = 10;
+
+ for (i = 0; i < 8; i++) {
+ hci_map *m = lmp_features_map[i];
+
+ while (m->str) {
+ if (m->val & features[i])
+ size += strlen(m->str) + (pref ? strlen(pref) : 0) + 1;
+ m++;
+ }
+ }
+
+ str = bt_malloc(size);
+ if (!str)
+ return NULL;
+
+ ptr = str; *ptr = '\0';
+
+ if (pref)
+ ptr += sprintf(ptr, "%s", pref);
+
+ off = ptr;
+
+ for (i = 0; i < 8; i++) {
+ hci_map *m = lmp_features_map[i];
+
+ while (m->str) {
+ if (m->val & features[i]) {
+ if (strlen(off) + strlen(m->str) > width - 1) {
+ ptr += sprintf(ptr, "\n%s", pref ? pref : "");
+ off = ptr;
+ }
+ ptr += sprintf(ptr, "%s ", m->str);
+ }
+ m++;
+ }
+ }
+
+ return str;
+}
+
+/* HCI functions that do not require open device */
+
+int hci_for_each_dev(int flag, int (*func)(int dd, int dev_id, long arg), long arg)
+{
+ struct hci_dev_list_req *dl;
+ struct hci_dev_req *dr;
+ int dev_id = -1;
+ int i, sk, err = 0;
+
+ sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
+ if (sk < 0)
+ return -1;
+
+ dl = malloc(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl));
+ if (!dl) {
+ err = errno;
+ goto done;
+ }
+
+ memset(dl, 0, HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl));
+
+ dl->dev_num = HCI_MAX_DEV;
+ dr = dl->dev_req;
+
+ if (ioctl(sk, HCIGETDEVLIST, (void *) dl) < 0) {
+ err = errno;
+ goto free;
+ }
+
+ for (i = 0; i < dl->dev_num; i++, dr++) {
+ if (hci_test_bit(flag, &dr->dev_opt))
+ if (!func || func(sk, dr->dev_id, arg)) {
+ dev_id = dr->dev_id;
+ break;
+ }
+ }
+
+ if (dev_id < 0)
+ err = ENODEV;
+
+free:
+ free(dl);
+
+done:
+ close(sk);
+ errno = err;
+
+ return dev_id;
+}
+
+static int __other_bdaddr(int dd, int dev_id, long arg)
+{
+ struct hci_dev_info di = { dev_id: dev_id };
+
+ if (ioctl(dd, HCIGETDEVINFO, (void *) &di))
+ return 0;
+
+ if (hci_test_bit(HCI_RAW, &di.flags))
+ return 0;
+
+ return bacmp((bdaddr_t *) arg, &di.bdaddr);
+}
+
+static int __same_bdaddr(int dd, int dev_id, long arg)
+{
+ struct hci_dev_info di = { dev_id: dev_id };
+
+ if (ioctl(dd, HCIGETDEVINFO, (void *) &di))
+ return 0;
+
+ return !bacmp((bdaddr_t *) arg, &di.bdaddr);
+}
+
+int hci_get_route(bdaddr_t *bdaddr)
+{
+ return hci_for_each_dev(HCI_UP, __other_bdaddr,
+ (long) (bdaddr ? bdaddr : BDADDR_ANY));
+}
+
+int hci_devid(const char *str)
+{
+ bdaddr_t ba;
+ int id = -1;
+
+ if (!strncmp(str, "hci", 3) && strlen(str) >= 4) {
+ id = atoi(str + 3);
+ if (hci_devba(id, &ba) < 0)
+ return -1;
+ } else {
+ errno = ENODEV;
+ str2ba(str, &ba);
+ id = hci_for_each_dev(HCI_UP, __same_bdaddr, (long) &ba);
+ }
+
+ return id;
+}
+
+int hci_devinfo(int dev_id, struct hci_dev_info *di)
+{
+ int dd, err, ret;
+
+ dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
+ if (dd < 0)
+ return dd;
+
+ memset(di, 0, sizeof(struct hci_dev_info));
+
+ di->dev_id = dev_id;
+ ret = ioctl(dd, HCIGETDEVINFO, (void *) di);
+
+ err = errno;
+ close(dd);
+ errno = err;
+
+ return ret;
+}
+
+int hci_devba(int dev_id, bdaddr_t *bdaddr)
+{
+ struct hci_dev_info di;
+
+ memset(&di, 0, sizeof(di));
+
+ if (hci_devinfo(dev_id, &di))
+ return -1;
+
+ if (!hci_test_bit(HCI_UP, &di.flags)) {
+ errno = ENETDOWN;
+ return -1;
+ }
+
+ bacpy(bdaddr, &di.bdaddr);
+
+ return 0;
+}
+
+int hci_inquiry(int dev_id, int len, int nrsp, const uint8_t *lap, inquiry_info **ii, long flags)
+{
+ struct hci_inquiry_req *ir;
+ uint8_t num_rsp = nrsp;
+ void *buf;
+ int dd, size, err, ret = -1;
+
+ if (nrsp <= 0) {
+ num_rsp = 0;
+ nrsp = 255;
+ }
+
+ if (dev_id < 0) {
+ dev_id = hci_get_route(NULL);
+ if (dev_id < 0) {
+ errno = ENODEV;
+ return -1;
+ }
+ }
+
+ dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
+ if (dd < 0)
+ return dd;
+
+ buf = malloc(sizeof(*ir) + (sizeof(inquiry_info) * (nrsp)));
+ if (!buf)
+ goto done;
+
+ ir = buf;
+ ir->dev_id = dev_id;
+ ir->num_rsp = num_rsp;
+ ir->length = len;
+ ir->flags = flags;
+
+ if (lap) {
+ memcpy(ir->lap, lap, 3);
+ } else {
+ ir->lap[0] = 0x33;
+ ir->lap[1] = 0x8b;
+ ir->lap[2] = 0x9e;
+ }
+
+ ret = ioctl(dd, HCIINQUIRY, (unsigned long) buf);
+ if (ret < 0)
+ goto free;
+
+ size = sizeof(inquiry_info) * ir->num_rsp;
+
+ if (!*ii)
+ *ii = malloc(size);
+
+ if (*ii) {
+ memcpy((void *) *ii, buf + sizeof(*ir), size);
+ ret = ir->num_rsp;
+ } else
+ ret = -1;
+
+free:
+ free(buf);
+
+done:
+ err = errno;
+ close(dd);
+ errno = err;
+
+ return ret;
+}
+
+/* Open HCI device.
+ * Returns device descriptor (dd). */
+int hci_open_dev(int dev_id)
+{
+ struct sockaddr_hci a;
+ int dd, err;
+
+ /* Create HCI socket */
+ dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
+ if (dd < 0)
+ return dd;
+
+ /* Bind socket to the HCI device */
+ memset(&a, 0, sizeof(a));
+ a.hci_family = AF_BLUETOOTH;
+ a.hci_dev = dev_id;
+ if (bind(dd, (struct sockaddr *) &a, sizeof(a)) < 0)
+ goto failed;
+
+ return dd;
+
+failed:
+ err = errno;
+ close(dd);
+ errno = err;
+
+ return -1;
+}
+
+int hci_close_dev(int dd)
+{
+ return close(dd);
+}
+
+/* HCI functions that require open device
+ * dd - Device descriptor returned by hci_open_dev. */
+
+int hci_send_cmd(int dd, uint16_t ogf, uint16_t ocf, uint8_t plen, void *param)
+{
+ uint8_t type = HCI_COMMAND_PKT;
+ hci_command_hdr hc;
+ struct iovec iv[3];
+ int ivn;
+
+ hc.opcode = htobs(cmd_opcode_pack(ogf, ocf));
+ hc.plen= plen;
+
+ iv[0].iov_base = &type;
+ iv[0].iov_len = 1;
+ iv[1].iov_base = &hc;
+ iv[1].iov_len = HCI_COMMAND_HDR_SIZE;
+ ivn = 2;
+
+ if (plen) {
+ iv[2].iov_base = param;
+ iv[2].iov_len = plen;
+ ivn = 3;
+ }
+
+ while (writev(dd, iv, ivn) < 0) {
+ if (errno == EAGAIN || errno == EINTR)
+ continue;
+ return -1;
+ }
+ return 0;
+}
+
+int hci_send_req(int dd, struct hci_request *r, int to)
+{
+ unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr;
+ uint16_t opcode = htobs(cmd_opcode_pack(r->ogf, r->ocf));
+ struct hci_filter nf, of;
+ socklen_t len;
+ hci_event_hdr *hdr;
+ int err, try;
+
+ len = sizeof(of);
+ if (getsockopt(dd, SOL_HCI, HCI_FILTER, &of, &len) < 0)
+ return -1;
+
+ hci_filter_clear(&nf);
+ hci_filter_set_ptype(HCI_EVENT_PKT, &nf);
+ hci_filter_set_event(EVT_CMD_STATUS, &nf);
+ hci_filter_set_event(EVT_CMD_COMPLETE, &nf);
+ hci_filter_set_event(r->event, &nf);
+ hci_filter_set_opcode(opcode, &nf);
+ if (setsockopt(dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf)) < 0)
+ return -1;
+
+ if (hci_send_cmd(dd, r->ogf, r->ocf, r->clen, r->cparam) < 0)
+ goto failed;
+
+ try = 10;
+ while (try--) {
+ evt_cmd_complete *cc;
+ evt_cmd_status *cs;
+ evt_remote_name_req_complete *rn;
+ remote_name_req_cp *cp;
+
+ if (to) {
+ struct pollfd p;
+ int n;
+
+ p.fd = dd; p.events = POLLIN;
+ while ((n = poll(&p, 1, to)) < 0) {
+ if (errno == EAGAIN || errno == EINTR)
+ continue;
+ goto failed;
+ }
+
+ if (!n) {
+ errno = ETIMEDOUT;
+ goto failed;
+ }
+
+ to -= 10;
+ if (to < 0) to = 0;
+
+ }
+
+ while ((len = read(dd, buf, sizeof(buf))) < 0) {
+ if (errno == EAGAIN || errno == EINTR)
+ continue;
+ goto failed;
+ }
+
+ hdr = (void *) (buf + 1);
+ ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
+ len -= (1 + HCI_EVENT_HDR_SIZE);
+
+ switch (hdr->evt) {
+ case EVT_CMD_STATUS:
+ cs = (void *) ptr;
+
+ if (cs->opcode != opcode)
+ continue;
+
+ if (r->event != EVT_CMD_STATUS) {
+ if (cs->status) {
+ errno = EIO;
+ goto failed;
+ }
+ break;
+ }
+
+ r->rlen = MIN(len, r->rlen);
+ memcpy(r->rparam, ptr, r->rlen);
+ goto done;
+
+ case EVT_CMD_COMPLETE:
+ cc = (void *) ptr;
+
+ if (cc->opcode != opcode)
+ continue;
+
+ ptr += EVT_CMD_COMPLETE_SIZE;
+ len -= EVT_CMD_COMPLETE_SIZE;
+
+ r->rlen = MIN(len, r->rlen);
+ memcpy(r->rparam, ptr, r->rlen);
+ goto done;
+
+ case EVT_REMOTE_NAME_REQ_COMPLETE:
+ if (hdr->evt != r->event)
+ break;
+
+ rn = (void *) ptr;
+ cp = r->cparam;
+
+ if (bacmp(&rn->bdaddr, &cp->bdaddr))
+ continue;
+
+ r->rlen = MIN(len, r->rlen);
+ memcpy(r->rparam, ptr, r->rlen);
+ goto done;
+
+ default:
+ if (hdr->evt != r->event)
+ break;
+
+ r->rlen = MIN(len, r->rlen);
+ memcpy(r->rparam, ptr, r->rlen);
+ goto done;
+ }
+ }
+ errno = ETIMEDOUT;
+
+failed:
+ err = errno;
+ setsockopt(dd, SOL_HCI, HCI_FILTER, &of, sizeof(of));
+ errno = err;
+ return -1;
+
+done:
+ setsockopt(dd, SOL_HCI, HCI_FILTER, &of, sizeof(of));
+ return 0;
+}
+
+int hci_create_connection(int dd, const bdaddr_t *bdaddr, uint16_t ptype, uint16_t clkoffset, uint8_t rswitch, uint16_t *handle, int to)
+{
+ evt_conn_complete rp;
+ create_conn_cp cp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ bacpy(&cp.bdaddr, bdaddr);
+ cp.pkt_type = ptype;
+ cp.pscan_rep_mode = 0x02;
+ cp.clock_offset = clkoffset;
+ cp.role_switch = rswitch;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LINK_CTL;
+ rq.ocf = OCF_CREATE_CONN;
+ rq.event = EVT_CONN_COMPLETE;
+ rq.cparam = &cp;
+ rq.clen = CREATE_CONN_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = EVT_CONN_COMPLETE_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *handle = rp.handle;
+ return 0;
+}
+
+int hci_disconnect(int dd, uint16_t handle, uint8_t reason, int to)
+{
+ evt_disconn_complete rp;
+ disconnect_cp cp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.handle = handle;
+ cp.reason = reason;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LINK_CTL;
+ rq.ocf = OCF_DISCONNECT;
+ rq.event = EVT_DISCONN_COMPLETE;
+ rq.cparam = &cp;
+ rq.clen = DISCONNECT_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = EVT_DISCONN_COMPLETE_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+ return 0;
+}
+
+int hci_read_local_name(int dd, int len, char *name, int to)
+{
+ read_local_name_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_LOCAL_NAME;
+ rq.rparam = &rp;
+ rq.rlen = READ_LOCAL_NAME_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ rp.name[247] = '\0';
+ strncpy(name, (char *) rp.name, len);
+ return 0;
+}
+
+int hci_write_local_name(int dd, const char *name, int to)
+{
+ change_local_name_cp cp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ strncpy((char *) cp.name, name, sizeof(cp.name));
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_CHANGE_LOCAL_NAME;
+ rq.cparam = &cp;
+ rq.clen = CHANGE_LOCAL_NAME_CP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ return 0;
+}
+
+int hci_read_remote_name_with_clock_offset(int dd, const bdaddr_t *bdaddr, uint8_t pscan_rep_mode, uint16_t clkoffset, int len, char *name, int to)
+{
+ evt_remote_name_req_complete rn;
+ remote_name_req_cp cp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ bacpy(&cp.bdaddr, bdaddr);
+ cp.pscan_rep_mode = pscan_rep_mode;
+ cp.clock_offset = clkoffset;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LINK_CTL;
+ rq.ocf = OCF_REMOTE_NAME_REQ;
+ rq.cparam = &cp;
+ rq.clen = REMOTE_NAME_REQ_CP_SIZE;
+ rq.event = EVT_REMOTE_NAME_REQ_COMPLETE;
+ rq.rparam = &rn;
+ rq.rlen = EVT_REMOTE_NAME_REQ_COMPLETE_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rn.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ rn.name[247] = '\0';
+ strncpy(name, (char *) rn.name, len);
+ return 0;
+}
+
+int hci_read_remote_name(int dd, const bdaddr_t *bdaddr, int len, char *name, int to)
+{
+ return hci_read_remote_name_with_clock_offset(dd, bdaddr, 0x02, 0x0000, len, name, to);
+}
+
+int hci_read_remote_name_cancel(int dd, const bdaddr_t *bdaddr, int to)
+{
+ remote_name_req_cancel_cp cp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ bacpy(&cp.bdaddr, bdaddr);
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LINK_CTL;
+ rq.ocf = OCF_REMOTE_NAME_REQ_CANCEL;
+ rq.cparam = &cp;
+ rq.clen = REMOTE_NAME_REQ_CANCEL_CP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ return 0;
+}
+
+int hci_read_remote_version(int dd, uint16_t handle, struct hci_version *ver, int to)
+{
+ evt_read_remote_version_complete rp;
+ read_remote_version_cp cp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.handle = handle;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LINK_CTL;
+ rq.ocf = OCF_READ_REMOTE_VERSION;
+ rq.event = EVT_READ_REMOTE_VERSION_COMPLETE;
+ rq.cparam = &cp;
+ rq.clen = READ_REMOTE_VERSION_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = EVT_READ_REMOTE_VERSION_COMPLETE_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ ver->manufacturer = btohs(rp.manufacturer);
+ ver->lmp_ver = rp.lmp_ver;
+ ver->lmp_subver = btohs(rp.lmp_subver);
+ return 0;
+}
+
+int hci_read_remote_features(int dd, uint16_t handle, uint8_t *features, int to)
+{
+ evt_read_remote_features_complete rp;
+ read_remote_features_cp cp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.handle = handle;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LINK_CTL;
+ rq.ocf = OCF_READ_REMOTE_FEATURES;
+ rq.event = EVT_READ_REMOTE_FEATURES_COMPLETE;
+ rq.cparam = &cp;
+ rq.clen = READ_REMOTE_FEATURES_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = EVT_READ_REMOTE_FEATURES_COMPLETE_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ memcpy(features, rp.features, 8);
+ return 0;
+}
+
+int hci_read_remote_ext_features(int dd, uint16_t handle, uint8_t page, uint8_t *max_page, uint8_t *features, int to)
+{
+ evt_read_remote_ext_features_complete rp;
+ read_remote_ext_features_cp cp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.handle = handle;
+ cp.page_num = page;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LINK_CTL;
+ rq.ocf = OCF_READ_REMOTE_EXT_FEATURES;
+ rq.event = EVT_READ_REMOTE_EXT_FEATURES_COMPLETE;
+ rq.cparam = &cp;
+ rq.clen = READ_REMOTE_EXT_FEATURES_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = EVT_READ_REMOTE_EXT_FEATURES_COMPLETE_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *max_page = rp.max_page_num;
+ memcpy(features, rp.features, 8);
+ return 0;
+}
+
+int hci_read_clock_offset(int dd, uint16_t handle, uint16_t *clkoffset, int to)
+{
+ evt_read_clock_offset_complete rp;
+ read_clock_offset_cp cp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.handle = handle;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LINK_CTL;
+ rq.ocf = OCF_READ_CLOCK_OFFSET;
+ rq.event = EVT_READ_CLOCK_OFFSET_COMPLETE;
+ rq.cparam = &cp;
+ rq.clen = READ_CLOCK_OFFSET_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = EVT_READ_CLOCK_OFFSET_COMPLETE_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *clkoffset = rp.clock_offset;
+ return 0;
+}
+
+int hci_read_local_version(int dd, struct hci_version *ver, int to)
+{
+ read_local_version_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_INFO_PARAM;
+ rq.ocf = OCF_READ_LOCAL_VERSION;
+ rq.rparam = &rp;
+ rq.rlen = READ_LOCAL_VERSION_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ ver->manufacturer = btohs(rp.manufacturer);
+ ver->hci_ver = rp.hci_ver;
+ ver->hci_rev = btohs(rp.hci_rev);
+ ver->lmp_ver = rp.lmp_ver;
+ ver->lmp_subver = btohs(rp.lmp_subver);
+ return 0;
+}
+
+int hci_read_local_commands(int dd, uint8_t *commands, int to)
+{
+ read_local_commands_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_INFO_PARAM;
+ rq.ocf = OCF_READ_LOCAL_COMMANDS;
+ rq.rparam = &rp;
+ rq.rlen = READ_LOCAL_COMMANDS_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ memcpy(commands, rp.commands, 64);
+ return 0;
+}
+
+int hci_read_local_features(int dd, uint8_t *features, int to)
+{
+ read_local_features_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_INFO_PARAM;
+ rq.ocf = OCF_READ_LOCAL_FEATURES;
+ rq.rparam = &rp;
+ rq.rlen = READ_LOCAL_FEATURES_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ memcpy(features, rp.features, 8);
+ return 0;
+}
+
+int hci_read_local_ext_features(int dd, uint8_t page, uint8_t *max_page, uint8_t *features, int to)
+{
+ read_local_ext_features_cp cp;
+ read_local_ext_features_rp rp;
+ struct hci_request rq;
+
+ cp.page_num = page;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_INFO_PARAM;
+ rq.ocf = OCF_READ_LOCAL_EXT_FEATURES;
+ rq.cparam = &cp;
+ rq.clen = READ_LOCAL_EXT_FEATURES_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = READ_LOCAL_EXT_FEATURES_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *max_page = rp.max_page_num;
+ memcpy(features, rp.features, 8);
+ return 0;
+}
+
+int hci_read_bd_addr(int dd, bdaddr_t *bdaddr, int to)
+{
+ read_bd_addr_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_INFO_PARAM;
+ rq.ocf = OCF_READ_BD_ADDR;
+ rq.rparam = &rp;
+ rq.rlen = READ_BD_ADDR_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ bacpy(bdaddr, &rp.bdaddr);
+ return 0;
+}
+
+int hci_read_class_of_dev(int dd, uint8_t *cls, int to)
+{
+ read_class_of_dev_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_CLASS_OF_DEV;
+ rq.rparam = &rp;
+ rq.rlen = READ_CLASS_OF_DEV_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ memcpy(cls, rp.dev_class, 3);
+ return 0;
+}
+
+int hci_write_class_of_dev(int dd, uint32_t cls, int to)
+{
+ write_class_of_dev_cp cp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ cp.dev_class[0] = cls & 0xff;
+ cp.dev_class[1] = (cls >> 8) & 0xff;
+ cp.dev_class[2] = (cls >> 16) & 0xff;
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_WRITE_CLASS_OF_DEV;
+ rq.cparam = &cp;
+ rq.clen = WRITE_CLASS_OF_DEV_CP_SIZE;
+ return hci_send_req(dd, &rq, to);
+}
+
+int hci_read_voice_setting(int dd, uint16_t *vs, int to)
+{
+ read_voice_setting_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_VOICE_SETTING;
+ rq.rparam = &rp;
+ rq.rlen = READ_VOICE_SETTING_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *vs = rp.voice_setting;
+ return 0;
+}
+
+int hci_write_voice_setting(int dd, uint16_t vs, int to)
+{
+ write_voice_setting_cp cp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ cp.voice_setting = vs;
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_WRITE_VOICE_SETTING;
+ rq.cparam = &cp;
+ rq.clen = WRITE_VOICE_SETTING_CP_SIZE;
+
+ return hci_send_req(dd, &rq, to);
+}
+
+int hci_read_current_iac_lap(int dd, uint8_t *num_iac, uint8_t *lap, int to)
+{
+ read_current_iac_lap_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_CURRENT_IAC_LAP;
+ rq.rparam = &rp;
+ rq.rlen = READ_CURRENT_IAC_LAP_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *num_iac = rp.num_current_iac;
+ memcpy(lap, rp.lap, rp.num_current_iac * 3);
+ return 0;
+}
+
+int hci_write_current_iac_lap(int dd, uint8_t num_iac, uint8_t *lap, int to)
+{
+ write_current_iac_lap_cp cp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.num_current_iac = num_iac;
+ memcpy(&cp.lap, lap, num_iac * 3);
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_WRITE_CURRENT_IAC_LAP;
+ rq.cparam = &cp;
+ rq.clen = num_iac * 3 + 1;
+
+ return hci_send_req(dd, &rq, to);
+}
+
+int hci_read_stored_link_key(int dd, bdaddr_t *bdaddr, uint8_t all, int to)
+{
+ read_stored_link_key_cp cp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ bacpy(&cp.bdaddr, bdaddr);
+ cp.read_all = all;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_STORED_LINK_KEY;
+ rq.cparam = &cp;
+ rq.clen = READ_STORED_LINK_KEY_CP_SIZE;
+
+ return hci_send_req(dd, &rq, to);
+}
+
+int hci_write_stored_link_key(int dd, bdaddr_t *bdaddr, uint8_t *key, int to)
+{
+ unsigned char cp[WRITE_STORED_LINK_KEY_CP_SIZE + 6 + 16];
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp[0] = 1;
+ bacpy((bdaddr_t *) (cp + 1), bdaddr);
+ memcpy(cp + 7, key, 16);
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_WRITE_STORED_LINK_KEY;
+ rq.cparam = &cp;
+ rq.clen = WRITE_STORED_LINK_KEY_CP_SIZE + 6 + 16;
+
+ return hci_send_req(dd, &rq, to);
+}
+
+int hci_delete_stored_link_key(int dd, bdaddr_t *bdaddr, uint8_t all, int to)
+{
+ delete_stored_link_key_cp cp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ bacpy(&cp.bdaddr, bdaddr);
+ cp.delete_all = all;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_DELETE_STORED_LINK_KEY;
+ rq.cparam = &cp;
+ rq.clen = DELETE_STORED_LINK_KEY_CP_SIZE;
+
+ return hci_send_req(dd, &rq, to);
+}
+
+int hci_authenticate_link(int dd, uint16_t handle, int to)
+{
+ auth_requested_cp cp;
+ evt_auth_complete rp;
+ struct hci_request rq;
+
+ cp.handle = handle;
+
+ rq.ogf = OGF_LINK_CTL;
+ rq.ocf = OCF_AUTH_REQUESTED;
+ rq.event = EVT_AUTH_COMPLETE;
+ rq.cparam = &cp;
+ rq.clen = AUTH_REQUESTED_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = EVT_AUTH_COMPLETE_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_encrypt_link(int dd, uint16_t handle, uint8_t encrypt, int to)
+{
+ set_conn_encrypt_cp cp;
+ evt_encrypt_change rp;
+ struct hci_request rq;
+
+ cp.handle = handle;
+ cp.encrypt = encrypt;
+
+ rq.ogf = OGF_LINK_CTL;
+ rq.ocf = OCF_SET_CONN_ENCRYPT;
+ rq.event = EVT_ENCRYPT_CHANGE;
+ rq.cparam = &cp;
+ rq.clen = SET_CONN_ENCRYPT_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = EVT_ENCRYPT_CHANGE_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_change_link_key(int dd, uint16_t handle, int to)
+{
+ change_conn_link_key_cp cp;
+ evt_change_conn_link_key_complete rp;
+ struct hci_request rq;
+
+ cp.handle = handle;
+
+ rq.ogf = OGF_LINK_CTL;
+ rq.ocf = OCF_CHANGE_CONN_LINK_KEY;
+ rq.event = EVT_CHANGE_CONN_LINK_KEY_COMPLETE;
+ rq.cparam = &cp;
+ rq.clen = CHANGE_CONN_LINK_KEY_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = EVT_CHANGE_CONN_LINK_KEY_COMPLETE_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_switch_role(int dd, bdaddr_t *bdaddr, uint8_t role, int to)
+{
+ switch_role_cp cp;
+ evt_role_change rp;
+ struct hci_request rq;
+
+ bacpy(&cp.bdaddr, bdaddr);
+ cp.role = role;
+ rq.ogf = OGF_LINK_POLICY;
+ rq.ocf = OCF_SWITCH_ROLE;
+ rq.cparam = &cp;
+ rq.clen = SWITCH_ROLE_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = EVT_ROLE_CHANGE_SIZE;
+ rq.event = EVT_ROLE_CHANGE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_park_mode(int dd, uint16_t handle, uint16_t max_interval, uint16_t min_interval, int to)
+{
+ park_mode_cp cp;
+ evt_mode_change rp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof (cp));
+ cp.handle = handle;
+ cp.max_interval = max_interval;
+ cp.min_interval = min_interval;
+
+ memset(&rq, 0, sizeof (rq));
+ rq.ogf = OGF_LINK_POLICY;
+ rq.ocf = OCF_PARK_MODE;
+ rq.event = EVT_MODE_CHANGE;
+ rq.cparam = &cp;
+ rq.clen = PARK_MODE_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = EVT_MODE_CHANGE_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_exit_park_mode(int dd, uint16_t handle, int to)
+{
+ exit_park_mode_cp cp;
+ evt_mode_change rp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof (cp));
+ cp.handle = handle;
+
+ memset (&rq, 0, sizeof (rq));
+ rq.ogf = OGF_LINK_POLICY;
+ rq.ocf = OCF_EXIT_PARK_MODE;
+ rq.event = EVT_MODE_CHANGE;
+ rq.cparam = &cp;
+ rq.clen = EXIT_PARK_MODE_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = EVT_MODE_CHANGE_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_read_inquiry_scan_type(int dd, uint8_t *type, int to)
+{
+ read_inquiry_scan_type_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_INQUIRY_SCAN_TYPE;
+ rq.rparam = &rp;
+ rq.rlen = READ_INQUIRY_SCAN_TYPE_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *type = rp.type;
+ return 0;
+}
+
+int hci_write_inquiry_scan_type(int dd, uint8_t type, int to)
+{
+ write_inquiry_scan_type_cp cp;
+ write_inquiry_scan_type_rp rp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.type = type;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_WRITE_INQUIRY_SCAN_TYPE;
+ rq.cparam = &cp;
+ rq.clen = WRITE_INQUIRY_SCAN_TYPE_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = WRITE_INQUIRY_SCAN_TYPE_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_read_inquiry_mode(int dd, uint8_t *mode, int to)
+{
+ read_inquiry_mode_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_INQUIRY_MODE;
+ rq.rparam = &rp;
+ rq.rlen = READ_INQUIRY_MODE_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *mode = rp.mode;
+ return 0;
+}
+
+int hci_write_inquiry_mode(int dd, uint8_t mode, int to)
+{
+ write_inquiry_mode_cp cp;
+ write_inquiry_mode_rp rp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.mode = mode;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_WRITE_INQUIRY_MODE;
+ rq.cparam = &cp;
+ rq.clen = WRITE_INQUIRY_MODE_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = WRITE_INQUIRY_MODE_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_read_afh_mode(int dd, uint8_t *mode, int to)
+{
+ read_afh_mode_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_AFH_MODE;
+ rq.rparam = &rp;
+ rq.rlen = READ_AFH_MODE_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *mode = rp.mode;
+ return 0;
+}
+
+int hci_write_afh_mode(int dd, uint8_t mode, int to)
+{
+ write_afh_mode_cp cp;
+ write_afh_mode_rp rp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.mode = mode;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_WRITE_AFH_MODE;
+ rq.cparam = &cp;
+ rq.clen = WRITE_AFH_MODE_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = WRITE_AFH_MODE_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_read_ext_inquiry_response(int dd, uint8_t *fec, uint8_t *data, int to)
+{
+ read_ext_inquiry_response_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_EXT_INQUIRY_RESPONSE;
+ rq.rparam = &rp;
+ rq.rlen = READ_EXT_INQUIRY_RESPONSE_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *fec = rp.fec;
+ memcpy(data, rp.data, 240);
+
+ return 0;
+}
+
+int hci_write_ext_inquiry_response(int dd, uint8_t fec, uint8_t *data, int to)
+{
+ write_ext_inquiry_response_cp cp;
+ write_ext_inquiry_response_rp rp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.fec = fec;
+ memcpy(cp.data, data, 240);
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_WRITE_EXT_INQUIRY_RESPONSE;
+ rq.cparam = &cp;
+ rq.clen = WRITE_EXT_INQUIRY_RESPONSE_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = WRITE_EXT_INQUIRY_RESPONSE_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_read_simple_pairing_mode(int dd, uint8_t *mode, int to)
+{
+ read_simple_pairing_mode_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_SIMPLE_PAIRING_MODE;
+ rq.rparam = &rp;
+ rq.rlen = READ_SIMPLE_PAIRING_MODE_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *mode = rp.mode;
+ return 0;
+}
+
+int hci_write_simple_pairing_mode(int dd, uint8_t mode, int to)
+{
+ write_simple_pairing_mode_cp cp;
+ write_simple_pairing_mode_rp rp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.mode = mode;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_WRITE_SIMPLE_PAIRING_MODE;
+ rq.cparam = &cp;
+ rq.clen = WRITE_SIMPLE_PAIRING_MODE_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = WRITE_SIMPLE_PAIRING_MODE_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_read_local_oob_data(int dd, uint8_t *hash, uint8_t *randomizer, int to)
+{
+ read_local_oob_data_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_LOCAL_OOB_DATA;
+ rq.rparam = &rp;
+ rq.rlen = READ_LOCAL_OOB_DATA_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ memcpy(hash, rp.hash, 16);
+ memcpy(randomizer, rp.randomizer, 16);
+ return 0;
+}
+
+int hci_read_inquiry_transmit_power_level(int dd, int8_t *level, int to)
+{
+ read_inquiry_transmit_power_level_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_INQUIRY_TRANSMIT_POWER_LEVEL;
+ rq.rparam = &rp;
+ rq.rlen = READ_INQUIRY_TRANSMIT_POWER_LEVEL_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *level = rp.level;
+ return 0;
+}
+
+int hci_write_inquiry_transmit_power_level(int dd, int8_t level, int to)
+{
+ write_inquiry_transmit_power_level_cp cp;
+ write_inquiry_transmit_power_level_rp rp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.level = level;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_WRITE_INQUIRY_TRANSMIT_POWER_LEVEL;
+ rq.cparam = &cp;
+ rq.clen = WRITE_INQUIRY_TRANSMIT_POWER_LEVEL_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = WRITE_INQUIRY_TRANSMIT_POWER_LEVEL_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_read_transmit_power_level(int dd, uint16_t handle, uint8_t type, int8_t *level, int to)
+{
+ read_transmit_power_level_cp cp;
+ read_transmit_power_level_rp rp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.handle = handle;
+ cp.type = type;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_TRANSMIT_POWER_LEVEL;
+ rq.cparam = &cp;
+ rq.clen = READ_TRANSMIT_POWER_LEVEL_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = READ_TRANSMIT_POWER_LEVEL_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *level = rp.level;
+ return 0;
+}
+
+int hci_read_link_policy(int dd, uint16_t handle, uint16_t *policy, int to)
+{
+ read_link_policy_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LINK_POLICY;
+ rq.ocf = OCF_READ_LINK_POLICY;
+ rq.cparam = &handle;
+ rq.clen = 2;
+ rq.rparam = &rp;
+ rq.rlen = READ_LINK_POLICY_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *policy = rp.policy;
+ return 0;
+}
+
+int hci_write_link_policy(int dd, uint16_t handle, uint16_t policy, int to)
+{
+ write_link_policy_cp cp;
+ write_link_policy_rp rp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.handle = handle;
+ cp.policy = policy;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LINK_POLICY;
+ rq.ocf = OCF_WRITE_LINK_POLICY;
+ rq.cparam = &cp;
+ rq.clen = WRITE_LINK_POLICY_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = WRITE_LINK_POLICY_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_read_link_supervision_timeout(int dd, uint16_t handle, uint16_t *timeout, int to)
+{
+ read_link_supervision_timeout_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_LINK_SUPERVISION_TIMEOUT;
+ rq.cparam = &handle;
+ rq.clen = 2;
+ rq.rparam = &rp;
+ rq.rlen = READ_LINK_SUPERVISION_TIMEOUT_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *timeout = rp.timeout;
+ return 0;
+}
+
+int hci_write_link_supervision_timeout(int dd, uint16_t handle, uint16_t timeout, int to)
+{
+ write_link_supervision_timeout_cp cp;
+ write_link_supervision_timeout_rp rp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.handle = handle;
+ cp.timeout = timeout;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_WRITE_LINK_SUPERVISION_TIMEOUT;
+ rq.cparam = &cp;
+ rq.clen = WRITE_LINK_SUPERVISION_TIMEOUT_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = WRITE_LINK_SUPERVISION_TIMEOUT_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_set_afh_classification(int dd, uint8_t *map, int to)
+{
+ set_afh_classification_cp cp;
+ set_afh_classification_rp rp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ memcpy(cp.map, map, 10);
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_SET_AFH_CLASSIFICATION;
+ rq.cparam = &cp;
+ rq.clen = SET_AFH_CLASSIFICATION_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = SET_AFH_CLASSIFICATION_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_read_link_quality(int dd, uint16_t handle, uint8_t *link_quality, int to)
+{
+ read_link_quality_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_STATUS_PARAM;
+ rq.ocf = OCF_READ_LINK_QUALITY;
+ rq.cparam = &handle;
+ rq.clen = 2;
+ rq.rparam = &rp;
+ rq.rlen = READ_LINK_QUALITY_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *link_quality = rp.link_quality;
+ return 0;
+}
+
+int hci_read_rssi(int dd, uint16_t handle, int8_t *rssi, int to)
+{
+ read_rssi_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_STATUS_PARAM;
+ rq.ocf = OCF_READ_RSSI;
+ rq.cparam = &handle;
+ rq.clen = 2;
+ rq.rparam = &rp;
+ rq.rlen = READ_RSSI_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *rssi = rp.rssi;
+ return 0;
+}
+
+int hci_read_afh_map(int dd, uint16_t handle, uint8_t *mode, uint8_t *map, int to)
+{
+ read_afh_map_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_STATUS_PARAM;
+ rq.ocf = OCF_READ_AFH_MAP;
+ rq.cparam = &handle;
+ rq.clen = 2;
+ rq.rparam = &rp;
+ rq.rlen = READ_AFH_MAP_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *mode = rp.mode;
+ memcpy(map, rp.map, 10);
+ return 0;
+}
+
+int hci_read_clock(int dd, uint16_t handle, uint8_t which, uint32_t *clock, uint16_t *accuracy, int to)
+{
+ read_clock_cp cp;
+ read_clock_rp rp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.handle = handle;
+ cp.which_clock = which;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_STATUS_PARAM;
+ rq.ocf = OCF_READ_CLOCK;
+ rq.cparam = &cp;
+ rq.clen = READ_CLOCK_CP_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = READ_CLOCK_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ *clock = rp.clock;
+ *accuracy = rp.accuracy;
+ return 0;
+}
+
+int hci_local_name(int dd, int len, char *name, int to)
+{
+ return hci_read_local_name(dd, len, name, to);
+}
+
+int hci_remote_name(int dd, const bdaddr_t *bdaddr, int len, char *name, int to)
+{
+ return hci_read_remote_name(dd, bdaddr, len, name, to);
+}
diff --git a/lib/sdp.c b/lib/sdp.c
new file mode 100644
index 00000000..3149c0f1
--- /dev/null
+++ b/lib/sdp.c
@@ -0,0 +1,4421 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2001-2002 Nokia Corporation
+ * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
+ * Copyright (C) 2002-2008 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2002-2003 Stephen Crane <steve.crane@rococosoft.com>
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <string.h>
+#include <syslog.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/hci.h>
+#include <bluetooth/hci_lib.h>
+#include <bluetooth/l2cap.h>
+#include <bluetooth/sdp.h>
+#include <bluetooth/sdp_lib.h>
+
+#include <netinet/in.h>
+
+#define SDPINF(fmt, arg...) syslog(LOG_INFO, fmt "\n", ## arg)
+#define SDPERR(fmt, arg...) syslog(LOG_ERR, "%s: " fmt "\n", __func__ , ## arg)
+
+#ifdef SDP_DEBUG
+#define SDPDBG(fmt, arg...) syslog(LOG_DEBUG, "%s: " fmt "\n", __func__ , ## arg)
+#else
+#define SDPDBG(fmt...)
+#endif
+
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define ntoh64(x) (x)
+static inline void ntoh128(uint128_t *src, uint128_t *dst)
+{
+ int i;
+ for (i = 0; i < 16; i++)
+ dst->data[i] = src->data[i];
+}
+#else
+static inline uint64_t ntoh64(uint64_t n)
+{
+ uint64_t h;
+ uint64_t tmp = ntohl(n & 0x00000000ffffffff);
+ h = ntohl(n >> 32);
+ h |= tmp << 32;
+ return h;
+}
+static inline void ntoh128(uint128_t *src, uint128_t *dst)
+{
+ int i;
+ for (i = 0; i < 16; i++)
+ dst->data[15 - i] = src->data[i];
+}
+#endif
+
+#define hton64(x) ntoh64(x)
+#define hton128(x, y) ntoh128(x, y)
+
+#define BASE_UUID "00000000-0000-1000-8000-00805F9B34FB"
+
+static uint128_t bluetooth_base_uuid = {
+ .data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
+ 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }
+};
+
+#define SDP_MAX_ATTR_LEN 65535
+
+/* Message structure. */
+struct tupla {
+ int index;
+ char *str;
+};
+
+static struct tupla Protocol[] = {
+ { SDP_UUID, "SDP" },
+ { UDP_UUID, "UDP" },
+ { RFCOMM_UUID, "RFCOMM" },
+ { TCP_UUID, "TCP" },
+ { TCS_BIN_UUID, "TCS-BIN" },
+ { TCS_AT_UUID, "TCS-AT" },
+ { OBEX_UUID, "OBEX" },
+ { IP_UUID, "IP" },
+ { FTP_UUID, "FTP" },
+ { HTTP_UUID, "HTTP" },
+ { WSP_UUID, "WSP" },
+ { BNEP_UUID, "BNEP" },
+ { UPNP_UUID, "UPNP" },
+ { HIDP_UUID, "HIDP" },
+ { HCRP_CTRL_UUID, "HCRP-Ctrl" },
+ { HCRP_DATA_UUID, "HCRP-Data" },
+ { HCRP_NOTE_UUID, "HCRP-Notify" },
+ { AVCTP_UUID, "AVCTP" },
+ { AVDTP_UUID, "AVDTP" },
+ { CMTP_UUID, "CMTP" },
+ { UDI_UUID, "UDI" },
+ { MCAP_CTRL_UUID, "MCAP-Ctrl" },
+ { MCAP_DATA_UUID, "MCAP-Data" },
+ { L2CAP_UUID, "L2CAP" },
+ { 0 }
+};
+
+static struct tupla ServiceClass[] = {
+ { SDP_SERVER_SVCLASS_ID, "SDP Server" },
+ { BROWSE_GRP_DESC_SVCLASS_ID, "Browse Group Descriptor" },
+ { PUBLIC_BROWSE_GROUP, "Public Browse Group" },
+ { SERIAL_PORT_SVCLASS_ID, "Serial Port" },
+ { LAN_ACCESS_SVCLASS_ID, "LAN Access Using PPP" },
+ { DIALUP_NET_SVCLASS_ID, "Dialup Networking" },
+ { IRMC_SYNC_SVCLASS_ID, "IrMC Sync" },
+ { OBEX_OBJPUSH_SVCLASS_ID, "OBEX Object Push" },
+ { OBEX_FILETRANS_SVCLASS_ID, "OBEX File Transfer" },
+ { IRMC_SYNC_CMD_SVCLASS_ID, "IrMC Sync Command" },
+ { HEADSET_SVCLASS_ID, "Headset" },
+ { CORDLESS_TELEPHONY_SVCLASS_ID, "Cordless Telephony" },
+ { AUDIO_SOURCE_SVCLASS_ID, "Audio Source" },
+ { AUDIO_SINK_SVCLASS_ID, "Audio Sink" },
+ { AV_REMOTE_TARGET_SVCLASS_ID, "AV Remote Target" },
+ { ADVANCED_AUDIO_SVCLASS_ID, "Advanced Audio" },
+ { AV_REMOTE_SVCLASS_ID, "AV Remote" },
+ { VIDEO_CONF_SVCLASS_ID, "Video Conferencing" },
+ { INTERCOM_SVCLASS_ID, "Intercom" },
+ { FAX_SVCLASS_ID, "Fax" },
+ { HEADSET_AGW_SVCLASS_ID, "Headset Audio Gateway" },
+ { WAP_SVCLASS_ID, "WAP" },
+ { WAP_CLIENT_SVCLASS_ID, "WAP Client" },
+ { PANU_SVCLASS_ID, "PAN User" },
+ { NAP_SVCLASS_ID, "Network Access Point" },
+ { GN_SVCLASS_ID, "PAN Group Network" },
+ { DIRECT_PRINTING_SVCLASS_ID, "Direct Printing" },
+ { REFERENCE_PRINTING_SVCLASS_ID, "Reference Printing" },
+ { IMAGING_SVCLASS_ID, "Imaging" },
+ { IMAGING_RESPONDER_SVCLASS_ID, "Imaging Responder" },
+ { IMAGING_ARCHIVE_SVCLASS_ID, "Imaging Automatic Archive" },
+ { IMAGING_REFOBJS_SVCLASS_ID, "Imaging Referenced Objects" },
+ { HANDSFREE_SVCLASS_ID, "Handsfree" },
+ { HANDSFREE_AGW_SVCLASS_ID, "Handsfree Audio Gateway" },
+ { DIRECT_PRT_REFOBJS_SVCLASS_ID, "Direct Printing Ref. Objects" },
+ { REFLECTED_UI_SVCLASS_ID, "Reflected UI" },
+ { BASIC_PRINTING_SVCLASS_ID, "Basic Printing" },
+ { PRINTING_STATUS_SVCLASS_ID, "Printing Status" },
+ { HID_SVCLASS_ID, "Human Interface Device" },
+ { HCR_SVCLASS_ID, "Hardcopy Cable Replacement" },
+ { HCR_PRINT_SVCLASS_ID, "HCR Print" },
+ { HCR_SCAN_SVCLASS_ID, "HCR Scan" },
+ { CIP_SVCLASS_ID, "Common ISDN Access" },
+ { VIDEO_CONF_GW_SVCLASS_ID, "Video Conferencing Gateway" },
+ { UDI_MT_SVCLASS_ID, "UDI MT" },
+ { UDI_TA_SVCLASS_ID, "UDI TA" },
+ { AV_SVCLASS_ID, "Audio/Video" },
+ { SAP_SVCLASS_ID, "SIM Access" },
+ { PBAP_PCE_SVCLASS_ID, "Phonebook Access - PCE" },
+ { PBAP_PSE_SVCLASS_ID, "Phonebook Access - PSE" },
+ { PBAP_SVCLASS_ID, "Phonebook Access" },
+ { PNP_INFO_SVCLASS_ID, "PnP Information" },
+ { GENERIC_NETWORKING_SVCLASS_ID, "Generic Networking" },
+ { GENERIC_FILETRANS_SVCLASS_ID, "Generic File Transfer" },
+ { GENERIC_AUDIO_SVCLASS_ID, "Generic Audio" },
+ { GENERIC_TELEPHONY_SVCLASS_ID, "Generic Telephony" },
+ { UPNP_SVCLASS_ID, "UPnP" },
+ { UPNP_IP_SVCLASS_ID, "UPnP IP" },
+ { UPNP_PAN_SVCLASS_ID, "UPnP PAN" },
+ { UPNP_LAP_SVCLASS_ID, "UPnP LAP" },
+ { UPNP_L2CAP_SVCLASS_ID, "UPnP L2CAP" },
+ { VIDEO_SOURCE_SVCLASS_ID, "Video Source" },
+ { VIDEO_SINK_SVCLASS_ID, "Video Sink" },
+ { VIDEO_DISTRIBUTION_SVCLASS_ID, "Video Distribution" },
+ { MDP_SVCLASS_ID, "MDP" },
+ { MDP_SOURCE_SVCLASS_ID, "MDP Source" },
+ { MDP_SINK_SVCLASS_ID, "MDP Sink" },
+ { APPLE_AGENT_SVCLASS_ID, "Apple Agent" },
+ { 0 }
+};
+
+#define Profile ServiceClass
+
+static char *string_lookup(struct tupla *pt0, int index)
+{
+ struct tupla *pt;
+
+ for (pt = pt0; pt->index; pt++)
+ if (pt->index == index)
+ return pt->str;
+
+ return "";
+}
+
+static char *string_lookup_uuid(struct tupla *pt0, const uuid_t* uuid)
+{
+ uuid_t tmp_uuid;
+
+ memcpy(&tmp_uuid, uuid, sizeof(tmp_uuid));
+
+ if (sdp_uuid128_to_uuid(&tmp_uuid)) {
+ switch (tmp_uuid.type) {
+ case SDP_UUID16:
+ return string_lookup(pt0, tmp_uuid.value.uuid16);
+ case SDP_UUID32:
+ return string_lookup(pt0, tmp_uuid.value.uuid32);
+ }
+ }
+
+ return "";
+}
+
+/*
+ * Prints into a string the Protocol UUID
+ * coping a maximum of n characters.
+ */
+static int uuid2str(struct tupla *message, const uuid_t *uuid, char *str, size_t n)
+{
+ char *str2;
+
+ if (!uuid) {
+ snprintf(str, n, "NULL");
+ return -2;
+ }
+
+ switch (uuid->type) {
+ case SDP_UUID16:
+ str2 = string_lookup(message, uuid->value.uuid16);
+ snprintf(str, n, str2);
+ break;
+ case SDP_UUID32:
+ str2 = string_lookup(message, uuid->value.uuid32);
+ snprintf(str, n, str2);
+ break;
+ case SDP_UUID128:
+ str2 = string_lookup_uuid(message, uuid);
+ snprintf(str, n, str2);
+ break;
+ default:
+ snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type);
+ return -1;
+ }
+
+ return 0;
+}
+
+int sdp_proto_uuid2strn(const uuid_t *uuid, char *str, size_t n)
+{
+ return uuid2str(Protocol, uuid, str, n);
+}
+
+int sdp_svclass_uuid2strn(const uuid_t *uuid, char *str, size_t n)
+{
+ return uuid2str(ServiceClass, uuid, str, n);
+}
+
+int sdp_profile_uuid2strn(const uuid_t *uuid, char *str, size_t n)
+{
+ return uuid2str(Profile, uuid, str, n);
+}
+
+/*
+ * convert the UUID to string, copying a maximum of n characters.
+ */
+int sdp_uuid2strn(const uuid_t *uuid, char *str, size_t n)
+{
+ if (!uuid) {
+ snprintf(str, n, "NULL");
+ return -2;
+ }
+ switch (uuid->type) {
+ case SDP_UUID16:
+ snprintf(str, n, "%.4x", uuid->value.uuid16);
+ break;
+ case SDP_UUID32:
+ snprintf(str, n, "%.8x", uuid->value.uuid32);
+ break;
+ case SDP_UUID128:{
+ unsigned int data0;
+ unsigned short data1;
+ unsigned short data2;
+ unsigned short data3;
+ unsigned int data4;
+ unsigned short data5;
+
+ memcpy(&data0, &uuid->value.uuid128.data[0], 4);
+ memcpy(&data1, &uuid->value.uuid128.data[4], 2);
+ memcpy(&data2, &uuid->value.uuid128.data[6], 2);
+ memcpy(&data3, &uuid->value.uuid128.data[8], 2);
+ memcpy(&data4, &uuid->value.uuid128.data[10], 4);
+ memcpy(&data5, &uuid->value.uuid128.data[14], 2);
+
+ snprintf(str, n, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
+ ntohl(data0), ntohs(data1),
+ ntohs(data2), ntohs(data3),
+ ntohl(data4), ntohs(data5));
+ }
+ break;
+ default:
+ snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type);
+ return -1; // Enum type of UUID not set
+ }
+ return 0;
+}
+
+#ifdef SDP_DEBUG
+/*
+ * Function prints the UUID in hex as per defined syntax -
+ *
+ * 4bytes-2bytes-2bytes-2bytes-6bytes
+ *
+ * There is some ugly code, including hardcoding, but
+ * that is just the way it is converting 16 and 32 bit
+ * UUIDs to 128 bit as defined in the SDP doc
+ */
+void sdp_uuid_print(const uuid_t *uuid)
+{
+ if (uuid == NULL) {
+ SDPERR("Null passed to print UUID\n");
+ return;
+ }
+ if (uuid->type == SDP_UUID16) {
+ SDPDBG(" uint16_t : 0x%.4x\n", uuid->value.uuid16);
+ } else if (uuid->type == SDP_UUID32) {
+ SDPDBG(" uint32_t : 0x%.8x\n", uuid->value.uuid32);
+ } else if (uuid->type == SDP_UUID128) {
+ unsigned int data0;
+ unsigned short data1;
+ unsigned short data2;
+ unsigned short data3;
+ unsigned int data4;
+ unsigned short data5;
+
+ memcpy(&data0, &uuid->value.uuid128.data[0], 4);
+ memcpy(&data1, &uuid->value.uuid128.data[4], 2);
+ memcpy(&data2, &uuid->value.uuid128.data[6], 2);
+ memcpy(&data3, &uuid->value.uuid128.data[8], 2);
+ memcpy(&data4, &uuid->value.uuid128.data[10], 4);
+ memcpy(&data5, &uuid->value.uuid128.data[14], 2);
+
+ SDPDBG(" uint128_t : 0x%.8x-", ntohl(data0));
+ SDPDBG("%.4x-", ntohs(data1));
+ SDPDBG("%.4x-", ntohs(data2));
+ SDPDBG("%.4x-", ntohs(data3));
+ SDPDBG("%.8x", ntohl(data4));
+ SDPDBG("%.4x\n", ntohs(data5));
+ } else
+ SDPERR("Enum type of UUID not set\n");
+}
+#endif
+
+sdp_data_t *sdp_data_alloc_with_length(uint8_t dtd, const void *value, uint32_t length)
+{
+ sdp_data_t *seq;
+ sdp_data_t *d = malloc(sizeof(sdp_data_t));
+
+ if (!d)
+ return NULL;
+
+ memset(d, 0, sizeof(sdp_data_t));
+ d->dtd = dtd;
+ d->unitSize = sizeof(uint8_t);
+
+ switch (dtd) {
+ case SDP_DATA_NIL:
+ break;
+ case SDP_UINT8:
+ d->val.uint8 = *(uint8_t *) value;
+ d->unitSize += sizeof(uint8_t);
+ break;
+ case SDP_INT8:
+ case SDP_BOOL:
+ d->val.int8 = *(int8_t *) value;
+ d->unitSize += sizeof(int8_t);
+ break;
+ case SDP_UINT16:
+ d->val.uint16 = bt_get_unaligned((uint16_t *) value);
+ d->unitSize += sizeof(uint16_t);
+ break;
+ case SDP_INT16:
+ d->val.int16 = bt_get_unaligned((int16_t *) value);
+ d->unitSize += sizeof(int16_t);
+ break;
+ case SDP_UINT32:
+ d->val.uint32 = bt_get_unaligned((uint32_t *) value);
+ d->unitSize += sizeof(uint32_t);
+ break;
+ case SDP_INT32:
+ d->val.int32 = bt_get_unaligned((int32_t *) value);
+ d->unitSize += sizeof(int32_t);
+ break;
+ case SDP_INT64:
+ d->val.int64 = bt_get_unaligned((int64_t *) value);
+ d->unitSize += sizeof(int64_t);
+ break;
+ case SDP_UINT64:
+ d->val.uint64 = bt_get_unaligned((uint64_t *) value);
+ d->unitSize += sizeof(uint64_t);
+ break;
+ case SDP_UINT128:
+ memcpy(&d->val.uint128.data, value, sizeof(uint128_t));
+ d->unitSize += sizeof(uint128_t);
+ break;
+ case SDP_INT128:
+ memcpy(&d->val.int128.data, value, sizeof(uint128_t));
+ d->unitSize += sizeof(uint128_t);
+ break;
+ case SDP_UUID16:
+ sdp_uuid16_create(&d->val.uuid, bt_get_unaligned((uint16_t *) value));
+ d->unitSize += sizeof(uint16_t);
+ break;
+ case SDP_UUID32:
+ sdp_uuid32_create(&d->val.uuid, bt_get_unaligned((uint32_t *) value));
+ d->unitSize += sizeof(uint32_t);
+ break;
+ case SDP_UUID128:
+ sdp_uuid128_create(&d->val.uuid, value);
+ d->unitSize += sizeof(uint128_t);
+ break;
+ case SDP_URL_STR8:
+ case SDP_URL_STR16:
+ case SDP_TEXT_STR8:
+ case SDP_TEXT_STR16:
+ if (!value) {
+ free(d);
+ return NULL;
+ }
+
+ d->unitSize += length;
+ if (length <= USHRT_MAX) {
+ d->val.str = malloc(length);
+ if (!d->val.str) {
+ free(d);
+ return NULL;
+ }
+
+ memcpy(d->val.str, value, length);
+ } else {
+ SDPERR("Strings of size > USHRT_MAX not supported\n");
+ free(d);
+ d = NULL;
+ }
+ break;
+ case SDP_URL_STR32:
+ case SDP_TEXT_STR32:
+ SDPERR("Strings of size > USHRT_MAX not supported\n");
+ break;
+ case SDP_ALT8:
+ case SDP_ALT16:
+ case SDP_ALT32:
+ case SDP_SEQ8:
+ case SDP_SEQ16:
+ case SDP_SEQ32:
+ if (dtd == SDP_ALT8 || dtd == SDP_SEQ8)
+ d->unitSize += sizeof(uint8_t);
+ else if (dtd == SDP_ALT16 || dtd == SDP_SEQ16)
+ d->unitSize += sizeof(uint16_t);
+ else if (dtd == SDP_ALT32 || dtd == SDP_SEQ32)
+ d->unitSize += sizeof(uint32_t);
+ seq = (sdp_data_t *)value;
+ d->val.dataseq = seq;
+ for (; seq; seq = seq->next)
+ d->unitSize += seq->unitSize;
+ break;
+ default:
+ free(d);
+ d = NULL;
+ }
+
+ return d;
+}
+
+sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value)
+{
+ uint32_t length;
+
+ switch (dtd) {
+ case SDP_URL_STR8:
+ case SDP_URL_STR16:
+ case SDP_TEXT_STR8:
+ case SDP_TEXT_STR16:
+ if (!value)
+ return NULL;
+
+ length = strlen((char *) value);
+ break;
+ default:
+ length = 0;
+ break;
+ }
+
+ return sdp_data_alloc_with_length(dtd, value, length);
+}
+
+sdp_data_t *sdp_seq_append(sdp_data_t *seq, sdp_data_t *d)
+{
+ if (seq) {
+ sdp_data_t *p;
+ for (p = seq; p->next; p = p->next);
+ p->next = d;
+ } else
+ seq = d;
+ d->next = NULL;
+ return seq;
+}
+
+sdp_data_t *sdp_seq_alloc_with_length(void **dtds, void **values, int *length, int len)
+{
+ sdp_data_t *curr = NULL, *seq = NULL;
+ int i;
+
+ for (i = 0; i < len; i++) {
+ sdp_data_t *data;
+ int8_t dtd = *(uint8_t *) dtds[i];
+
+ if (dtd >= SDP_SEQ8 && dtd <= SDP_ALT32)
+ data = (sdp_data_t *) values[i];
+ else
+ data = sdp_data_alloc_with_length(dtd, values[i], length[i]);
+
+ if (!data)
+ return NULL;
+
+ if (curr)
+ curr->next = data;
+ else
+ seq = data;
+
+ curr = data;
+ }
+
+ return sdp_data_alloc_with_length(SDP_SEQ8, seq, length[i]);
+}
+
+sdp_data_t *sdp_seq_alloc(void **dtds, void **values, int len)
+{
+ sdp_data_t *curr = NULL, *seq = NULL;
+ int i;
+
+ for (i = 0; i < len; i++) {
+ sdp_data_t *data;
+ uint8_t dtd = *(uint8_t *) dtds[i];
+
+ if (dtd >= SDP_SEQ8 && dtd <= SDP_ALT32)
+ data = (sdp_data_t *) values[i];
+ else
+ data = sdp_data_alloc(dtd, values[i]);
+
+ if (!data)
+ return NULL;
+
+ if (curr)
+ curr->next = data;
+ else
+ seq = data;
+
+ curr = data;
+ }
+
+ return sdp_data_alloc(SDP_SEQ8, seq);
+}
+
+static void extract_svclass_uuid(sdp_data_t *data, uuid_t *uuid)
+{
+ sdp_data_t *d;
+
+ if (!data || data->dtd < SDP_SEQ8 || data->dtd > SDP_SEQ32)
+ return;
+
+ d = data->val.dataseq;
+ if (!d)
+ return;
+
+ if (d->dtd < SDP_UUID16 || d->dtd > SDP_UUID128)
+ return;
+
+ *uuid = d->val.uuid;
+}
+
+int sdp_attr_add(sdp_record_t *rec, uint16_t attr, sdp_data_t *d)
+{
+ sdp_data_t *p = sdp_data_get(rec, attr);
+
+ if (p)
+ return -1;
+
+ d->attrId = attr;
+ rec->attrlist = sdp_list_insert_sorted(rec->attrlist, d, sdp_attrid_comp_func);
+
+ if (attr == SDP_ATTR_SVCLASS_ID_LIST)
+ extract_svclass_uuid(d, &rec->svclass);
+
+ return 0;
+}
+
+void sdp_attr_remove(sdp_record_t *rec, uint16_t attr)
+{
+ sdp_data_t *d = sdp_data_get(rec, attr);
+
+ if (d)
+ rec->attrlist = sdp_list_remove(rec->attrlist, d);
+
+ if (attr == SDP_ATTR_SVCLASS_ID_LIST)
+ memset(&rec->svclass, 0, sizeof(rec->svclass));
+}
+
+void sdp_set_seq_len(uint8_t *ptr, uint32_t length)
+{
+ uint8_t dtd = *(uint8_t *) ptr++;
+
+ switch (dtd) {
+ case SDP_SEQ8:
+ case SDP_ALT8:
+ case SDP_TEXT_STR8:
+ case SDP_URL_STR8:
+ *(uint8_t *)ptr = (uint8_t) length;
+ break;
+ case SDP_SEQ16:
+ case SDP_ALT16:
+ case SDP_TEXT_STR16:
+ case SDP_URL_STR16:
+ bt_put_unaligned(htons(length), (uint16_t *) ptr);
+ break;
+ case SDP_SEQ32:
+ case SDP_ALT32:
+ case SDP_TEXT_STR32:
+ case SDP_URL_STR32:
+ bt_put_unaligned(htonl(length), (uint32_t *) ptr);
+ break;
+ }
+}
+
+int sdp_set_data_type(sdp_buf_t *buf, uint8_t dtd)
+{
+ int orig = buf->data_size;
+ uint8_t *p = buf->data + buf->data_size;
+
+ *p++ = dtd;
+ buf->data_size += sizeof(uint8_t);
+
+ switch (dtd) {
+ case SDP_SEQ8:
+ case SDP_TEXT_STR8:
+ case SDP_URL_STR8:
+ case SDP_ALT8:
+ buf->data_size += sizeof(uint8_t);
+ break;
+ case SDP_SEQ16:
+ case SDP_TEXT_STR16:
+ case SDP_URL_STR16:
+ case SDP_ALT16:
+ buf->data_size += sizeof(uint16_t);
+ break;
+ case SDP_SEQ32:
+ case SDP_TEXT_STR32:
+ case SDP_URL_STR32:
+ case SDP_ALT32:
+ buf->data_size += sizeof(uint32_t);
+ break;
+ }
+
+ return buf->data_size - orig;
+}
+
+void sdp_set_attrid(sdp_buf_t *buf, uint16_t attr)
+{
+ uint8_t *p = buf->data;
+
+ // data type for attr
+ *p++ = SDP_UINT16;
+ buf->data_size = sizeof(uint8_t);
+ bt_put_unaligned(htons(attr), (uint16_t *) p);
+ p += sizeof(uint16_t);
+ buf->data_size += sizeof(uint16_t);
+}
+
+static int get_data_size(sdp_buf_t *buf, sdp_data_t *sdpdata)
+{
+ sdp_data_t *d;
+ int n = 0;
+
+ for (d = sdpdata->val.dataseq; d; d = d->next)
+ n += sdp_gen_pdu(buf, d);
+
+ return n;
+}
+
+int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d)
+{
+ uint32_t pdu_size = 0, data_size = 0;
+ unsigned char *src = NULL, is_seq = 0, is_alt = 0;
+ uint8_t dtd = d->dtd;
+ uint16_t u16;
+ uint32_t u32;
+ uint64_t u64;
+ uint128_t u128;
+ uint8_t *seqp = buf->data + buf->data_size;
+
+ pdu_size = sdp_set_data_type(buf, dtd);
+
+ switch (dtd) {
+ case SDP_DATA_NIL:
+ break;
+ case SDP_UINT8:
+ src = &d->val.uint8;
+ data_size = sizeof(uint8_t);
+ break;
+ case SDP_UINT16:
+ u16 = htons(d->val.uint16);
+ src = (unsigned char *) &u16;
+ data_size = sizeof(uint16_t);
+ break;
+ case SDP_UINT32:
+ u32 = htonl(d->val.uint32);
+ src = (unsigned char *) &u32;
+ data_size = sizeof(uint32_t);
+ break;
+ case SDP_UINT64:
+ u64 = hton64(d->val.uint64);
+ src = (unsigned char *) &u64;
+ data_size = sizeof(uint64_t);
+ break;
+ case SDP_UINT128:
+ hton128(&d->val.uint128, &u128);
+ src = (unsigned char *) &u128;
+ data_size = sizeof(uint128_t);
+ break;
+ case SDP_INT8:
+ case SDP_BOOL:
+ src = (unsigned char *) &d->val.int8;
+ data_size = sizeof(int8_t);
+ break;
+ case SDP_INT16:
+ u16 = htons(d->val.int16);
+ src = (unsigned char *) &u16;
+ data_size = sizeof(int16_t);
+ break;
+ case SDP_INT32:
+ u32 = htonl(d->val.int32);
+ src = (unsigned char *) &u32;
+ data_size = sizeof(int32_t);
+ break;
+ case SDP_INT64:
+ u64 = hton64(d->val.int64);
+ src = (unsigned char *) &u64;
+ data_size = sizeof(int64_t);
+ break;
+ case SDP_INT128:
+ hton128(&d->val.int128, &u128);
+ src = (unsigned char *) &u128;
+ data_size = sizeof(uint128_t);
+ break;
+ case SDP_TEXT_STR8:
+ case SDP_TEXT_STR16:
+ case SDP_TEXT_STR32:
+ case SDP_URL_STR8:
+ case SDP_URL_STR16:
+ case SDP_URL_STR32:
+ src = (unsigned char *) d->val.str;
+ data_size = d->unitSize - sizeof(uint8_t);
+ sdp_set_seq_len(seqp, data_size);
+ break;
+ case SDP_SEQ8:
+ case SDP_SEQ16:
+ case SDP_SEQ32:
+ is_seq = 1;
+ data_size = get_data_size(buf, d);
+ sdp_set_seq_len(seqp, data_size);
+ break;
+ case SDP_ALT8:
+ case SDP_ALT16:
+ case SDP_ALT32:
+ is_alt = 1;
+ data_size = get_data_size(buf, d);
+ sdp_set_seq_len(seqp, data_size);
+ break;
+ case SDP_UUID16:
+ u16 = htons(d->val.uuid.value.uuid16);
+ src = (unsigned char *) &u16;
+ data_size = sizeof(uint16_t);
+ break;
+ case SDP_UUID32:
+ u32 = htonl(d->val.uuid.value.uuid32);
+ src = (unsigned char *) &u32;
+ data_size = sizeof(uint32_t);
+ break;
+ case SDP_UUID128:
+ src = (unsigned char *) &d->val.uuid.value.uuid128;
+ data_size = sizeof(uint128_t);
+ break;
+ default:
+ break;
+ }
+
+ if (!is_seq && !is_alt) {
+ if (src && buf) {
+ memcpy(buf->data + buf->data_size, src, data_size);
+ buf->data_size += data_size;
+ } else if (dtd != SDP_DATA_NIL)
+ SDPDBG("Gen PDU : Cant copy from NULL source or dest\n");
+ }
+
+ pdu_size += data_size;
+
+ return pdu_size;
+}
+
+static void sdp_attr_pdu(void *value, void *udata)
+{
+ sdp_append_to_pdu((sdp_buf_t *)udata, (sdp_data_t *)value);
+}
+
+int sdp_gen_record_pdu(const sdp_record_t *rec, sdp_buf_t *buf)
+{
+ buf->data = malloc(SDP_PDU_CHUNK_SIZE);
+ if (!buf->data)
+ return -ENOMEM;
+
+ buf->buf_size = SDP_PDU_CHUNK_SIZE;
+ buf->data_size = 0;
+ memset(buf->data, 0, buf->buf_size);
+ sdp_list_foreach(rec->attrlist, sdp_attr_pdu, buf);
+
+ return 0;
+}
+
+void sdp_attr_replace(sdp_record_t *rec, uint16_t attr, sdp_data_t *d)
+{
+ sdp_data_t *p = sdp_data_get(rec, attr);
+
+ if (p) {
+ rec->attrlist = sdp_list_remove(rec->attrlist, p);
+ sdp_data_free(p);
+ }
+
+ d->attrId = attr;
+ rec->attrlist = sdp_list_insert_sorted(rec->attrlist, d, sdp_attrid_comp_func);
+
+ if (attr == SDP_ATTR_SVCLASS_ID_LIST)
+ extract_svclass_uuid(d, &rec->svclass);
+}
+
+int sdp_attrid_comp_func(const void *key1, const void *key2)
+{
+ const sdp_data_t *d1 = (const sdp_data_t *)key1;
+ const sdp_data_t *d2 = (const sdp_data_t *)key2;
+
+ if (d1 && d2)
+ return d1->attrId - d2->attrId;
+ return 0;
+}
+
+static void data_seq_free(sdp_data_t *seq)
+{
+ sdp_data_t *d = seq->val.dataseq;
+
+ while (d) {
+ sdp_data_t *next = d->next;
+ sdp_data_free(d);
+ d = next;
+ }
+}
+
+void sdp_data_free(sdp_data_t *d)
+{
+ switch (d->dtd) {
+ case SDP_SEQ8:
+ case SDP_SEQ16:
+ case SDP_SEQ32:
+ data_seq_free(d);
+ break;
+ case SDP_URL_STR8:
+ case SDP_URL_STR16:
+ case SDP_URL_STR32:
+ case SDP_TEXT_STR8:
+ case SDP_TEXT_STR16:
+ case SDP_TEXT_STR32:
+ free(d->val.str);
+ break;
+ }
+ free(d);
+}
+
+int sdp_uuid_extract_safe(const uint8_t *p, int bufsize, uuid_t *uuid, int *scanned)
+{
+ uint8_t type;
+
+ if (bufsize < sizeof(uint8_t)) {
+ SDPERR("Unexpected end of packet");
+ return -1;
+ }
+
+ type = *(const uint8_t *) p;
+
+ if (!SDP_IS_UUID(type)) {
+ SDPERR("Unknown data type : %d expecting a svc UUID\n", type);
+ return -1;
+ }
+ p += sizeof(uint8_t);
+ *scanned += sizeof(uint8_t);
+ bufsize -= sizeof(uint8_t);
+ if (type == SDP_UUID16) {
+ if (bufsize < sizeof(uint16_t)) {
+ SDPERR("Not enough room for 16-bit UUID");
+ return -1;
+ }
+ sdp_uuid16_create(uuid, ntohs(bt_get_unaligned((uint16_t *) p)));
+ *scanned += sizeof(uint16_t);
+ p += sizeof(uint16_t);
+ } else if (type == SDP_UUID32) {
+ if (bufsize < sizeof(uint32_t)) {
+ SDPERR("Not enough room for 32-bit UUID");
+ return -1;
+ }
+ sdp_uuid32_create(uuid, ntohl(bt_get_unaligned((uint32_t *) p)));
+ *scanned += sizeof(uint32_t);
+ p += sizeof(uint32_t);
+ } else {
+ if (bufsize < sizeof(uint128_t)) {
+ SDPERR("Not enough room for 128-bit UUID");
+ return -1;
+ }
+ sdp_uuid128_create(uuid, p);
+ *scanned += sizeof(uint128_t);
+ p += sizeof(uint128_t);
+ }
+ return 0;
+}
+
+int sdp_uuid_extract(const uint8_t *p, uuid_t *uuid, int *scanned)
+{
+ /* Assume p points to a buffer of size at least SDP_MAX_ATTR_LEN,
+ because we don't have any better information */
+ return sdp_uuid_extract_safe(p, SDP_MAX_ATTR_LEN, uuid, scanned);
+}
+
+static sdp_data_t *extract_int(const void *p, int bufsize, int *len)
+{
+ sdp_data_t *d;
+
+ if (bufsize < sizeof(uint8_t)) {
+ SDPERR("Unexpected end of packet");
+ return NULL;
+ }
+
+ d = malloc(sizeof(sdp_data_t));
+
+ SDPDBG("Extracting integer\n");
+ memset(d, 0, sizeof(sdp_data_t));
+ d->dtd = *(uint8_t *) p;
+ p += sizeof(uint8_t);
+ *len += sizeof(uint8_t);
+ bufsize -= sizeof(uint8_t);
+
+ switch (d->dtd) {
+ case SDP_DATA_NIL:
+ break;
+ case SDP_BOOL:
+ case SDP_INT8:
+ case SDP_UINT8:
+ if (bufsize < sizeof(uint8_t)) {
+ SDPERR("Unexpected end of packet");
+ free(d);
+ return NULL;
+ }
+ *len += sizeof(uint8_t);
+ d->val.uint8 = *(uint8_t *) p;
+ break;
+ case SDP_INT16:
+ case SDP_UINT16:
+ if (bufsize < sizeof(uint16_t)) {
+ SDPERR("Unexpected end of packet");
+ free(d);
+ return NULL;
+ }
+ *len += sizeof(uint16_t);
+ d->val.uint16 = ntohs(bt_get_unaligned((uint16_t *) p));
+ break;
+ case SDP_INT32:
+ case SDP_UINT32:
+ if (bufsize < sizeof(uint32_t)) {
+ SDPERR("Unexpected end of packet");
+ free(d);
+ return NULL;
+ }
+ *len += sizeof(uint32_t);
+ d->val.uint32 = ntohl(bt_get_unaligned((uint32_t *) p));
+ break;
+ case SDP_INT64:
+ case SDP_UINT64:
+ if (bufsize < sizeof(uint64_t)) {
+ SDPERR("Unexpected end of packet");
+ free(d);
+ return NULL;
+ }
+ *len += sizeof(uint64_t);
+ d->val.uint64 = ntoh64(bt_get_unaligned((uint64_t *) p));
+ break;
+ case SDP_INT128:
+ case SDP_UINT128:
+ if (bufsize < sizeof(uint128_t)) {
+ SDPERR("Unexpected end of packet");
+ free(d);
+ return NULL;
+ }
+ *len += sizeof(uint128_t);
+ ntoh128((uint128_t *) p, &d->val.uint128);
+ break;
+ default:
+ free(d);
+ d = NULL;
+ }
+ return d;
+}
+
+static sdp_data_t *extract_uuid(const uint8_t *p, int bufsize, int *len, sdp_record_t *rec)
+{
+ sdp_data_t *d = malloc(sizeof(sdp_data_t));
+
+ SDPDBG("Extracting UUID");
+ memset(d, 0, sizeof(sdp_data_t));
+ if (sdp_uuid_extract_safe(p, bufsize, &d->val.uuid, len) < 0) {
+ free(d);
+ return NULL;
+ }
+ d->dtd = *(uint8_t *) p;
+ if (rec)
+ sdp_pattern_add_uuid(rec, &d->val.uuid);
+ return d;
+}
+
+/*
+ * Extract strings from the PDU (could be service description and similar info)
+ */
+static sdp_data_t *extract_str(const void *p, int bufsize, int *len)
+{
+ char *s;
+ int n;
+ sdp_data_t *d;
+
+ if (bufsize < sizeof(uint8_t)) {
+ SDPERR("Unexpected end of packet");
+ return NULL;
+ }
+
+ d = malloc(sizeof(sdp_data_t));
+
+ memset(d, 0, sizeof(sdp_data_t));
+ d->dtd = *(uint8_t *) p;
+ p += sizeof(uint8_t);
+ *len += sizeof(uint8_t);
+ bufsize -= sizeof(uint8_t);
+
+ switch (d->dtd) {
+ case SDP_TEXT_STR8:
+ case SDP_URL_STR8:
+ if (bufsize < sizeof(uint8_t)) {
+ SDPERR("Unexpected end of packet");
+ free(d);
+ return NULL;
+ }
+ n = *(uint8_t *) p;
+ p += sizeof(uint8_t);
+ *len += sizeof(uint8_t);
+ bufsize -= sizeof(uint8_t);
+ break;
+ case SDP_TEXT_STR16:
+ case SDP_URL_STR16:
+ if (bufsize < sizeof(uint16_t)) {
+ SDPERR("Unexpected end of packet");
+ free(d);
+ return NULL;
+ }
+ n = ntohs(bt_get_unaligned((uint16_t *) p));
+ p += sizeof(uint16_t);
+ *len += sizeof(uint16_t) + n;
+ bufsize -= sizeof(uint16_t);
+ break;
+ default:
+ SDPERR("Sizeof text string > UINT16_MAX\n");
+ free(d);
+ return 0;
+ }
+
+ if (bufsize < n) {
+ SDPERR("String too long to fit in packet");
+ free(d);
+ return NULL;
+ }
+
+ s = malloc(n + 1);
+ if (!s) {
+ SDPERR("Not enough memory for incoming string");
+ free(d);
+ return NULL;
+ }
+ memset(s, 0, n + 1);
+ memcpy(s, p, n);
+
+ *len += n;
+
+ SDPDBG("Len : %d\n", n);
+ SDPDBG("Str : %s\n", s);
+
+ d->val.str = s;
+ d->unitSize = n + sizeof(uint8_t);
+ return d;
+}
+
+/*
+ * Extract the sequence type and its length, and return offset into buf
+ * or 0 on failure.
+ */
+int sdp_extract_seqtype_safe(const uint8_t *buf, int bufsize, uint8_t *dtdp, int *size)
+{
+ uint8_t dtd;
+ int scanned = sizeof(uint8_t);
+
+ if (bufsize < sizeof(uint8_t)) {
+ SDPERR("Unexpected end of packet");
+ return 0;
+ }
+
+ dtd = *(uint8_t *) buf;
+ buf += sizeof(uint8_t);
+ bufsize -= sizeof(uint8_t);
+ *dtdp = dtd;
+ switch (dtd) {
+ case SDP_SEQ8:
+ case SDP_ALT8:
+ if (bufsize < sizeof(uint8_t)) {
+ SDPERR("Unexpected end of packet");
+ return 0;
+ }
+ *size = *(uint8_t *) buf;
+ scanned += sizeof(uint8_t);
+ break;
+ case SDP_SEQ16:
+ case SDP_ALT16:
+ if (bufsize < sizeof(uint16_t)) {
+ SDPERR("Unexpected end of packet");
+ return 0;
+ }
+ *size = ntohs(bt_get_unaligned((uint16_t *) buf));
+ scanned += sizeof(uint16_t);
+ break;
+ case SDP_SEQ32:
+ case SDP_ALT32:
+ if (bufsize < sizeof(uint32_t)) {
+ SDPERR("Unexpected end of packet");
+ return 0;
+ }
+ *size = ntohl(bt_get_unaligned((uint32_t *) buf));
+ scanned += sizeof(uint32_t);
+ break;
+ default:
+ SDPERR("Unknown sequence type, aborting\n");
+ return 0;
+ }
+ return scanned;
+}
+
+int sdp_extract_seqtype(const uint8_t *buf, uint8_t *dtdp, int *size)
+{
+ /* Assume buf points to a buffer of size at least SDP_MAX_ATTR_LEN,
+ because we don't have any better information */
+ return sdp_extract_seqtype_safe(buf, SDP_MAX_ATTR_LEN, dtdp, size);
+}
+
+static sdp_data_t *extract_seq(const void *p, int bufsize, int *len, sdp_record_t *rec)
+{
+ int seqlen, n = 0;
+ sdp_data_t *curr, *prev;
+ sdp_data_t *d = malloc(sizeof(sdp_data_t));
+
+ SDPDBG("Extracting SEQ");
+ memset(d, 0, sizeof(sdp_data_t));
+ *len = sdp_extract_seqtype_safe(p, bufsize, &d->dtd, &seqlen);
+ SDPDBG("Sequence Type : 0x%x length : 0x%x\n", d->dtd, seqlen);
+
+ if (*len == 0)
+ return d;
+
+ if (*len > bufsize) {
+ SDPERR("Packet not big enough to hold sequence.");
+ free(d);
+ return NULL;
+ }
+
+ p += *len;
+ bufsize -= *len;
+ curr = prev = NULL;
+ while (n < seqlen) {
+ int attrlen = 0;
+ curr = sdp_extract_attr_safe(p, bufsize, &attrlen, rec);
+ if (curr == NULL)
+ break;
+
+ if (prev)
+ prev->next = curr;
+ else
+ d->val.dataseq = curr;
+ prev = curr;
+ p += attrlen;
+ n += attrlen;
+ bufsize -= attrlen;
+
+ SDPDBG("Extracted: %d SequenceLength: %d", n, seqlen);
+ }
+
+ *len += n;
+ return d;
+}
+
+sdp_data_t *sdp_extract_attr_safe(const uint8_t *p, int bufsize, int *size, sdp_record_t *rec)
+{
+ sdp_data_t *elem;
+ int n = 0;
+ uint8_t dtd;
+
+ if (bufsize < sizeof(uint8_t)) {
+ SDPERR("Unexpected end of packet");
+ return NULL;
+ }
+
+ dtd = *(const uint8_t *)p;
+
+ SDPDBG("extract_attr: dtd=0x%x", dtd);
+ switch (dtd) {
+ case SDP_DATA_NIL:
+ case SDP_BOOL:
+ case SDP_UINT8:
+ case SDP_UINT16:
+ case SDP_UINT32:
+ case SDP_UINT64:
+ case SDP_UINT128:
+ case SDP_INT8:
+ case SDP_INT16:
+ case SDP_INT32:
+ case SDP_INT64:
+ case SDP_INT128:
+ elem = extract_int(p, bufsize, &n);
+ break;
+ case SDP_UUID16:
+ case SDP_UUID32:
+ case SDP_UUID128:
+ elem = extract_uuid(p, bufsize, &n, rec);
+ break;
+ case SDP_TEXT_STR8:
+ case SDP_TEXT_STR16:
+ case SDP_TEXT_STR32:
+ case SDP_URL_STR8:
+ case SDP_URL_STR16:
+ case SDP_URL_STR32:
+ elem = extract_str(p, bufsize, &n);
+ break;
+ case SDP_SEQ8:
+ case SDP_SEQ16:
+ case SDP_SEQ32:
+ case SDP_ALT8:
+ case SDP_ALT16:
+ case SDP_ALT32:
+ elem = extract_seq(p, bufsize, &n, rec);
+ break;
+ default:
+ SDPERR("Unknown data descriptor : 0x%x terminating\n", dtd);
+ return NULL;
+ }
+ *size += n;
+ return elem;
+}
+
+sdp_data_t *sdp_extract_attr(const uint8_t *p, int *size, sdp_record_t *rec)
+{
+ /* Assume p points to a buffer of size at least SDP_MAX_ATTR_LEN,
+ because we don't have any better information */
+ return sdp_extract_attr_safe(p, SDP_MAX_ATTR_LEN, size, rec);
+}
+
+#ifdef SDP_DEBUG
+static void attr_print_func(void *value, void *userData)
+{
+ sdp_data_t *d = (sdp_data_t *)value;
+
+ SDPDBG("=====================================\n");
+ SDPDBG("ATTRIBUTE IDENTIFIER : 0x%x\n", d->attrId);
+ SDPDBG("ATTRIBUTE VALUE PTR : 0x%x\n", (uint32_t)value);
+ if (d)
+ sdp_data_print(d);
+ else
+ SDPDBG("NULL value\n");
+ SDPDBG("=====================================\n");
+}
+
+void sdp_print_service_attr(sdp_list_t *svcAttrList)
+{
+ SDPDBG("Printing service attr list %p\n", svcAttrList);
+ sdp_list_foreach(svcAttrList, attr_print_func, NULL);
+ SDPDBG("Printed service attr list %p\n", svcAttrList);
+}
+#endif
+
+sdp_record_t *sdp_extract_pdu_safe(const uint8_t *buf, int bufsize, int *scanned)
+{
+ int extracted = 0, seqlen = 0;
+ uint8_t dtd;
+ uint16_t attr;
+ sdp_record_t *rec = sdp_record_alloc();
+ const uint8_t *p = buf;
+
+ *scanned = sdp_extract_seqtype_safe(buf, bufsize, &dtd, &seqlen);
+ p += *scanned;
+ bufsize -= *scanned;
+ rec->attrlist = NULL;
+
+ while (extracted < seqlen && bufsize > 0) {
+ int n = sizeof(uint8_t), attrlen = 0;
+ sdp_data_t *data = NULL;
+
+ SDPDBG("Extract PDU, sequenceLength: %d localExtractedLength: %d",
+ seqlen, extracted);
+
+ if (bufsize < n + sizeof(uint16_t)) {
+ SDPERR("Unexpected end of packet");
+ break;
+ }
+
+ dtd = *(uint8_t *) p;
+ attr = ntohs(bt_get_unaligned((uint16_t *) (p + n)));
+ n += sizeof(uint16_t);
+
+ SDPDBG("DTD of attrId : %d Attr id : 0x%x \n", dtd, attr);
+
+ data = sdp_extract_attr_safe(p + n, bufsize - n, &attrlen, rec);
+
+ SDPDBG("Attr id : 0x%x attrValueLength : %d\n", attr, attrlen);
+
+ n += attrlen;
+ if (data == NULL) {
+ SDPDBG("Terminating extraction of attributes");
+ break;
+ }
+
+ if (attr == SDP_ATTR_RECORD_HANDLE)
+ rec->handle = data->val.uint32;
+
+ if (attr == SDP_ATTR_SVCLASS_ID_LIST)
+ extract_svclass_uuid(data, &rec->svclass);
+
+ extracted += n;
+ p += n;
+ bufsize -= n;
+ sdp_attr_replace(rec, attr, data);
+
+ SDPDBG("Extract PDU, seqLength: %d localExtractedLength: %d",
+ seqlen, extracted);
+ }
+#ifdef SDP_DEBUG
+ SDPDBG("Successful extracting of Svc Rec attributes\n");
+ sdp_print_service_attr(rec->attrlist);
+#endif
+ *scanned += seqlen;
+ return rec;
+}
+
+sdp_record_t *sdp_extract_pdu(const uint8_t *buf, int *scanned)
+{
+ /* Assume buf points to a buffer of size at least SDP_MAX_ATTR_LEN,
+ because we don't have any better information */
+ return sdp_extract_pdu_safe(buf, SDP_MAX_ATTR_LEN, scanned);
+}
+
+#ifdef SDP_DEBUG
+static void print_dataseq(sdp_data_t *p)
+{
+ sdp_data_t *d;
+
+ for (d = p; d; d = d->next)
+ sdp_data_print(d);
+}
+#endif
+
+void sdp_record_print(const sdp_record_t *rec)
+{
+ sdp_data_t *d = sdp_data_get(rec, SDP_ATTR_SVCNAME_PRIMARY);
+ if (d)
+ printf("Service Name: %.*s\n", d->unitSize, d->val.str);
+ d = sdp_data_get(rec, SDP_ATTR_SVCDESC_PRIMARY);
+ if (d)
+ printf("Service Description: %.*s\n", d->unitSize, d->val.str);
+ d = sdp_data_get(rec, SDP_ATTR_PROVNAME_PRIMARY);
+ if (d)
+ printf("Service Provider: %.*s\n", d->unitSize, d->val.str);
+}
+
+#ifdef SDP_DEBUG
+void sdp_data_print(sdp_data_t *d)
+{
+ switch (d->dtd) {
+ case SDP_DATA_NIL:
+ SDPDBG("NIL\n");
+ break;
+ case SDP_BOOL:
+ case SDP_UINT8:
+ case SDP_UINT16:
+ case SDP_UINT32:
+ case SDP_UINT64:
+ case SDP_UINT128:
+ case SDP_INT8:
+ case SDP_INT16:
+ case SDP_INT32:
+ case SDP_INT64:
+ case SDP_INT128:
+ SDPDBG("Integer : 0x%x\n", d->val.uint32);
+ break;
+ case SDP_UUID16:
+ case SDP_UUID32:
+ case SDP_UUID128:
+ SDPDBG("UUID\n");
+ sdp_uuid_print(&d->val.uuid);
+ break;
+ case SDP_TEXT_STR8:
+ case SDP_TEXT_STR16:
+ case SDP_TEXT_STR32:
+ SDPDBG("Text : %s\n", d->val.str);
+ break;
+ case SDP_URL_STR8:
+ case SDP_URL_STR16:
+ case SDP_URL_STR32:
+ SDPDBG("URL : %s\n", d->val.str);
+ break;
+ case SDP_SEQ8:
+ case SDP_SEQ16:
+ case SDP_SEQ32:
+ print_dataseq(d->val.dataseq);
+ break;
+ case SDP_ALT8:
+ case SDP_ALT16:
+ case SDP_ALT32:
+ SDPDBG("Data Sequence Alternates\n");
+ print_dataseq(d->val.dataseq);
+ break;
+ }
+}
+#endif
+
+sdp_data_t *sdp_data_get(const sdp_record_t *rec, uint16_t attrId)
+{
+ if (rec->attrlist) {
+ sdp_data_t sdpTemplate;
+ sdp_list_t *p;
+
+ sdpTemplate.attrId = attrId;
+ p = sdp_list_find(rec->attrlist, &sdpTemplate, sdp_attrid_comp_func);
+ if (p)
+ return (sdp_data_t *)p->data;
+ }
+ return NULL;
+}
+
+int sdp_send_req(sdp_session_t *session, uint8_t *buf, uint32_t size)
+{
+ uint32_t sent = 0;
+
+ while (sent < size) {
+ int n = send(session->sock, buf + sent, size - sent, 0);
+ if (n < 0)
+ return -1;
+ sent += n;
+ }
+ return 0;
+}
+
+int sdp_read_rsp(sdp_session_t *session, uint8_t *buf, uint32_t size)
+{
+ fd_set readFds;
+ struct timeval timeout = { SDP_RESPONSE_TIMEOUT, 0 };
+
+ FD_ZERO(&readFds);
+ FD_SET(session->sock, &readFds);
+ SDPDBG("Waiting for response\n");
+ if (select(session->sock + 1, &readFds, NULL, NULL, &timeout) == 0) {
+ SDPERR("Client timed out\n");
+ errno = ETIMEDOUT;
+ return -1;
+ }
+ return recv(session->sock, buf, size, 0);
+}
+
+/*
+ * generic send request, wait for response method.
+ */
+int sdp_send_req_w4_rsp(sdp_session_t *session, uint8_t *reqbuf, uint8_t *rspbuf, uint32_t reqsize, uint32_t *rspsize)
+{
+ int n;
+ sdp_pdu_hdr_t *reqhdr = (sdp_pdu_hdr_t *)reqbuf;
+ sdp_pdu_hdr_t *rsphdr = (sdp_pdu_hdr_t *)rspbuf;
+
+ SDPDBG("");
+ if (0 > sdp_send_req(session, reqbuf, reqsize)) {
+ SDPERR("Error sending data:%s", strerror(errno));
+ return -1;
+ }
+ n = sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE);
+ if (0 > n)
+ return -1;
+ SDPDBG("Read : %d\n", n);
+ if (n == 0 || reqhdr->tid != rsphdr->tid) {
+ errno = EPROTO;
+ return -1;
+ }
+ *rspsize = n;
+ return 0;
+}
+
+/*
+ * singly-linked lists (after openobex implementation)
+ */
+sdp_list_t *sdp_list_append(sdp_list_t *p, void *d)
+{
+ sdp_list_t *q, *n = malloc(sizeof(sdp_list_t));
+
+ if (!n)
+ return 0;
+
+ n->data = d;
+ n->next = 0;
+
+ if (!p)
+ return n;
+
+ for (q = p; q->next; q = q->next);
+ q->next = n;
+
+ return p;
+}
+
+sdp_list_t *sdp_list_remove(sdp_list_t *list, void *d)
+{
+ sdp_list_t *p, *q;
+
+ for (q = 0, p = list; p; q = p, p = p->next)
+ if (p->data == d) {
+ if (q)
+ q->next = p->next;
+ else
+ list = p->next;
+ free(p);
+ break;
+ }
+
+ return list;
+}
+
+sdp_list_t *sdp_list_insert_sorted(sdp_list_t *list, void *d, sdp_comp_func_t f)
+{
+ sdp_list_t *q, *p, *n;
+
+ n = malloc(sizeof(sdp_list_t));
+ if (!n)
+ return 0;
+ n->data = d;
+ for (q = 0, p = list; p; q = p, p = p->next)
+ if (f(p->data, d) >= 0)
+ break;
+ // insert between q and p; if !q insert at head
+ if (q)
+ q->next = n;
+ else
+ list = n;
+ n->next = p;
+ return list;
+}
+
+/*
+ * Every element of the list points to things which need
+ * to be free()'d. This method frees the list's contents
+ */
+void sdp_list_free(sdp_list_t *list, sdp_free_func_t f)
+{
+ sdp_list_t *next;
+ while (list) {
+ next = list->next;
+ if (f)
+ f(list->data);
+ free(list);
+ list = next;
+ }
+}
+
+static inline int __find_port(sdp_data_t *seq, int proto)
+{
+ if (!seq || !seq->next)
+ return 0;
+
+ if (SDP_IS_UUID(seq->dtd) && sdp_uuid_to_proto(&seq->val.uuid) == proto) {
+ seq = seq->next;
+ switch (seq->dtd) {
+ case SDP_UINT8:
+ return seq->val.uint8;
+ case SDP_UINT16:
+ return seq->val.uint16;
+ }
+ }
+ return 0;
+}
+
+int sdp_get_proto_port(const sdp_list_t *list, int proto)
+{
+ if (proto != L2CAP_UUID && proto != RFCOMM_UUID) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ for (; list; list = list->next) {
+ sdp_list_t *p;
+ for (p = list->data; p; p = p->next) {
+ sdp_data_t *seq = (sdp_data_t *) p->data;
+ int port = __find_port(seq, proto);
+ if (port)
+ return port;
+ }
+ }
+ return 0;
+}
+
+sdp_data_t *sdp_get_proto_desc(sdp_list_t *list, int proto)
+{
+ for (; list; list = list->next) {
+ sdp_list_t *p;
+ for (p = list->data; p; p = p->next) {
+ sdp_data_t *seq = (sdp_data_t *) p->data;
+ if (SDP_IS_UUID(seq->dtd) &&
+ sdp_uuid_to_proto(&seq->val.uuid) == proto)
+ return seq->next;
+ }
+ }
+ return NULL;
+}
+
+int sdp_get_access_protos(const sdp_record_t *rec, sdp_list_t **pap)
+{
+ sdp_data_t *pdlist, *curr;
+ sdp_list_t *ap = 0;
+
+ pdlist = sdp_data_get(rec, SDP_ATTR_PROTO_DESC_LIST);
+ if (pdlist == NULL) {
+ errno = ENODATA;
+ return -1;
+ }
+ SDPDBG("AP type : 0%x\n", pdlist->dtd);
+
+ for (; pdlist; pdlist = pdlist->next) {
+ sdp_list_t *pds = 0;
+ for (curr = pdlist->val.dataseq; curr; curr = curr->next)
+ pds = sdp_list_append(pds, curr->val.dataseq);
+ ap = sdp_list_append(ap, pds);
+ }
+ *pap = ap;
+ return 0;
+}
+
+int sdp_get_add_access_protos(const sdp_record_t *rec, sdp_list_t **pap)
+{
+ sdp_data_t *pdlist, *curr;
+ sdp_list_t *ap = 0;
+
+ pdlist = sdp_data_get(rec, SDP_ATTR_ADD_PROTO_DESC_LIST);
+ if (pdlist == NULL) {
+ errno = ENODATA;
+ return -1;
+ }
+ SDPDBG("AP type : 0%x\n", pdlist->dtd);
+
+ pdlist = pdlist->val.dataseq;
+
+ for (; pdlist; pdlist = pdlist->next) {
+ sdp_list_t *pds = 0;
+ for (curr = pdlist->val.dataseq; curr; curr = curr->next)
+ pds = sdp_list_append(pds, curr->val.dataseq);
+ ap = sdp_list_append(ap, pds);
+ }
+ *pap = ap;
+ return 0;
+}
+
+int sdp_get_uuidseq_attr(const sdp_record_t *rec, uint16_t attr, sdp_list_t **seqp)
+{
+ sdp_data_t *sdpdata = sdp_data_get(rec, attr);
+
+ *seqp = NULL;
+ if (sdpdata && sdpdata->dtd >= SDP_SEQ8 && sdpdata->dtd <= SDP_SEQ32) {
+ sdp_data_t *d;
+ for (d = sdpdata->val.dataseq; d; d = d->next) {
+ uuid_t *u;
+ if (d->dtd < SDP_UUID16 || d->dtd > SDP_UUID128)
+ goto fail;
+
+ u = malloc(sizeof(uuid_t));
+ memset(u, 0, sizeof(uuid_t));
+ *u = d->val.uuid;
+ *seqp = sdp_list_append(*seqp, u);
+ }
+ return 0;
+ }
+fail:
+ sdp_list_free(*seqp, free);
+ errno = EINVAL;
+ return -1;
+}
+
+int sdp_set_uuidseq_attr(sdp_record_t *rec, uint16_t aid, sdp_list_t *seq)
+{
+ int status = 0, i, len;
+ void **dtds, **values;
+ uint8_t uuid16 = SDP_UUID16;
+ uint8_t uuid32 = SDP_UUID32;
+ uint8_t uuid128 = SDP_UUID128;
+ sdp_list_t *p;
+
+ len = sdp_list_len(seq);
+ if (!seq || len == 0)
+ return -1;
+ dtds = (void **)malloc(len * sizeof(void *));
+ values = (void **)malloc(len * sizeof(void *));
+ for (p = seq, i = 0; i < len; i++, p = p->next) {
+ uuid_t *uuid = (uuid_t *)p->data;
+ if (uuid)
+ switch (uuid->type) {
+ case SDP_UUID16:
+ dtds[i] = &uuid16;
+ values[i] = &uuid->value.uuid16;
+ break;
+ case SDP_UUID32:
+ dtds[i] = &uuid32;
+ values[i] = &uuid->value.uuid32;
+ break;
+ case SDP_UUID128:
+ dtds[i] = &uuid128;
+ values[i] = &uuid->value.uuid128;
+ break;
+ default:
+ status = -1;
+ break;
+ }
+ else {
+ status = -1;
+ break;
+ }
+ }
+ if (status == 0) {
+ sdp_data_t *data = sdp_seq_alloc(dtds, values, len);
+ sdp_attr_replace(rec, aid, data);
+ sdp_pattern_add_uuidseq(rec, seq);
+ }
+ free(dtds);
+ free(values);
+ return status;
+}
+
+int sdp_get_lang_attr(const sdp_record_t *rec, sdp_list_t **langSeq)
+{
+ sdp_lang_attr_t *lang;
+ sdp_data_t *sdpdata, *curr_data;
+
+ *langSeq = NULL;
+ sdpdata = sdp_data_get(rec, SDP_ATTR_LANG_BASE_ATTR_ID_LIST);
+ if (sdpdata == NULL) {
+ errno = ENODATA;
+ return -1;
+ }
+ curr_data = sdpdata->val.dataseq;
+ while (curr_data) {
+ sdp_data_t *pCode = curr_data;
+ sdp_data_t *pEncoding = pCode->next;
+ sdp_data_t *pOffset = pEncoding->next;
+ if (pCode && pEncoding && pOffset) {
+ lang = malloc(sizeof(sdp_lang_attr_t));
+ lang->code_ISO639 = pCode->val.uint16;
+ lang->encoding = pEncoding->val.uint16;
+ lang->base_offset = pOffset->val.uint16;
+ SDPDBG("code_ISO639 : 0x%02x\n", lang->code_ISO639);
+ SDPDBG("encoding : 0x%02x\n", lang->encoding);
+ SDPDBG("base_offfset : 0x%02x\n", lang->base_offset);
+ *langSeq = sdp_list_append(*langSeq, lang);
+ }
+ curr_data = pOffset->next;
+ }
+ return 0;
+}
+
+int sdp_get_profile_descs(const sdp_record_t *rec, sdp_list_t **profDescSeq)
+{
+ sdp_profile_desc_t *profDesc;
+ sdp_data_t *sdpdata, *seq;
+
+ *profDescSeq = NULL;
+ sdpdata = sdp_data_get(rec, SDP_ATTR_PFILE_DESC_LIST);
+ if (!sdpdata || !sdpdata->val.dataseq) {
+ errno = ENODATA;
+ return -1;
+ }
+ for (seq = sdpdata->val.dataseq; seq && seq->val.dataseq; seq = seq->next) {
+ uuid_t *uuid = NULL;
+ uint16_t version = 0x100;
+
+ if (SDP_IS_UUID(seq->dtd)) {
+ uuid = &seq->val.uuid;
+ } else {
+ sdp_data_t *puuid = seq->val.dataseq;
+ sdp_data_t *pVnum = seq->val.dataseq->next;
+ if (puuid && pVnum) {
+ uuid = &puuid->val.uuid;
+ version = pVnum->val.uint16;
+ }
+ }
+
+ if (uuid != NULL) {
+ profDesc = malloc(sizeof(sdp_profile_desc_t));
+ profDesc->uuid = *uuid;
+ profDesc->version = version;
+#ifdef SDP_DEBUG
+ sdp_uuid_print(&profDesc->uuid);
+ SDPDBG("Vnum : 0x%04x\n", profDesc->version);
+#endif
+ *profDescSeq = sdp_list_append(*profDescSeq, profDesc);
+ }
+ }
+ return 0;
+}
+
+int sdp_get_server_ver(const sdp_record_t *rec, sdp_list_t **u16)
+{
+ sdp_data_t *d, *curr;
+
+ *u16 = NULL;
+ d = sdp_data_get(rec, SDP_ATTR_VERSION_NUM_LIST);
+ if (d == NULL) {
+ errno = ENODATA;
+ return -1;
+ }
+ for (curr = d->val.dataseq; curr; curr = curr->next)
+ *u16 = sdp_list_append(*u16, &curr->val.uint16);
+ return 0;
+}
+
+/* flexible extraction of basic attributes - Jean II */
+/* How do we expect caller to extract predefined data sequences? */
+int sdp_get_int_attr(const sdp_record_t *rec, uint16_t attrid, int *value)
+{
+ sdp_data_t *sdpdata = sdp_data_get(rec, attrid);
+
+ if (sdpdata)
+ /* Verify that it is what the caller expects */
+ if (sdpdata->dtd == SDP_BOOL || sdpdata->dtd == SDP_UINT8 ||
+ sdpdata->dtd == SDP_UINT16 || sdpdata->dtd == SDP_UINT32 ||
+ sdpdata->dtd == SDP_INT8 || sdpdata->dtd == SDP_INT16 ||
+ sdpdata->dtd == SDP_INT32) {
+ *value = sdpdata->val.uint32;
+ return 0;
+ }
+ errno = EINVAL;
+ return -1;
+}
+
+int sdp_get_string_attr(const sdp_record_t *rec, uint16_t attrid, char *value, int valuelen)
+{
+ sdp_data_t *sdpdata = sdp_data_get(rec, attrid);
+ if (sdpdata)
+ /* Verify that it is what the caller expects */
+ if (sdpdata->dtd == SDP_TEXT_STR8 || sdpdata->dtd == SDP_TEXT_STR16 || sdpdata->dtd == SDP_TEXT_STR32)
+ if (strlen(sdpdata->val.str) < valuelen) {
+ strcpy(value, sdpdata->val.str);
+ return 0;
+ }
+ errno = EINVAL;
+ return -1;
+}
+
+#define get_basic_attr(attrID, pAttrValue, fieldName) \
+ sdp_data_t *data = sdp_data_get(rec, attrID); \
+ if (data) { \
+ *pAttrValue = data->val.fieldName; \
+ return 0; \
+ } \
+ errno = EINVAL; \
+ return -1;
+
+int sdp_get_service_id(const sdp_record_t *rec, uuid_t *uuid)
+{
+ get_basic_attr(SDP_ATTR_SERVICE_ID, uuid, uuid);
+}
+
+int sdp_get_group_id(const sdp_record_t *rec, uuid_t *uuid)
+{
+ get_basic_attr(SDP_ATTR_GROUP_ID, uuid, uuid);
+}
+
+int sdp_get_record_state(const sdp_record_t *rec, uint32_t *svcRecState)
+{
+ get_basic_attr(SDP_ATTR_RECORD_STATE, svcRecState, uint32);
+}
+
+int sdp_get_service_avail(const sdp_record_t *rec, uint8_t *svcAvail)
+{
+ get_basic_attr(SDP_ATTR_SERVICE_AVAILABILITY, svcAvail, uint8);
+}
+
+int sdp_get_service_ttl(const sdp_record_t *rec, uint32_t *svcTTLInfo)
+{
+ get_basic_attr(SDP_ATTR_SVCINFO_TTL, svcTTLInfo, uint32);
+}
+
+int sdp_get_database_state(const sdp_record_t *rec, uint32_t *svcDBState)
+{
+ get_basic_attr(SDP_ATTR_SVCDB_STATE, svcDBState, uint32);
+}
+
+/*
+ * NOTE that none of the setXXX() functions below will
+ * actually update the SDP server, unless the
+ * {register, update}sdp_record_t() function is invoked.
+ */
+
+int sdp_attr_add_new(sdp_record_t *rec, uint16_t attr, uint8_t dtd, const void *value)
+{
+ sdp_data_t *d = sdp_data_alloc(dtd, value);
+ if (d) {
+ sdp_attr_replace(rec, attr, d);
+ return 0;
+ }
+ return -1;
+}
+
+/*
+ * Set the information attributes of the service
+ * pointed to by rec. The attributes are
+ * service name, description and provider name
+ */
+void sdp_set_info_attr(sdp_record_t *rec, const char *name, const char *prov, const char *desc)
+{
+ if (name)
+ sdp_attr_add_new(rec, SDP_ATTR_SVCNAME_PRIMARY, SDP_TEXT_STR8, (void *)name);
+ if (prov)
+ sdp_attr_add_new(rec, SDP_ATTR_PROVNAME_PRIMARY, SDP_TEXT_STR8, (void *)prov);
+ if (desc)
+ sdp_attr_add_new(rec, SDP_ATTR_SVCDESC_PRIMARY, SDP_TEXT_STR8, (void *)desc);
+}
+
+static sdp_data_t *access_proto_to_dataseq(sdp_record_t *rec, sdp_list_t *proto)
+{
+ sdp_data_t *seq = NULL;
+ void *dtds[10], *values[10];
+ void **seqDTDs, **seqs;
+ int i, seqlen;
+ sdp_list_t *p;
+
+ seqlen = sdp_list_len(proto);
+ seqDTDs = (void **)malloc(seqlen * sizeof(void *));
+ seqs = (void **)malloc(seqlen * sizeof(void *));
+ for (i = 0, p = proto; p; p = p->next, i++) {
+ sdp_list_t *elt = (sdp_list_t *)p->data;
+ sdp_data_t *s;
+ int pslen = 0;
+ for (; elt && pslen < sizeof(dtds); elt = elt->next, pslen++) {
+ sdp_data_t *d = (sdp_data_t *)elt->data;
+ dtds[pslen] = &d->dtd;
+ switch (d->dtd) {
+ case SDP_UUID16:
+ values[pslen] = &((uuid_t *)d)->value.uuid16;
+ break;
+ case SDP_UUID32:
+ values[pslen] = &((uuid_t *)d)->value.uuid32;
+ break;
+ case SDP_UUID128:
+ values[pslen] = &((uuid_t *)d)->value.uuid128;
+ break;
+ case SDP_UINT8:
+ values[pslen] = &d->val.uint8;
+ break;
+ case SDP_UINT16:
+ values[pslen] = &d->val.uint16;
+ break;
+ case SDP_SEQ8:
+ case SDP_SEQ16:
+ case SDP_SEQ32:
+ values[pslen] = d;
+ break;
+ // FIXME: more
+ }
+ }
+ s = sdp_seq_alloc(dtds, values, pslen);
+ if (s) {
+ seqDTDs[i] = &s->dtd;
+ seqs[i] = s;
+ }
+ }
+ seq = sdp_seq_alloc(seqDTDs, seqs, seqlen);
+ free(seqDTDs);
+ free(seqs);
+ return seq;
+}
+
+/*
+ * sets the access protocols of the service specified
+ * to the value specified in "access_proto"
+ *
+ * Note that if there are alternate mechanisms by
+ * which the service is accessed, then they should
+ * be specified as sequences
+ *
+ * Using a value of NULL for accessProtocols has
+ * effect of removing this attribute (if previously set)
+ *
+ * This function replaces the existing sdp_access_proto_t
+ * structure (if any) with the new one specified.
+ *
+ * returns 0 if successful or -1 if there is a failure.
+ */
+int sdp_set_access_protos(sdp_record_t *rec, const sdp_list_t *ap)
+{
+ const sdp_list_t *p;
+ sdp_data_t *protos = NULL;
+
+ for (p = ap; p; p = p->next) {
+ sdp_data_t *seq = access_proto_to_dataseq(rec, (sdp_list_t *) p->data);
+ protos = sdp_seq_append(protos, seq);
+ }
+
+ sdp_attr_add(rec, SDP_ATTR_PROTO_DESC_LIST, protos);
+
+ return 0;
+}
+
+int sdp_set_add_access_protos(sdp_record_t *rec, const sdp_list_t *ap)
+{
+ const sdp_list_t *p;
+ sdp_data_t *protos = NULL;
+
+ for (p = ap; p; p = p->next) {
+ sdp_data_t *seq = access_proto_to_dataseq(rec, (sdp_list_t *) p->data);
+ protos = sdp_seq_append(protos, seq);
+ }
+
+ sdp_attr_add(rec, SDP_ATTR_ADD_PROTO_DESC_LIST,
+ protos ? sdp_data_alloc(SDP_SEQ8, protos) : NULL);
+
+ return 0;
+}
+
+/*
+ * set the "LanguageBase" attributes of the service record
+ * record to the value specified in "langAttrList".
+ *
+ * "langAttrList" is a linked list of "sdp_lang_attr_t"
+ * objects, one for each language in which user visible
+ * attributes are present in the service record.
+ *
+ * Using a value of NULL for langAttrList has
+ * effect of removing this attribute (if previously set)
+ *
+ * This function replaces the exisiting sdp_lang_attr_t
+ * structure (if any) with the new one specified.
+ *
+ * returns 0 if successful or -1 if there is a failure.
+ */
+int sdp_set_lang_attr(sdp_record_t *rec, const sdp_list_t *seq)
+{
+ uint8_t uint16 = SDP_UINT16;
+ int status = 0, i = 0, seqlen = sdp_list_len(seq);
+ void **dtds = (void **)malloc(3 * seqlen * sizeof(void *));
+ void **values = (void **)malloc(3 * seqlen * sizeof(void *));
+ const sdp_list_t *p;
+
+ for (p = seq; p; p = p->next) {
+ sdp_lang_attr_t *lang = (sdp_lang_attr_t *)p->data;
+ if (!lang) {
+ status = -1;
+ break;
+ }
+ dtds[i] = &uint16;
+ values[i] = &lang->code_ISO639;
+ i++;
+ dtds[i] = &uint16;
+ values[i] = &lang->encoding;
+ i++;
+ dtds[i] = &uint16;
+ values[i] = &lang->base_offset;
+ i++;
+ }
+ if (status == 0) {
+ sdp_data_t *seq = sdp_seq_alloc(dtds, values, 3 * seqlen);
+ sdp_attr_add(rec, SDP_ATTR_LANG_BASE_ATTR_ID_LIST, seq);
+ }
+ free(dtds);
+ free(values);
+ return status;
+}
+
+/*
+ * set the "ServiceID" attribute of the service.
+ *
+ * This is the UUID of the service.
+ *
+ * returns 0 if successful or -1 if there is a failure.
+ */
+void sdp_set_service_id(sdp_record_t *rec, uuid_t uuid)
+{
+ switch (uuid.type) {
+ case SDP_UUID16:
+ sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID16, &uuid.value.uuid16);
+ break;
+ case SDP_UUID32:
+ sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID32, &uuid.value.uuid32);
+ break;
+ case SDP_UUID128:
+ sdp_attr_add_new(rec, SDP_ATTR_SERVICE_ID, SDP_UUID128, &uuid.value.uuid128);
+ break;
+ }
+ sdp_pattern_add_uuid(rec, &uuid);
+}
+
+/*
+ * set the GroupID attribute of the service record defining a group.
+ *
+ * This is the UUID of the group.
+ *
+ * returns 0 if successful or -1 if there is a failure.
+ */
+void sdp_set_group_id(sdp_record_t *rec, uuid_t uuid)
+{
+ switch (uuid.type) {
+ case SDP_UUID16:
+ sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID16, &uuid.value.uuid16);
+ break;
+ case SDP_UUID32:
+ sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID32, &uuid.value.uuid32);
+ break;
+ case SDP_UUID128:
+ sdp_attr_add_new(rec, SDP_ATTR_GROUP_ID, SDP_UUID128, &uuid.value.uuid128);
+ break;
+ }
+ sdp_pattern_add_uuid(rec, &uuid);
+}
+
+/*
+ * set the ProfileDescriptorList attribute of the service record
+ * pointed to by record to the value specified in "profileDesc".
+ *
+ * Each element in the list is an object of type
+ * sdp_profile_desc_t which is a definition of the
+ * Bluetooth profile that this service conforms to.
+ *
+ * Using a value of NULL for profileDesc has
+ * effect of removing this attribute (if previously set)
+ *
+ * This function replaces the exisiting ProfileDescriptorList
+ * structure (if any) with the new one specified.
+ *
+ * returns 0 if successful or -1 if there is a failure.
+ */
+int sdp_set_profile_descs(sdp_record_t *rec, const sdp_list_t *profiles)
+{
+ int status = 0;
+ uint8_t uuid16 = SDP_UUID16;
+ uint8_t uuid32 = SDP_UUID32;
+ uint8_t uuid128 = SDP_UUID128;
+ uint8_t uint16 = SDP_UINT16;
+ int i = 0, seqlen = sdp_list_len(profiles);
+ void **seqDTDs = (void **)malloc(seqlen * sizeof(void *));
+ void **seqs = (void **)malloc(seqlen * sizeof(void *));
+ const sdp_list_t *p;
+
+ for (p = profiles; p; p = p->next) {
+ sdp_data_t *seq;
+ void *dtds[2], *values[2];
+ sdp_profile_desc_t *profile = (sdp_profile_desc_t *)p->data;
+ if (!profile) {
+ status = -1;
+ break;
+ }
+ switch (profile->uuid.type) {
+ case SDP_UUID16:
+ dtds[0] = &uuid16;
+ values[0] = &profile->uuid.value.uuid16;
+ break;
+ case SDP_UUID32:
+ dtds[0] = &uuid32;
+ values[0] = &profile->uuid.value.uuid32;
+ break;
+ case SDP_UUID128:
+ dtds[0] = &uuid128;
+ values[0] = &profile->uuid.value.uuid128;
+ break;
+ default:
+ status = -1;
+ break;
+ }
+ dtds[1] = &uint16;
+ values[1] = &profile->version;
+ seq = sdp_seq_alloc(dtds, values, 2);
+ if (seq) {
+ seqDTDs[i] = &seq->dtd;
+ seqs[i] = seq;
+ sdp_pattern_add_uuid(rec, &profile->uuid);
+ }
+ i++;
+ }
+ if (status == 0) {
+ sdp_data_t *pAPSeq = sdp_seq_alloc(seqDTDs, seqs, seqlen);
+ sdp_attr_add(rec, SDP_ATTR_PFILE_DESC_LIST, pAPSeq);
+ }
+ free(seqDTDs);
+ free(seqs);
+ return status;
+}
+
+/*
+ * sets various URL attributes of the service
+ * pointed to by record. The URL include
+ *
+ * client: a URL to the client's
+ * platform specific (WinCE, PalmOS) executable
+ * code that can be used to access this service.
+ *
+ * doc: a URL pointing to service documentation
+ *
+ * icon: a URL to an icon that can be used to represent
+ * this service.
+ *
+ * Note that you need to pass NULL for any URLs
+ * that you don't want to set or remove
+ */
+void sdp_set_url_attr(sdp_record_t *rec, const char *client, const char *doc, const char *icon)
+{
+ sdp_attr_add_new(rec, SDP_ATTR_CLNT_EXEC_URL, SDP_URL_STR8, client);
+ sdp_attr_add_new(rec, SDP_ATTR_DOC_URL, SDP_URL_STR8, doc);
+ sdp_attr_add_new(rec, SDP_ATTR_ICON_URL, SDP_URL_STR8, icon);
+}
+
+/*
+ * The code in this function is executed only once per
+ * thread. We compute the actual bit value of the Bluetooth
+ * base UUID which is a string defined in bt_std_values.h
+ * and is assumed to be of the standard form with "-" separators.
+ *
+ * The algorithm however converts the string to 4 unsigned longs
+ * using the strtoul() and assigns the values in sequence to
+ * the 128bit value
+ */
+uint128_t *sdp_create_base_uuid(void)
+{
+ uint128_t *base_uuid;
+ char baseStr[128];
+ int delim = '-';
+ unsigned long dataLongValue;
+ char *delimPtr;
+ char *dataPtr;
+ char temp[10];
+ int toBeCopied;
+ uint8_t *data;
+
+ strcpy(baseStr, BASE_UUID);
+ base_uuid = malloc(sizeof(uint128_t));
+ if (!base_uuid)
+ return NULL;
+
+ data = base_uuid->data;
+ memset(data, '\0', sizeof(uint128_t));
+ memset(temp, '\0', 10);
+ dataPtr = baseStr;
+ delimPtr = NULL;
+ delimPtr = strchr(dataPtr, delim);
+ toBeCopied = delimPtr - dataPtr;
+ if (toBeCopied != 8) {
+ SDPDBG("To be copied(1) : %d\n", toBeCopied);
+ free(base_uuid);
+ return NULL;
+ }
+ strncpy(temp, dataPtr, toBeCopied);
+ dataLongValue = htonl(strtoul(temp, NULL, 16));
+ memcpy(&data[0], &dataLongValue, 4);
+
+ /*
+ * Get the next 4 bytes (note that there is a "-"
+ * between them now)
+ */
+ memset(temp, '\0', 10);
+ dataPtr = delimPtr + 1;
+ delimPtr = strchr(dataPtr, delim);
+ toBeCopied = delimPtr - dataPtr;
+ if (toBeCopied != 4) {
+ SDPDBG("To be copied(2) : %d\n", toBeCopied);
+ free(base_uuid);
+ return NULL;
+ }
+ strncpy(temp, dataPtr, toBeCopied);
+ dataPtr = delimPtr + 1;
+ delimPtr = strchr(dataPtr, delim);
+ toBeCopied = delimPtr - dataPtr;
+ if (toBeCopied != 4) {
+ SDPDBG("To be copied(3) : %d\n", toBeCopied);
+ free(base_uuid);
+ return NULL;
+ }
+ strncat(temp, dataPtr, toBeCopied);
+ dataLongValue = htonl(strtoul(temp, NULL, 16));
+ memcpy(&data[4], &dataLongValue, 4);
+
+ /*
+ * Get the last 4 bytes (note that there are 6 bytes
+ * after the last separator, which is truncated (2+4)
+ */
+ memset(temp, '\0', 10);
+ dataPtr = delimPtr + 1;
+ dataPtr = delimPtr + 1;
+ delimPtr = strchr(dataPtr, delim);
+ toBeCopied = delimPtr - dataPtr;
+ if (toBeCopied != 4) {
+ SDPDBG("To be copied(4) : %d\n", toBeCopied);
+ free(base_uuid);
+ return NULL;
+ }
+ strncpy(temp, dataPtr, toBeCopied);
+ strncat(temp, (delimPtr + 1), 4);
+ dataLongValue = htonl(strtoul(temp, NULL, 16));
+ memcpy(&data[8], &dataLongValue, 4);
+ dataLongValue = htonl(strtoul(delimPtr + 5, NULL, 16));
+ memcpy(&data[12], &dataLongValue, 4);
+
+ return base_uuid;
+}
+
+uuid_t *sdp_uuid16_create(uuid_t *u, uint16_t val)
+{
+ memset(u, 0, sizeof(uuid_t));
+ u->type = SDP_UUID16;
+ u->value.uuid16 = val;
+ return u;
+}
+
+uuid_t *sdp_uuid32_create(uuid_t *u, uint32_t val)
+{
+ memset(u, 0, sizeof(uuid_t));
+ u->type = SDP_UUID32;
+ u->value.uuid32 = val;
+ return u;
+}
+
+uuid_t *sdp_uuid128_create(uuid_t *u, const void *val)
+{
+ memset(u, 0, sizeof(uuid_t));
+ u->type = SDP_UUID128;
+ memcpy(&u->value.uuid128, val, sizeof(uint128_t));
+ return u;
+}
+
+/*
+ * UUID comparison function
+ * returns 0 if uuidValue1 == uuidValue2 else -1
+ */
+int sdp_uuid16_cmp(const void *p1, const void *p2)
+{
+ const uuid_t *u1 = (const uuid_t *)p1;
+ const uuid_t *u2 = (const uuid_t *)p2;
+ return memcmp(&u1->value.uuid16, &u2->value.uuid16, sizeof(uint16_t));
+}
+
+/*
+ * UUID comparison function
+ * returns 0 if uuidValue1 == uuidValue2 else -1
+ */
+int sdp_uuid128_cmp(const void *p1, const void *p2)
+{
+ const uuid_t *u1 = (const uuid_t *)p1;
+ const uuid_t *u2 = (const uuid_t *)p2;
+ return memcmp(&u1->value.uuid128, &u2->value.uuid128, sizeof(uint128_t));
+}
+
+/*
+ * 128 to 16 bit and 32 to 16 bit UUID conversion functions
+ * yet to be implemented. Note that the input is in NBO in
+ * both 32 and 128 bit UUIDs and conversion is needed
+ */
+void sdp_uuid16_to_uuid128(uuid_t *uuid128, uuid_t *uuid16)
+{
+ /*
+ * We have a 16 bit value, which needs to be added to
+ * bytes 3 and 4 (at indices 2 and 3) of the Bluetooth base
+ */
+ unsigned short data1;
+
+ // allocate a 128bit UUID and init to the Bluetooth base UUID
+ uuid128->value.uuid128 = bluetooth_base_uuid;
+ uuid128->type = SDP_UUID128;
+
+ // extract bytes 2 and 3 of 128bit BT base UUID
+ memcpy(&data1, &bluetooth_base_uuid.data[2], 2);
+
+ // add the given UUID (16 bits)
+ data1 += htons(uuid16->value.uuid16);
+
+ // set bytes 2 and 3 of the 128 bit value
+ memcpy(&uuid128->value.uuid128.data[2], &data1, 2);
+}
+
+void sdp_uuid32_to_uuid128(uuid_t *uuid128, uuid_t *uuid32)
+{
+ /*
+ * We have a 32 bit value, which needs to be added to
+ * bytes 1->4 (at indices 0 thru 3) of the Bluetooth base
+ */
+ unsigned int data0;
+
+ // allocate a 128bit UUID and init to the Bluetooth base UUID
+ uuid128->value.uuid128 = bluetooth_base_uuid;
+ uuid128->type = SDP_UUID128;
+
+ // extract first 4 bytes
+ memcpy(&data0, &bluetooth_base_uuid.data[0], 4);
+
+ // add the given UUID (32bits)
+ data0 += htonl(uuid32->value.uuid32);
+
+ // set the 4 bytes of the 128 bit value
+ memcpy(&uuid128->value.uuid128.data[0], &data0, 4);
+}
+
+uuid_t *sdp_uuid_to_uuid128(uuid_t *uuid)
+{
+ uuid_t *uuid128 = bt_malloc(sizeof(uuid_t));
+ memset(uuid128, 0, sizeof(uuid_t));
+ switch (uuid->type) {
+ case SDP_UUID128:
+ *uuid128 = *uuid;
+ break;
+ case SDP_UUID32:
+ sdp_uuid32_to_uuid128(uuid128, uuid);
+ break;
+ case SDP_UUID16:
+ sdp_uuid16_to_uuid128(uuid128, uuid);
+ break;
+ }
+ return uuid128;
+}
+
+/*
+ * converts a 128-bit uuid to a 16/32-bit one if possible
+ * returns true if uuid contains a 16/32-bit UUID at exit
+ */
+int sdp_uuid128_to_uuid(uuid_t *uuid)
+{
+ uint128_t *b = &bluetooth_base_uuid;
+ uint128_t *u = &uuid->value.uuid128;
+ uint32_t data;
+ int i;
+
+ if (uuid->type != SDP_UUID128)
+ return 1;
+
+ for (i = 4; i < sizeof(b->data); i++)
+ if (b->data[i] != u->data[i])
+ return 0;
+
+ memcpy(&data, u->data, 4);
+ data = htonl(data);
+ if (data <= 0xffff) {
+ uuid->type = SDP_UUID16;
+ uuid->value.uuid16 = (uint16_t) data;
+ } else {
+ uuid->type = SDP_UUID32;
+ uuid->value.uuid32 = data;
+ }
+ return 1;
+}
+
+/*
+ * convert a UUID to the 16-bit short-form
+ */
+int sdp_uuid_to_proto(uuid_t *uuid)
+{
+ uuid_t u = *uuid;
+ if (sdp_uuid128_to_uuid(&u)) {
+ switch (u.type) {
+ case SDP_UUID16:
+ return u.value.uuid16;
+ case SDP_UUID32:
+ return u.value.uuid32;
+ }
+ }
+ return 0;
+}
+
+/*
+ * This function appends data to the PDU buffer "dst" from source "src".
+ * The data length is also computed and set.
+ * Should the PDU length exceed 2^8, then sequence type is
+ * set accordingly and the data is memmove()'d.
+ */
+void sdp_append_to_buf(sdp_buf_t *dst, uint8_t *data, uint32_t len)
+{
+ uint8_t *p = dst->data;
+ uint8_t dtd = *(uint8_t *) p;
+
+ SDPDBG("Append src size: %d\n", len);
+ SDPDBG("Append dst size: %d\n", dst->data_size);
+ SDPDBG("Dst buffer size: %d\n", dst->buf_size);
+ if (dst->data_size + len > dst->buf_size) {
+ int need = SDP_PDU_CHUNK_SIZE * ((len / SDP_PDU_CHUNK_SIZE) + 1);
+ dst->data = realloc(dst->data, dst->buf_size + need);
+
+ SDPDBG("Realloc'ing : %d\n", need);
+
+ if (dst->data == NULL) {
+ SDPERR("Realloc fails \n");
+ }
+ dst->buf_size += need;
+ }
+ if (dst->data_size == 0 && dtd == 0) {
+ // create initial sequence
+ *(uint8_t *)p = SDP_SEQ8;
+ p += sizeof(uint8_t);
+ dst->data_size += sizeof(uint8_t);
+ // reserve space for sequence size
+ p += sizeof(uint8_t);
+ dst->data_size += sizeof(uint8_t);
+ }
+
+ memcpy(dst->data + dst->data_size, data, len);
+ dst->data_size += len;
+
+ dtd = *(uint8_t *)dst->data;
+ if (dst->data_size > UCHAR_MAX && dtd == SDP_SEQ8) {
+ short offset = sizeof(uint8_t) + sizeof(uint8_t);
+ memmove(dst->data + offset + 1, dst->data + offset, dst->data_size - offset);
+ p = dst->data;
+ *(uint8_t *) p = SDP_SEQ16;
+ p += sizeof(uint8_t);
+ dst->data_size += 1;
+ }
+ p = dst->data;
+ dtd = *(uint8_t *) p;
+ p += sizeof(uint8_t);
+ switch (dtd) {
+ case SDP_SEQ8:
+ *(uint8_t *) p = dst->data_size - sizeof(uint8_t) - sizeof(uint8_t);
+ break;
+ case SDP_SEQ16:
+ bt_put_unaligned(htons(dst->data_size - sizeof(uint8_t) - sizeof(uint16_t)), (uint16_t *) p);
+ break;
+ case SDP_SEQ32:
+ bt_put_unaligned(htonl(dst->data_size - sizeof(uint8_t) - sizeof(uint32_t)), (uint32_t *) p);
+ break;
+ }
+}
+
+void sdp_append_to_pdu(sdp_buf_t *pdu, sdp_data_t *d)
+{
+ uint8_t buf[256];
+ sdp_buf_t append;
+
+ memset(&append, 0, sizeof(sdp_buf_t));
+ append.data = buf;
+ append.buf_size = sizeof(buf);
+ append.data_size = 0;
+
+ sdp_set_attrid(&append, d->attrId);
+ sdp_gen_pdu(&append, d);
+ sdp_append_to_buf(pdu, append.data, append.data_size);
+}
+
+/*
+ * Registers an sdp record.
+ *
+ * It is incorrect to call this method on a record that
+ * has been already registered with the server.
+ *
+ * Returns zero on success, otherwise -1 (and sets errno).
+ */
+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)
+{
+ uint8_t *req, *rsp, *p;
+ uint32_t reqsize, rspsize;
+ sdp_pdu_hdr_t *reqhdr, *rsphdr;
+ int status;
+
+ SDPDBG("");
+
+ if (!session->local) {
+ errno = EREMOTE;
+ return -1;
+ }
+ req = malloc(SDP_REQ_BUFFER_SIZE);
+ rsp = malloc(SDP_RSP_BUFFER_SIZE);
+ if (req == NULL || rsp == NULL) {
+ status = -1;
+ errno = ENOMEM;
+ goto end;
+ }
+
+ reqhdr = (sdp_pdu_hdr_t *)req;
+ reqhdr->pdu_id = SDP_SVC_REGISTER_REQ;
+ reqhdr->tid = htons(sdp_gen_tid(session));
+ reqsize = sizeof(sdp_pdu_hdr_t) + 1;
+ p = req + sizeof(sdp_pdu_hdr_t);
+
+ if (bacmp(device, BDADDR_ANY)) {
+ *p++ = flags | SDP_DEVICE_RECORD;
+ bacpy((bdaddr_t *) p, device);
+ p += sizeof(bdaddr_t);
+ reqsize += sizeof(bdaddr_t);
+ } else
+ *p++ = flags;
+
+ memcpy(p, data, size);
+ reqsize += size;
+ reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
+
+ status = sdp_send_req_w4_rsp(session, req, rsp, reqsize, &rspsize);
+ if (status < 0)
+ goto end;
+
+ if (rspsize < sizeof(sdp_pdu_hdr_t)) {
+ SDPERR("Unexpected end of packet");
+ errno = EPROTO;
+ status = -1;
+ goto end;
+ }
+
+ rsphdr = (sdp_pdu_hdr_t *) rsp;
+ p = rsp + sizeof(sdp_pdu_hdr_t);
+
+ if (rsphdr->pdu_id == SDP_ERROR_RSP) {
+ /* Invalid service record */
+ errno = EINVAL;
+ status = -1;
+ } else if (rsphdr->pdu_id != SDP_SVC_REGISTER_RSP) {
+ errno = EPROTO;
+ status = -1;
+ } else {
+ if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint32_t)) {
+ SDPERR("Unexpected end of packet");
+ errno = EPROTO;
+ status = -1;
+ goto end;
+ }
+ if (handle)
+ *handle = ntohl(bt_get_unaligned((uint32_t *) p));
+ }
+
+end:
+ if (req)
+ free(req);
+
+ if (rsp)
+ free(rsp);
+
+ return status;
+}
+
+int sdp_device_record_register(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec, uint8_t flags)
+{
+ sdp_buf_t pdu;
+ uint32_t handle;
+ int err;
+
+ SDPDBG("");
+
+ if (rec->handle && rec->handle != 0xffffffff) {
+ uint32_t handle = rec->handle;
+ sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle);
+ sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data);
+ }
+
+ if (sdp_gen_record_pdu(rec, &pdu) < 0) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ err = sdp_device_record_register_binary(session, device,
+ pdu.data, pdu.data_size, flags, &handle);
+
+ free(pdu.data);
+
+ if (err == 0) {
+ sdp_data_t *data = sdp_data_alloc(SDP_UINT32, &handle);
+ rec->handle = handle;
+ sdp_attr_replace(rec, SDP_ATTR_RECORD_HANDLE, data);
+ }
+
+ return err;
+}
+
+int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags)
+{
+ return sdp_device_record_register(session, BDADDR_ANY, rec, flags);
+}
+
+/*
+ * unregister a service record
+ */
+int sdp_device_record_unregister_binary(sdp_session_t *session, bdaddr_t *device, uint32_t handle)
+{
+ uint8_t *reqbuf, *rspbuf, *p;
+ uint32_t reqsize = 0, rspsize = 0;
+ sdp_pdu_hdr_t *reqhdr, *rsphdr;
+ int status;
+
+ SDPDBG("");
+
+ if (handle == SDP_SERVER_RECORD_HANDLE) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (!session->local) {
+ errno = EREMOTE;
+ return -1;
+ }
+
+ reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
+ rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
+ if (!reqbuf || !rspbuf) {
+ errno = ENOMEM;
+ status = -1;
+ goto end;
+ }
+ reqhdr = (sdp_pdu_hdr_t *) reqbuf;
+ reqhdr->pdu_id = SDP_SVC_REMOVE_REQ;
+ reqhdr->tid = htons(sdp_gen_tid(session));
+
+ p = reqbuf + sizeof(sdp_pdu_hdr_t);
+ reqsize = sizeof(sdp_pdu_hdr_t);
+ bt_put_unaligned(htonl(handle), (uint32_t *) p);
+ reqsize += sizeof(uint32_t);
+
+ reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
+ status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
+ if (status < 0)
+ goto end;
+
+ if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint16_t)) {
+ SDPERR("Unexpected end of packet");
+ errno = EPROTO;
+ status = -1;
+ goto end;
+ }
+
+ rsphdr = (sdp_pdu_hdr_t *) rspbuf;
+ p = rspbuf + sizeof(sdp_pdu_hdr_t);
+ status = bt_get_unaligned((uint16_t *) p);
+
+ if (rsphdr->pdu_id == SDP_ERROR_RSP) {
+ /* For this case the status always is invalid record handle */
+ errno = EINVAL;
+ status = -1;
+ } else if (rsphdr->pdu_id != SDP_SVC_REMOVE_RSP) {
+ errno = EPROTO;
+ status = -1;
+ }
+end:
+ if (reqbuf)
+ free(reqbuf);
+
+ if (rspbuf)
+ free(rspbuf);
+
+ return status;
+}
+
+int sdp_device_record_unregister(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec)
+{
+ int err;
+
+ err = sdp_device_record_unregister_binary(session, device, rec->handle);
+ if (err == 0)
+ sdp_record_free(rec);
+
+ return err;
+}
+
+int sdp_record_unregister(sdp_session_t *session, sdp_record_t *rec)
+{
+ return sdp_device_record_unregister(session, BDADDR_ANY, rec);
+}
+
+/*
+ * modify an existing service record
+ */
+int sdp_device_record_update_binary(sdp_session_t *session, bdaddr_t *device, uint32_t handle, uint8_t *data, uint32_t size)
+{
+ return -1;
+}
+
+int sdp_device_record_update(sdp_session_t *session, bdaddr_t *device, const sdp_record_t *rec)
+{
+ uint8_t *reqbuf, *rspbuf, *p;
+ uint32_t reqsize, rspsize;
+ sdp_pdu_hdr_t *reqhdr, *rsphdr;
+ uint32_t handle;
+ sdp_buf_t pdu;
+ int status;
+
+ SDPDBG("");
+
+ handle = rec->handle;
+
+ if (handle == SDP_SERVER_RECORD_HANDLE) {
+ errno = EINVAL;
+ return -1;
+ }
+ if (!session->local) {
+ errno = EREMOTE;
+ return -1;
+ }
+ reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
+ rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
+ if (!reqbuf || !rspbuf) {
+ errno = ENOMEM;
+ status = -1;
+ goto end;
+ }
+ reqhdr = (sdp_pdu_hdr_t *) reqbuf;
+ reqhdr->pdu_id = SDP_SVC_UPDATE_REQ;
+ reqhdr->tid = htons(sdp_gen_tid(session));
+
+ p = reqbuf + sizeof(sdp_pdu_hdr_t);
+ reqsize = sizeof(sdp_pdu_hdr_t);
+
+ bt_put_unaligned(htonl(handle), (uint32_t *) p);
+ reqsize += sizeof(uint32_t);
+ p += sizeof(uint32_t);
+
+ if (sdp_gen_record_pdu(rec, &pdu) < 0) {
+ errno = ENOMEM;
+ status = -1;
+ goto end;
+ }
+ memcpy(p, pdu.data, pdu.data_size);
+ reqsize += pdu.data_size;
+ free(pdu.data);
+
+ reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
+ status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
+ if (status < 0)
+ goto end;
+
+ if (rspsize < sizeof(sdp_pdu_hdr_t) + sizeof(uint16_t)) {
+ SDPERR("Unexpected end of packet");
+ errno = EPROTO;
+ status = -1;
+ goto end;
+ }
+
+ SDPDBG("Send req status : %d\n", status);
+
+ rsphdr = (sdp_pdu_hdr_t *) rspbuf;
+ p = rspbuf + sizeof(sdp_pdu_hdr_t);
+ status = bt_get_unaligned((uint16_t *) p);
+
+ if (rsphdr->pdu_id == SDP_ERROR_RSP) {
+ /* The status can be invalid sintax or invalid record handle */
+ errno = EINVAL;
+ status = -1;
+ } else if (rsphdr->pdu_id != SDP_SVC_UPDATE_RSP) {
+ errno = EPROTO;
+ status = -1;
+ }
+end:
+ if (reqbuf)
+ free(reqbuf);
+ if (rspbuf)
+ free(rspbuf);
+ return status;
+}
+
+int sdp_record_update(sdp_session_t *session, const sdp_record_t *rec)
+{
+ return sdp_device_record_update(session, BDADDR_ANY, rec);
+}
+
+sdp_record_t *sdp_record_alloc()
+{
+ sdp_record_t *rec = malloc(sizeof(sdp_record_t));
+ memset((void *)rec, 0, sizeof(sdp_record_t));
+ rec->handle = 0xffffffff;
+ return rec;
+}
+
+/*
+ * Free the contents of a service record
+ */
+void sdp_record_free(sdp_record_t *rec)
+{
+ sdp_list_free(rec->attrlist, (sdp_free_func_t)sdp_data_free);
+ sdp_list_free(rec->pattern, free);
+ free(rec);
+}
+
+void sdp_pattern_add_uuid(sdp_record_t *rec, uuid_t *uuid)
+{
+ uuid_t *uuid128 = sdp_uuid_to_uuid128(uuid);
+
+ SDPDBG("SvcRec : 0x%lx\n", (unsigned long)rec);
+ SDPDBG("Elements in target pattern : %d\n", sdp_list_len(rec->pattern));
+ SDPDBG("Trying to add : 0x%lx\n", (unsigned long)uuid128);
+
+ if (sdp_list_find(rec->pattern, uuid128, sdp_uuid128_cmp) == NULL)
+ rec->pattern = sdp_list_insert_sorted(rec->pattern, uuid128, sdp_uuid128_cmp);
+ else
+ bt_free(uuid128);
+
+ SDPDBG("Elements in target pattern : %d\n", sdp_list_len(rec->pattern));
+}
+
+void sdp_pattern_add_uuidseq(sdp_record_t *rec, sdp_list_t *seq)
+{
+ for (; seq; seq = seq->next) {
+ uuid_t *uuid = (uuid_t *)seq->data;
+ sdp_pattern_add_uuid(rec, uuid);
+ }
+}
+
+/*
+ * Extract a sequence of service record handles from a PDU buffer
+ * and add the entries to a sdp_list_t. Note that the service record
+ * handles are not in "data element sequence" form, but just like
+ * an array of service handles
+ */
+static void extract_record_handle_seq(uint8_t *pdu, int bufsize, sdp_list_t **seq, int count, int *scanned)
+{
+ sdp_list_t *pSeq = *seq;
+ uint8_t *pdata = pdu;
+ int n;
+
+ for (n = 0; n < count; n++) {
+ if (bufsize < sizeof(uint32_t)) {
+ SDPERR("Unexpected end of packet");
+ break;
+ }
+ uint32_t *pSvcRec = malloc(sizeof(uint32_t));
+ *pSvcRec = ntohl(bt_get_unaligned((uint32_t *) pdata));
+ pSeq = sdp_list_append(pSeq, pSvcRec);
+ pdata += sizeof(uint32_t);
+ *scanned += sizeof(uint32_t);
+ bufsize -= sizeof(uint32_t);
+ }
+ *seq = pSeq;
+}
+/*
+ * Generate the attribute sequence pdu form
+ * from sdp_list_t elements. Return length of attr seq
+ */
+static int gen_dataseq_pdu(uint8_t *dst, const sdp_list_t *seq, uint8_t dtd)
+{
+ sdp_data_t *dataseq;
+ void **types, **values;
+ sdp_buf_t buf;
+ int i, seqlen = sdp_list_len(seq);
+
+ // Fill up the value and the dtd arrays
+ SDPDBG("");
+
+ memset(&buf, 0, sizeof(sdp_buf_t));
+ buf.data = malloc(256);
+ buf.buf_size = 256;
+
+ if (!buf.data)
+ return -ENOMEM;
+
+ SDPDBG("Seq length : %d\n", seqlen);
+
+ types = malloc(seqlen * sizeof(void *));
+ values = malloc(seqlen * sizeof(void *));
+ for (i = 0; i < seqlen; i++) {
+ void *data = seq->data;
+ types[i] = &dtd;
+ if (SDP_IS_UUID(dtd))
+ data = &((uuid_t *)data)->value;
+ values[i] = data;
+ seq = seq->next;
+ }
+
+ dataseq = sdp_seq_alloc(types, values, seqlen);
+ SDPDBG("Data Seq : 0x%p\n", seq);
+ seqlen = sdp_gen_pdu(&buf, dataseq);
+ SDPDBG("Copying : %d\n", buf.data_size);
+ memcpy(dst, buf.data, buf.data_size);
+
+ sdp_data_free(dataseq);
+
+ free(types);
+ free(values);
+ free(buf.data);
+ return seqlen;
+}
+
+static int gen_searchseq_pdu(uint8_t *dst, const sdp_list_t *seq)
+{
+ uuid_t *uuid = (uuid_t *) seq->data;
+ return gen_dataseq_pdu(dst, seq, uuid->type);
+}
+
+static int gen_attridseq_pdu(uint8_t *dst, const sdp_list_t *seq, uint8_t dataType)
+{
+ return gen_dataseq_pdu(dst, seq, dataType);
+}
+
+typedef struct {
+ uint8_t length;
+ unsigned char data[16];
+} __attribute__ ((packed)) sdp_cstate_t;
+
+static int copy_cstate(uint8_t *pdata, int pdata_len, const sdp_cstate_t *cstate)
+{
+ if (cstate) {
+ uint8_t len = cstate->length;
+ if (len >= pdata_len) {
+ SDPERR("Continuation state size exceeds internal buffer");
+ len = pdata_len - 1;
+ }
+ *pdata++ = len;
+ memcpy(pdata, cstate->data, len);
+ return len + 1;
+ }
+ *pdata = 0;
+ return 1;
+}
+
+/*
+ * This is a service search request.
+ *
+ * INPUT :
+ *
+ * sdp_list_t *search
+ * Singly linked list containing elements of the search
+ * pattern. Each entry in the list is a UUID (DataTypeSDP_UUID16)
+ * of the service to be searched
+ *
+ * uint16_t max_rec_num
+ * A 16 bit integer which tells the service, the maximum
+ * entries that the client can handle in the response. The
+ * server is obliged not to return > max_rec_num entries
+ *
+ * OUTPUT :
+ *
+ * int return value
+ * 0:
+ * The request completed successfully. This does not
+ * mean the requested services were found
+ * -1:
+ * On any failure and sets errno
+ *
+ * sdp_list_t **rsp_list
+ * This variable is set on a successful return if there are
+ * non-zero service handles. It is a singly linked list of
+ * service record handles (uint16_t)
+ */
+int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search,
+ uint16_t max_rec_num, sdp_list_t **rsp)
+{
+ int status = 0;
+ uint32_t reqsize = 0, _reqsize;
+ uint32_t rspsize = 0, rsplen;
+ int seqlen = 0;
+ int scanned, total_rec_count, rec_count, pdata_len;
+ uint8_t *pdata, *_pdata;
+ uint8_t *reqbuf, *rspbuf;
+ sdp_pdu_hdr_t *reqhdr, *rsphdr;
+ sdp_cstate_t *cstate = NULL;
+
+ reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
+ rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
+ if (!reqbuf || !rspbuf) {
+ errno = ENOMEM;
+ status = -1;
+ goto end;
+ }
+ reqhdr = (sdp_pdu_hdr_t *) reqbuf;
+ reqhdr->pdu_id = SDP_SVC_SEARCH_REQ;
+ pdata = reqbuf + sizeof(sdp_pdu_hdr_t);
+ reqsize = sizeof(sdp_pdu_hdr_t);
+
+ // add service class IDs for search
+ seqlen = gen_searchseq_pdu(pdata, search);
+
+ SDPDBG("Data seq added : %d\n", seqlen);
+
+ // set the length and increment the pointer
+ reqsize += seqlen;
+ pdata += seqlen;
+
+ // specify the maximum svc rec count that client expects
+ bt_put_unaligned(htons(max_rec_num), (uint16_t *) pdata);
+ reqsize += sizeof(uint16_t);
+ pdata += sizeof(uint16_t);
+
+ _reqsize = reqsize;
+ _pdata = pdata;
+ *rsp = NULL;
+
+ do {
+ // Add continuation state or NULL (first time)
+ reqsize = _reqsize + copy_cstate(_pdata,
+ SDP_REQ_BUFFER_SIZE - _reqsize, cstate);
+
+ // Set the request header's param length
+ reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
+
+ reqhdr->tid = htons(sdp_gen_tid(session));
+ /*
+ * Send the request, wait for response and if
+ * no error, set the appropriate values and return
+ */
+ status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
+ if (status < 0)
+ goto end;
+
+ if (rspsize < sizeof(sdp_pdu_hdr_t)) {
+ SDPERR("Unexpected end of packet");
+ status = -1;
+ goto end;
+ }
+
+ rsphdr = (sdp_pdu_hdr_t *) rspbuf;
+ rsplen = ntohs(rsphdr->plen);
+
+ if (rsphdr->pdu_id == SDP_ERROR_RSP) {
+ SDPDBG("Status : 0x%x\n", rsphdr->pdu_id);
+ status = -1;
+ goto end;
+ }
+ scanned = 0;
+ pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
+ pdata_len = rspsize - sizeof(sdp_pdu_hdr_t);
+
+ if (pdata_len < sizeof(uint16_t) + sizeof(uint16_t)) {
+ SDPERR("Unexpected end of packet");
+ status = -1;
+ goto end;
+ }
+
+ // net service record match count
+ total_rec_count = ntohs(bt_get_unaligned((uint16_t *) pdata));
+ pdata += sizeof(uint16_t);
+ scanned += sizeof(uint16_t);
+ pdata_len -= sizeof(uint16_t);
+ rec_count = ntohs(bt_get_unaligned((uint16_t *) pdata));
+ pdata += sizeof(uint16_t);
+ scanned += sizeof(uint16_t);
+ pdata_len -= sizeof(uint16_t);
+
+ SDPDBG("Total svc count: %d\n", total_rec_count);
+ SDPDBG("Current svc count: %d\n", rec_count);
+ SDPDBG("ResponseLength: %d\n", rsplen);
+
+ if (!rec_count) {
+ status = -1;
+ goto end;
+ }
+ extract_record_handle_seq(pdata, pdata_len, rsp, rec_count, &scanned);
+ SDPDBG("BytesScanned : %d\n", scanned);
+
+ if (rsplen > scanned) {
+ uint8_t cstate_len;
+
+ if (rspsize < sizeof(sdp_pdu_hdr_t) + scanned + sizeof(uint8_t)) {
+ SDPERR("Unexpected end of packet: continuation state data missing");
+ status = -1;
+ goto end;
+ }
+
+ pdata = rspbuf + sizeof(sdp_pdu_hdr_t) + scanned;
+ cstate_len = *(uint8_t *) pdata;
+ if (cstate_len > 0) {
+ cstate = (sdp_cstate_t *)pdata;
+ SDPDBG("Cont state length: %d\n", cstate_len);
+ } else
+ cstate = NULL;
+ }
+ } while (cstate);
+
+end:
+ if (reqbuf)
+ free(reqbuf);
+ if (rspbuf)
+ free(rspbuf);
+
+ return status;
+}
+
+/*
+ * This is a service attribute request.
+ *
+ * INPUT :
+ *
+ * uint32_t handle
+ * The handle of the service for which the attribute(s) are
+ * requested
+ *
+ * sdp_attrreq_type_t reqtype
+ * Attribute identifiers are 16 bit unsigned integers specified
+ * in one of 2 ways described below :
+ * SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
+ * They are the actual attribute identifiers in ascending order
+ *
+ * SDP_ATTR_REQ_RANGE - 32bit identifier range
+ * The high-order 16bits is the start of range
+ * the low-order 16bits are the end of range
+ * 0x0000 to 0xFFFF gets all attributes
+ *
+ * sdp_list_t *attrid
+ * Singly linked list containing attribute identifiers desired.
+ * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
+ * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
+ *
+ * OUTPUT :
+ * return sdp_record_t *
+ * 0:
+ * On any error and sets errno
+ * !0:
+ * The service record
+ */
+sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle,
+ sdp_attrreq_type_t reqtype, const sdp_list_t *attrids)
+{
+ int status = 0;
+ uint32_t reqsize = 0, _reqsize;
+ uint32_t rspsize = 0, rsp_count;
+ int attr_list_len = 0;
+ int seqlen = 0, pdata_len;
+ uint8_t *pdata, *_pdata;
+ uint8_t *reqbuf, *rspbuf;
+ sdp_pdu_hdr_t *reqhdr, *rsphdr;
+ sdp_cstate_t *cstate = NULL;
+ uint8_t cstate_len = 0;
+ sdp_buf_t rsp_concat_buf;
+ sdp_record_t *rec = 0;
+
+ if (reqtype != SDP_ATTR_REQ_INDIVIDUAL && reqtype != SDP_ATTR_REQ_RANGE) {
+ errno = EINVAL;
+ return 0;
+ }
+
+ reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
+ rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
+ if (!reqbuf || !rspbuf) {
+ errno = ENOMEM;
+ status = -1;
+ goto end;
+ }
+ memset((char *) &rsp_concat_buf, 0, sizeof(sdp_buf_t));
+ reqhdr = (sdp_pdu_hdr_t *) reqbuf;
+ reqhdr->pdu_id = SDP_SVC_ATTR_REQ;
+
+ pdata = reqbuf + sizeof(sdp_pdu_hdr_t);
+ reqsize = sizeof(sdp_pdu_hdr_t);
+
+ // add the service record handle
+ bt_put_unaligned(htonl(handle), (uint32_t *) pdata);
+ reqsize += sizeof(uint32_t);
+ pdata += sizeof(uint32_t);
+
+ // specify the response limit
+ bt_put_unaligned(htons(65535), (uint16_t *) pdata);
+ reqsize += sizeof(uint16_t);
+ pdata += sizeof(uint16_t);
+
+ // get attr seq PDU form
+ seqlen = gen_attridseq_pdu(pdata, attrids,
+ reqtype == SDP_ATTR_REQ_INDIVIDUAL? SDP_UINT16 : SDP_UINT32);
+ if (seqlen == -1) {
+ errno = EINVAL;
+ status = -1;
+ goto end;
+ }
+ pdata += seqlen;
+ reqsize += seqlen;
+ SDPDBG("Attr list length : %d\n", seqlen);
+
+ // save before Continuation State
+ _pdata = pdata;
+ _reqsize = reqsize;
+
+ do {
+ // add NULL continuation state
+ reqsize = _reqsize + copy_cstate(_pdata,
+ SDP_REQ_BUFFER_SIZE - _reqsize, cstate);
+
+ // set the request header's param length
+ reqhdr->tid = htons(sdp_gen_tid(session));
+ reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
+
+ status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
+ if (status < 0)
+ goto end;
+
+ if (rspsize < sizeof(sdp_pdu_hdr_t)) {
+ SDPERR("Unexpected end of packet");
+ status = -1;
+ goto end;
+ }
+
+ rsp_count = 0;
+ rsphdr = (sdp_pdu_hdr_t *) rspbuf;
+ if (rsphdr->pdu_id == SDP_ERROR_RSP) {
+ SDPDBG("PDU ID : 0x%x\n", rsphdr->pdu_id);
+ status = -1;
+ goto end;
+ }
+ pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
+ pdata_len = rspsize - sizeof(sdp_pdu_hdr_t);
+
+ if (pdata_len < sizeof(uint16_t)) {
+ SDPERR("Unexpected end of packet");
+ status = -1;
+ goto end;
+ }
+
+ rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata));
+ attr_list_len += rsp_count;
+ pdata += sizeof(uint16_t);
+ pdata_len -= sizeof(uint16_t);
+
+ // if continuation state set need to re-issue request before parsing
+ if (pdata_len < rsp_count + sizeof(uint8_t)) {
+ SDPERR("Unexpected end of packet: continuation state data missing");
+ status = -1;
+ goto end;
+ }
+ cstate_len = *(uint8_t *) (pdata + rsp_count);
+
+ SDPDBG("Response id : %d\n", rsphdr->pdu_id);
+ SDPDBG("Attrlist byte count : %d\n", rsp_count);
+ SDPDBG("sdp_cstate_t length : %d\n", cstate_len);
+
+ /*
+ * a split response: concatenate intermediate responses
+ * and the last one (which has cstate_len == 0)
+ */
+ if (cstate_len > 0 || rsp_concat_buf.data_size != 0) {
+ uint8_t *targetPtr = NULL;
+
+ cstate = cstate_len > 0 ? (sdp_cstate_t *) (pdata + rsp_count) : 0;
+
+ // build concatenated response buffer
+ rsp_concat_buf.data = realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count);
+ rsp_concat_buf.buf_size = rsp_concat_buf.data_size + rsp_count;
+ targetPtr = rsp_concat_buf.data + rsp_concat_buf.data_size;
+ memcpy(targetPtr, pdata, rsp_count);
+ rsp_concat_buf.data_size += rsp_count;
+ }
+ } while (cstate);
+
+ if (attr_list_len > 0) {
+ int scanned = 0;
+ if (rsp_concat_buf.data_size != 0) {
+ pdata = rsp_concat_buf.data;
+ pdata_len = rsp_concat_buf.data_size;
+ }
+ rec = sdp_extract_pdu_safe(pdata, pdata_len, &scanned);
+
+ if (!rec)
+ status = -1;
+ }
+
+end:
+ if (reqbuf)
+ free(reqbuf);
+ if (rsp_concat_buf.data)
+ free(rsp_concat_buf.data);
+ if (rspbuf)
+ free(rspbuf);
+ return rec;
+}
+
+/*
+ * SDP transaction structure for asynchronous search
+ */
+struct sdp_transaction {
+ sdp_callback_t *cb; /* called when the transaction finishes */
+ void *udata; /* client user data */
+ uint8_t *reqbuf; /* pointer to request PDU */
+ sdp_buf_t rsp_concat_buf;
+ uint32_t reqsize; /* without cstate */
+ int err; /* ZERO if success or the errno if failed */
+};
+
+/*
+ * Creates a new sdp session for asynchronous search
+ * INPUT:
+ * int sk
+ * non-blocking L2CAP socket
+ *
+ * RETURN:
+ * sdp_session_t *
+ * NULL - On memory allocation failure
+ */
+sdp_session_t *sdp_create(int sk, uint32_t flags)
+{
+ sdp_session_t *session;
+ struct sdp_transaction *t;
+
+ session = malloc(sizeof(sdp_session_t));
+ if (!session) {
+ errno = ENOMEM;
+ return NULL;
+ }
+ memset(session, 0, sizeof(*session));
+
+ session->flags = flags;
+ session->sock = sk;
+
+ t = malloc(sizeof(struct sdp_transaction));
+ if (!t) {
+ errno = ENOMEM;
+ free(session);
+ return NULL;
+ }
+ memset(t, 0, sizeof(*t));
+
+ session->priv = t;
+
+ return session;
+}
+
+/*
+ * Sets the callback function/user data used to notify the application
+ * that the asynchronous transaction finished. This function must be
+ * called before request an asynchronous search.
+ *
+ * INPUT:
+ * sdp_session_t *session
+ * Current sdp session to be handled
+ * sdp_callback_t *cb
+ * callback to be called when the transaction finishes
+ * void *udata
+ * user data passed to callback
+ * RETURN:
+ * 0 - Success
+ * -1 - Failure
+ */
+int sdp_set_notify(sdp_session_t *session, sdp_callback_t *func, void *udata)
+{
+ struct sdp_transaction *t;
+
+ if (!session || !session->priv)
+ return -1;
+
+ t = session->priv;
+ t->cb = func;
+ t->udata = udata;
+
+ return 0;
+}
+
+/*
+ * This function starts an asynchronous service search request.
+ * The incomming and outgoing data are stored in the transaction structure
+ * buffers. When there is incomming data the sdp_process function must be
+ * called to get the data and handle the continuation state.
+ *
+ * INPUT :
+ * sdp_session_t *session
+ * Current sdp session to be handled
+ *
+ * sdp_list_t *search
+ * Singly linked list containing elements of the search
+ * pattern. Each entry in the list is a UUID (DataTypeSDP_UUID16)
+ * of the service to be searched
+ *
+ * uint16_t max_rec_num
+ * A 16 bit integer which tells the service, the maximum
+ * entries that the client can handle in the response. The
+ * server is obliged not to return > max_rec_num entries
+ *
+ * OUTPUT :
+ *
+ * int return value
+ * 0 - if the request has been sent properly
+ * -1 - On any failure and sets errno
+ */
+
+int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search, uint16_t max_rec_num)
+{
+ struct sdp_transaction *t;
+ sdp_pdu_hdr_t *reqhdr;
+ uint8_t *pdata;
+ int cstate_len, seqlen = 0;
+
+ if (!session || !session->priv)
+ return -1;
+
+ t = session->priv;
+
+ /* check if the buffer is already allocated */
+ if (t->rsp_concat_buf.data)
+ free(t->rsp_concat_buf.data);
+ memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t));
+
+ if (!t->reqbuf) {
+ t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
+ if (!t->reqbuf) {
+ t->err = ENOMEM;
+ goto end;
+ }
+ }
+ memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE);
+
+ reqhdr = (sdp_pdu_hdr_t *) t->reqbuf;
+ reqhdr->tid = htons(sdp_gen_tid(session));
+ reqhdr->pdu_id = SDP_SVC_SEARCH_REQ;
+
+ // generate PDU
+ pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t);
+ t->reqsize = sizeof(sdp_pdu_hdr_t);
+
+ // add service class IDs for search
+ seqlen = gen_searchseq_pdu(pdata, search);
+
+ SDPDBG("Data seq added : %d\n", seqlen);
+
+ // now set the length and increment the pointer
+ t->reqsize += seqlen;
+ pdata += seqlen;
+
+ bt_put_unaligned(htons(max_rec_num), (uint16_t *) pdata);
+ t->reqsize += sizeof(uint16_t);
+ pdata += sizeof(uint16_t);
+
+ // set the request header's param length
+ cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL);
+ reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t));
+
+ if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) {
+ SDPERR("Error sendind data:%s", strerror(errno));
+ t->err = errno;
+ goto end;
+ }
+
+ return 0;
+end:
+
+ if (t->reqbuf) {
+ free(t->reqbuf);
+ t->reqbuf = NULL;
+ }
+
+ return -1;
+}
+
+/*
+ * This function starts an asynchronous service attribute request.
+ * The incomming and outgoing data are stored in the transaction structure
+ * buffers. When there is incomming data the sdp_process function must be
+ * called to get the data and handle the continuation state.
+ *
+ * INPUT :
+ * sdp_session_t *session
+ * Current sdp session to be handled
+ *
+ * uint32_t handle
+ * The handle of the service for which the attribute(s) are
+ * requested
+ *
+ * sdp_attrreq_type_t reqtype
+ * Attribute identifiers are 16 bit unsigned integers specified
+ * in one of 2 ways described below :
+ * SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
+ * They are the actual attribute identifiers in ascending order
+ *
+ * SDP_ATTR_REQ_RANGE - 32bit identifier range
+ * The high-order 16bits is the start of range
+ * the low-order 16bits are the end of range
+ * 0x0000 to 0xFFFF gets all attributes
+ *
+ * sdp_list_t *attrid_list
+ * Singly linked list containing attribute identifiers desired.
+ * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
+ * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
+ *
+ * OUTPUT :
+ * int return value
+ * 0 - if the request has been sent properly
+ * -1 - On any failure and sets errno
+ */
+
+int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list)
+{
+ struct sdp_transaction *t;
+ sdp_pdu_hdr_t *reqhdr;
+ uint8_t *pdata;
+ int cstate_len, seqlen = 0;
+
+ if (!session || !session->priv)
+ return -1;
+
+ t = session->priv;
+
+ /* check if the buffer is already allocated */
+ if (t->rsp_concat_buf.data)
+ free(t->rsp_concat_buf.data);
+ memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t));
+
+ if (!t->reqbuf) {
+ t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
+ if (!t->reqbuf) {
+ t->err = ENOMEM;
+ goto end;
+ }
+ }
+ memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE);
+
+ reqhdr = (sdp_pdu_hdr_t *) t->reqbuf;
+ reqhdr->tid = htons(sdp_gen_tid(session));
+ reqhdr->pdu_id = SDP_SVC_ATTR_REQ;
+
+ // generate PDU
+ pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t);
+ t->reqsize = sizeof(sdp_pdu_hdr_t);
+
+ // add the service record handle
+ bt_put_unaligned(htonl(handle), (uint32_t *) pdata);
+ t->reqsize += sizeof(uint32_t);
+ pdata += sizeof(uint32_t);
+
+ // specify the response limit
+ bt_put_unaligned(htons(65535), (uint16_t *) pdata);
+ t->reqsize += sizeof(uint16_t);
+ pdata += sizeof(uint16_t);
+
+ // get attr seq PDU form
+ seqlen = gen_attridseq_pdu(pdata, attrid_list,
+ reqtype == SDP_ATTR_REQ_INDIVIDUAL? SDP_UINT16 : SDP_UINT32);
+ if (seqlen == -1) {
+ t->err = EINVAL;
+ goto end;
+ }
+
+ // now set the length and increment the pointer
+ t->reqsize += seqlen;
+ pdata += seqlen;
+ SDPDBG("Attr list length : %d\n", seqlen);
+
+ // set the request header's param length
+ cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL);
+ reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t));
+
+ if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) {
+ SDPERR("Error sendind data:%s", strerror(errno));
+ t->err = errno;
+ goto end;
+ }
+
+ return 0;
+end:
+
+ if (t->reqbuf) {
+ free(t->reqbuf);
+ t->reqbuf = NULL;
+ }
+
+ return -1;
+}
+
+/*
+ * This function starts an asynchronous service search attributes.
+ * It is a service search request combined with attribute request. The incomming
+ * and outgoing data are stored in the transaction structure buffers. When there
+ * is incomming data the sdp_process function must be called to get the data
+ * and handle the continuation state.
+ *
+ * INPUT:
+ * sdp_session_t *session
+ * Current sdp session to be handled
+ *
+ * sdp_list_t *search
+ * Singly linked list containing elements of the search
+ * pattern. Each entry in the list is a UUID(DataTypeSDP_UUID16)
+ * of the service to be searched
+ *
+ * AttributeSpecification attrSpec
+ * Attribute identifiers are 16 bit unsigned integers specified
+ * in one of 2 ways described below :
+ * SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
+ * They are the actual attribute identifiers in ascending order
+ *
+ * SDP_ATTR_REQ_RANGE - 32bit identifier range
+ * The high-order 16bits is the start of range
+ * the low-order 16bits are the end of range
+ * 0x0000 to 0xFFFF gets all attributes
+ *
+ * sdp_list_t *attrid_list
+ * Singly linked list containing attribute identifiers desired.
+ * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
+ * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
+ *
+
+ * RETURN:
+ * 0 - if the request has been sent properly
+ * -1 - On any failure
+ */
+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)
+{
+ struct sdp_transaction *t;
+ sdp_pdu_hdr_t *reqhdr;
+ uint8_t *pdata;
+ int cstate_len, seqlen = 0;
+
+ if (!session || !session->priv)
+ return -1;
+
+ t = session->priv;
+
+ /* check if the buffer is already allocated */
+ if (t->rsp_concat_buf.data)
+ free(t->rsp_concat_buf.data);
+ memset(&t->rsp_concat_buf, 0, sizeof(sdp_buf_t));
+
+ if (!t->reqbuf) {
+ t->reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
+ if (!t->reqbuf) {
+ t->err = ENOMEM;
+ goto end;
+ }
+ }
+ memset(t->reqbuf, 0, SDP_REQ_BUFFER_SIZE);
+
+ reqhdr = (sdp_pdu_hdr_t *) t->reqbuf;
+ reqhdr->tid = htons(sdp_gen_tid(session));
+ reqhdr->pdu_id = SDP_SVC_SEARCH_ATTR_REQ;
+
+ // generate PDU
+ pdata = t->reqbuf + sizeof(sdp_pdu_hdr_t);
+ t->reqsize = sizeof(sdp_pdu_hdr_t);
+
+ // add service class IDs for search
+ seqlen = gen_searchseq_pdu(pdata, search);
+
+ SDPDBG("Data seq added : %d\n", seqlen);
+
+ // now set the length and increment the pointer
+ t->reqsize += seqlen;
+ pdata += seqlen;
+
+ bt_put_unaligned(htons(SDP_MAX_ATTR_LEN), (uint16_t *) pdata);
+ t->reqsize += sizeof(uint16_t);
+ pdata += sizeof(uint16_t);
+
+ SDPDBG("Max attr byte count : %d\n", SDP_MAX_ATTR_LEN);
+
+ // get attr seq PDU form
+ seqlen = gen_attridseq_pdu(pdata, attrid_list,
+ reqtype == SDP_ATTR_REQ_INDIVIDUAL ? SDP_UINT16 : SDP_UINT32);
+ if (seqlen == -1) {
+ t->err = EINVAL;
+ goto end;
+ }
+
+ pdata += seqlen;
+ SDPDBG("Attr list length : %d\n", seqlen);
+ t->reqsize += seqlen;
+
+ // set the request header's param length
+ cstate_len = copy_cstate(pdata, SDP_REQ_BUFFER_SIZE - t->reqsize, NULL);
+ reqhdr->plen = htons((t->reqsize + cstate_len) - sizeof(sdp_pdu_hdr_t));
+
+ if (sdp_send_req(session, t->reqbuf, t->reqsize + cstate_len) < 0) {
+ SDPERR("Error sendind data:%s", strerror(errno));
+ t->err = errno;
+ goto end;
+ }
+
+ return 0;
+end:
+
+ if (t->reqbuf) {
+ free(t->reqbuf);
+ t->reqbuf = NULL;
+ }
+
+ return -1;
+}
+
+/*
+ * Function used to get the error reason after sdp_callback_t function has been called
+ * and the status is 0xffff or if sdp_service_{search, attr, search_attr}_async returns -1.
+ * It indicates that an error NOT related to SDP_ErrorResponse happened. Get errno directly
+ * is not safe because multiple transactions can be triggered.
+ * This function must be used with asynchronous sdp functions only.
+ *
+ * INPUT:
+ * sdp_session_t *session
+ * Current sdp session to be handled
+ * RETURN:
+ * 0 = No error in the current transaction
+ * -1 - if the session is invalid
+ * positive value - the errno value
+ *
+ */
+int sdp_get_error(sdp_session_t *session)
+{
+ struct sdp_transaction *t;
+
+ if (!session || !session->priv) {
+ SDPERR("Invalid session");
+ return -1;
+ }
+
+ t = session->priv;
+
+ return t->err;
+}
+
+/*
+ * Receive the incomming SDP PDU. This function must be called when there is data
+ * available to be read. On continuation state, the original request (with a new
+ * transaction ID) and the continuation state data will be appended in the initial PDU.
+ * If an error happens or the transaction finishes the callback function will be called.
+ *
+ * INPUT:
+ * sdp_session_t *session
+ * Current sdp session to be handled
+ * RETURN:
+ * 0 - if the transaction is on continuation state
+ * -1 - On any failure or the transaction finished
+ */
+int sdp_process(sdp_session_t *session)
+{
+ struct sdp_transaction *t;
+ sdp_pdu_hdr_t *reqhdr, *rsphdr;
+ sdp_cstate_t *pcstate;
+ uint8_t *pdata, *rspbuf, *targetPtr;
+ int rsp_count, err = -1;
+ size_t size = 0;
+ int n, plen;
+ uint16_t status = 0xffff;
+ uint8_t pdu_id = 0x00;
+
+ if (!session || !session->priv) {
+ SDPERR("Invalid session");
+ return -1;
+ }
+
+ rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
+ if (!rspbuf) {
+ SDPERR("Response buffer alloc failure:%s (%d)",
+ strerror(errno), errno);
+ return -1;
+ }
+
+ memset(rspbuf, 0, SDP_RSP_BUFFER_SIZE);
+
+ t = session->priv;
+ reqhdr = (sdp_pdu_hdr_t *)t->reqbuf;
+ rsphdr = (sdp_pdu_hdr_t *)rspbuf;
+
+ pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
+
+ n = sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE);
+ if (n < 0) {
+ SDPERR("Read response:%s (%d)", strerror(errno), errno);
+ t->err = errno;
+ goto end;
+ }
+
+ if (n == 0 || reqhdr->tid != rsphdr->tid ||
+ (n != (ntohs(rsphdr->plen) + sizeof(sdp_pdu_hdr_t)))) {
+ t->err = EPROTO;
+ SDPERR("Protocol error.");
+ goto end;
+ }
+
+ pdu_id = rsphdr->pdu_id;
+ switch (rsphdr->pdu_id) {
+ uint8_t *ssr_pdata;
+ uint16_t tsrc, csrc;
+ case SDP_SVC_SEARCH_RSP:
+ /*
+ * TSRC: Total Service Record Count (2 bytes)
+ * CSRC: Current Service Record Count (2 bytes)
+ */
+ ssr_pdata = pdata;
+ tsrc = ntohs(bt_get_unaligned((uint16_t *) ssr_pdata));
+ ssr_pdata += sizeof(uint16_t);
+ csrc = ntohs(bt_get_unaligned((uint16_t *) ssr_pdata));
+
+ /* csrc should never be larger than tsrc */
+ if (csrc > tsrc) {
+ t->err = EPROTO;
+ SDPERR("Protocol error: wrong current service record count value.");
+ goto end;
+ }
+
+ SDPDBG("Total svc count: %d\n", tsrc);
+ SDPDBG("Current svc count: %d\n", csrc);
+
+ /* parameter length without continuation state */
+ plen = sizeof(tsrc) + sizeof(csrc) + csrc * 4;
+
+ if (t->rsp_concat_buf.data_size == 0) {
+ /* first fragment */
+ rsp_count = sizeof(tsrc) + sizeof(csrc) + csrc * 4;
+ } else {
+ /* point to the first csrc */
+ uint16_t *pcsrc = (uint16_t *) (t->rsp_concat_buf.data + 2);
+
+ /* FIXME: update the interface later. csrc doesn't need be passed to clients */
+
+ pdata += sizeof(uint16_t); /* point to csrc */
+
+ /* the first csrc contains the sum of partial csrc responses */
+ *pcsrc += bt_get_unaligned((uint16_t *) pdata);
+
+ pdata += sizeof(uint16_t); /* point to the first handle */
+ rsp_count = csrc * 4;
+ }
+ status = 0x0000;
+ break;
+ case SDP_SVC_ATTR_RSP:
+ case SDP_SVC_SEARCH_ATTR_RSP:
+ rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata));
+ SDPDBG("Attrlist byte count : %d\n", rsp_count);
+
+ /*
+ * Number of bytes in the AttributeLists parameter(without
+ * continuation state) + AttributeListsByteCount field size.
+ */
+ plen = sizeof(uint16_t) + rsp_count;
+
+ pdata += sizeof(uint16_t); // points to attribute list
+ status = 0x0000;
+ break;
+ case SDP_ERROR_RSP:
+ status = ntohs(bt_get_unaligned((uint16_t *) pdata));
+ size = ntohs(rsphdr->plen);
+
+ /* error code + error info */
+ plen = size;
+ goto end;
+ default:
+ t->err = EPROTO;
+ SDPERR("Illegal PDU ID: 0x%x", rsphdr->pdu_id);
+ goto end;
+ }
+
+ pcstate = (sdp_cstate_t *) (pdata + rsp_count);
+
+ SDPDBG("Cstate length : %d\n", pcstate->length);
+
+ /*
+ * Check out of bound. Continuation state must have at least
+ * 1 byte: ZERO to indicate that it is not a partial response.
+ */
+ if ((n - sizeof(sdp_pdu_hdr_t)) != (plen + pcstate->length + 1)) {
+ t->err = EPROTO;
+ SDPERR("Protocol error: wrong PDU size.");
+ status = 0xffff;
+ goto end;
+ }
+
+ /*
+ * This is a split response, need to concatenate intermediate
+ * responses and the last one which will have cstate length == 0
+ */
+ t->rsp_concat_buf.data = realloc(t->rsp_concat_buf.data, t->rsp_concat_buf.data_size + rsp_count);
+ targetPtr = t->rsp_concat_buf.data + t->rsp_concat_buf.data_size;
+ t->rsp_concat_buf.buf_size = t->rsp_concat_buf.data_size + rsp_count;
+ memcpy(targetPtr, pdata, rsp_count);
+ t->rsp_concat_buf.data_size += rsp_count;
+
+ if (pcstate->length > 0) {
+ int reqsize, cstate_len;
+
+ reqhdr->tid = htons(sdp_gen_tid(session));
+
+ // add continuation state
+ cstate_len = copy_cstate(t->reqbuf + t->reqsize,
+ SDP_REQ_BUFFER_SIZE - t->reqsize, pcstate);
+
+ reqsize = t->reqsize + cstate_len;
+
+ // set the request header's param length
+ reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
+
+ if (sdp_send_req(session, t->reqbuf, reqsize) < 0) {
+ SDPERR("Error sendind data:%s(%d)", strerror(errno), errno);
+ status = 0xffff;
+ t->err = errno;
+ goto end;
+ }
+ err = 0;
+ }
+
+end:
+ if (err) {
+ if (t->rsp_concat_buf.data_size != 0) {
+ pdata = t->rsp_concat_buf.data;
+ size = t->rsp_concat_buf.data_size;
+ }
+ if (t->cb)
+ t->cb(pdu_id, status, pdata, size, t->udata);
+ }
+
+ if (rspbuf)
+ free(rspbuf);
+
+ return err;
+}
+
+/*
+ * This is a service search request combined with the service
+ * attribute request. First a service class match is done and
+ * for matching service, requested attributes are extracted
+ *
+ * INPUT :
+ *
+ * sdp_list_t *search
+ * Singly linked list containing elements of the search
+ * pattern. Each entry in the list is a UUID(DataTypeSDP_UUID16)
+ * of the service to be searched
+ *
+ * AttributeSpecification attrSpec
+ * Attribute identifiers are 16 bit unsigned integers specified
+ * in one of 2 ways described below :
+ * SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
+ * They are the actual attribute identifiers in ascending order
+ *
+ * SDP_ATTR_REQ_RANGE - 32bit identifier range
+ * The high-order 16bits is the start of range
+ * the low-order 16bits are the end of range
+ * 0x0000 to 0xFFFF gets all attributes
+ *
+ * sdp_list_t *attrids
+ * Singly linked list containing attribute identifiers desired.
+ * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
+ * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
+ *
+ * OUTPUT :
+ * int return value
+ * 0:
+ * The request completed successfully. This does not
+ * mean the requested services were found
+ * -1:
+ * On any error and sets errno
+ *
+ * sdp_list_t **rsp
+ * This variable is set on a successful return to point to
+ * service(s) found. Each element of this list is of type
+ * sdp_record_t* (of the services which matched the search list)
+ */
+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)
+{
+ int status = 0;
+ uint32_t reqsize = 0, _reqsize;
+ uint32_t rspsize = 0;
+ int seqlen = 0, attr_list_len = 0;
+ int rsp_count = 0, cstate_len = 0, pdata_len;
+ uint8_t *pdata, *_pdata;
+ uint8_t *reqbuf, *rspbuf;
+ sdp_pdu_hdr_t *reqhdr, *rsphdr;
+ uint8_t dataType;
+ sdp_list_t *rec_list = NULL;
+ sdp_buf_t rsp_concat_buf;
+ sdp_cstate_t *cstate = NULL;
+
+ if (reqtype != SDP_ATTR_REQ_INDIVIDUAL && reqtype != SDP_ATTR_REQ_RANGE) {
+ errno = EINVAL;
+ return -1;
+ }
+ reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
+ rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
+ if (!reqbuf || !rspbuf) {
+ errno = ENOMEM;
+ status = -1;
+ goto end;
+ }
+
+ memset((char *)&rsp_concat_buf, 0, sizeof(sdp_buf_t));
+ reqhdr = (sdp_pdu_hdr_t *) reqbuf;
+ reqhdr->pdu_id = SDP_SVC_SEARCH_ATTR_REQ;
+
+ // generate PDU
+ pdata = reqbuf + sizeof(sdp_pdu_hdr_t);
+ reqsize = sizeof(sdp_pdu_hdr_t);
+
+ // add service class IDs for search
+ seqlen = gen_searchseq_pdu(pdata, search);
+
+ SDPDBG("Data seq added : %d\n", seqlen);
+
+ // now set the length and increment the pointer
+ reqsize += seqlen;
+ pdata += seqlen;
+
+ bt_put_unaligned(htons(SDP_MAX_ATTR_LEN), (uint16_t *) pdata);
+ reqsize += sizeof(uint16_t);
+ pdata += sizeof(uint16_t);
+
+ SDPDBG("Max attr byte count : %d\n", SDP_MAX_ATTR_LEN);
+
+ // get attr seq PDU form
+ seqlen = gen_attridseq_pdu(pdata, attrids,
+ reqtype == SDP_ATTR_REQ_INDIVIDUAL ? SDP_UINT16 : SDP_UINT32);
+ if (seqlen == -1) {
+ status = EINVAL;
+ goto end;
+ }
+ pdata += seqlen;
+ SDPDBG("Attr list length : %d\n", seqlen);
+ reqsize += seqlen;
+ *rsp = 0;
+
+ // save before Continuation State
+ _pdata = pdata;
+ _reqsize = reqsize;
+
+ do {
+ reqhdr->tid = htons(sdp_gen_tid(session));
+
+ // add continuation state (can be null)
+ reqsize = _reqsize + copy_cstate(_pdata,
+ SDP_REQ_BUFFER_SIZE - _reqsize, cstate);
+
+ // set the request header's param length
+ reqhdr->plen = htons(reqsize - sizeof(sdp_pdu_hdr_t));
+ rsphdr = (sdp_pdu_hdr_t *) rspbuf;
+ status = sdp_send_req_w4_rsp(session, reqbuf, rspbuf, reqsize, &rspsize);
+ if (rspsize < sizeof(sdp_pdu_hdr_t)) {
+ SDPERR("Unexpected end of packet");
+ status = -1;
+ goto end;
+ }
+
+ if (status < 0) {
+ SDPDBG("Status : 0x%x\n", rsphdr->pdu_id);
+ goto end;
+ }
+
+ if (rsphdr->pdu_id == SDP_ERROR_RSP) {
+ status = -1;
+ goto end;
+ }
+
+ pdata = rspbuf + sizeof(sdp_pdu_hdr_t);
+ pdata_len = rspsize - sizeof(sdp_pdu_hdr_t);
+
+ if (pdata_len < sizeof(uint16_t)) {
+ SDPERR("Unexpected end of packet");
+ status = -1;
+ goto end;
+ }
+
+ rsp_count = ntohs(bt_get_unaligned((uint16_t *) pdata));
+ attr_list_len += rsp_count;
+ pdata += sizeof(uint16_t); // pdata points to attribute list
+ pdata_len -= sizeof(uint16_t);
+
+ if (pdata_len < rsp_count + sizeof(uint8_t)) {
+ SDPERR("Unexpected end of packet: continuation state data missing");
+ status = -1;
+ goto end;
+ }
+
+ cstate_len = *(uint8_t *) (pdata + rsp_count);
+
+ SDPDBG("Attrlist byte count : %d\n", attr_list_len);
+ SDPDBG("Response byte count : %d\n", rsp_count);
+ SDPDBG("Cstate length : %d\n", cstate_len);
+ /*
+ * This is a split response, need to concatenate intermediate
+ * responses and the last one which will have cstate_len == 0
+ */
+ if (cstate_len > 0 || rsp_concat_buf.data_size != 0) {
+ uint8_t *targetPtr = NULL;
+
+ cstate = cstate_len > 0 ? (sdp_cstate_t *) (pdata + rsp_count) : 0;
+
+ // build concatenated response buffer
+ rsp_concat_buf.data = realloc(rsp_concat_buf.data, rsp_concat_buf.data_size + rsp_count);
+ targetPtr = rsp_concat_buf.data + rsp_concat_buf.data_size;
+ rsp_concat_buf.buf_size = rsp_concat_buf.data_size + rsp_count;
+ memcpy(targetPtr, pdata, rsp_count);
+ rsp_concat_buf.data_size += rsp_count;
+ }
+ } while (cstate);
+
+ if (attr_list_len > 0) {
+ int scanned = 0;
+
+ if (rsp_concat_buf.data_size != 0) {
+ pdata = rsp_concat_buf.data;
+ pdata_len = rsp_concat_buf.data_size;
+ }
+
+ /*
+ * Response is a sequence of sequence(s) for one or
+ * more data element sequence(s) representing services
+ * for which attributes are returned
+ */
+ scanned = sdp_extract_seqtype_safe(pdata, pdata_len, &dataType, &seqlen);
+
+ SDPDBG("Bytes scanned : %d\n", scanned);
+ SDPDBG("Seq length : %d\n", seqlen);
+
+ if (scanned && seqlen) {
+ pdata += scanned;
+ pdata_len -= scanned;
+ do {
+ int recsize = 0;
+ sdp_record_t *rec = sdp_extract_pdu_safe(pdata, pdata_len, &recsize);
+ if (rec == NULL) {
+ SDPERR("SVC REC is null\n");
+ status = -1;
+ goto end;
+ }
+ if (!recsize) {
+ sdp_record_free(rec);
+ break;
+ }
+ scanned += recsize;
+ pdata += recsize;
+ pdata_len -= recsize;
+
+ SDPDBG("Loc seq length : %d\n", recsize);
+ SDPDBG("Svc Rec Handle : 0x%x\n", rec->handle);
+ SDPDBG("Bytes scanned : %d\n", scanned);
+ SDPDBG("Attrlist byte count : %d\n", attr_list_len);
+ rec_list = sdp_list_append(rec_list, rec);
+ } while (scanned < attr_list_len && pdata_len > 0);
+
+ SDPDBG("Successful scan of service attr lists\n");
+ *rsp = rec_list;
+ }
+ }
+ end:
+ if (rsp_concat_buf.data)
+ free(rsp_concat_buf.data);
+ if (reqbuf)
+ free(reqbuf);
+ if (rspbuf)
+ free(rspbuf);
+ return status;
+}
+
+/*
+ * Find devices in the piconet.
+ */
+int sdp_general_inquiry(inquiry_info *ii, int num_dev, int duration, uint8_t *found)
+{
+ int n = hci_inquiry(-1, 10, num_dev, NULL, &ii, 0);
+ if (n < 0) {
+ SDPERR("Inquiry failed:%s", strerror(errno));
+ return -1;
+ }
+ *found = n;
+ return 0;
+}
+
+int sdp_close(sdp_session_t *session)
+{
+ struct sdp_transaction *t;
+ int ret;
+
+ if (!session)
+ return -1;
+
+ ret = close(session->sock);
+
+ t = session->priv;
+
+ if (t) {
+ if (t->reqbuf)
+ free(t->reqbuf);
+
+ if (t->rsp_concat_buf.data)
+ free(t->rsp_concat_buf.data);
+
+ free(t);
+ }
+ free(session);
+ return ret;
+}
+
+static inline int sdp_is_local(const bdaddr_t *device)
+{
+ return memcmp(device, BDADDR_LOCAL, sizeof(bdaddr_t)) == 0;
+}
+
+static int sdp_connect_local(sdp_session_t *session)
+{
+ struct sockaddr_un sa;
+
+ session->sock = socket(PF_UNIX, SOCK_STREAM, 0);
+ if (session->sock < 0)
+ return -1;
+ session->local = 1;
+
+ sa.sun_family = AF_UNIX;
+ strcpy(sa.sun_path, SDP_UNIX_PATH);
+
+ return connect(session->sock, (struct sockaddr *)&sa, sizeof(sa));
+}
+
+static int sdp_connect_l2cap(const bdaddr_t *src,
+ const bdaddr_t *dst, sdp_session_t *session)
+{
+ uint32_t flags = session->flags;
+ struct sockaddr_l2 sa;
+ int sk;
+
+ session->sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
+ if (session->sock < 0)
+ return -1;
+ session->local = 0;
+
+ sk = session->sock;
+
+ if (flags & SDP_NON_BLOCKING) {
+ long arg = fcntl(sk, F_GETFL, 0);
+ fcntl(sk, F_SETFL, arg | O_NONBLOCK);
+ }
+
+ sa.l2_family = AF_BLUETOOTH;
+ sa.l2_psm = 0;
+
+ if (bacmp(src, BDADDR_ANY)) {
+ sa.l2_bdaddr = *src;
+ if (bind(sk, (struct sockaddr *) &sa, sizeof(sa)) < 0)
+ return -1;
+ }
+
+ if (flags & SDP_WAIT_ON_CLOSE) {
+ struct linger l = { .l_onoff = 1, .l_linger = 1 };
+ setsockopt(sk, SOL_SOCKET, SO_LINGER, &l, sizeof(l));
+ }
+
+ sa.l2_psm = htobs(SDP_PSM);
+ sa.l2_bdaddr = *dst;
+
+ do {
+ int ret = connect(sk, (struct sockaddr *) &sa, sizeof(sa));
+ if (!ret)
+ return 0;
+ if (ret < 0 && (flags & SDP_NON_BLOCKING) &&
+ (errno == EAGAIN || errno == EINPROGRESS))
+ return 0;
+ } while (errno == EBUSY && (flags & SDP_RETRY_IF_BUSY));
+
+ return -1;
+}
+
+sdp_session_t *sdp_connect(const bdaddr_t *src,
+ const bdaddr_t *dst, uint32_t flags)
+{
+ sdp_session_t *session;
+ int err;
+
+ if ((flags & SDP_RETRY_IF_BUSY) && (flags & SDP_NON_BLOCKING)) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ session = sdp_create(-1, flags);
+ if (!session)
+ return NULL;
+
+ if (sdp_is_local(dst)) {
+ if (sdp_connect_local(session) < 0)
+ goto fail;
+ } else {
+ if (sdp_connect_l2cap(src, dst, session) < 0)
+ goto fail;
+ }
+
+ return session;
+
+fail:
+ err = errno;
+ if (session->sock >= 0)
+ close(session->sock);
+ if (session->priv)
+ free(session->priv);
+ free(session);
+ errno = err;
+
+ return NULL;
+}
+
+int sdp_get_socket(const sdp_session_t *session)
+{
+ return session->sock;
+}
+
+uint16_t sdp_gen_tid(sdp_session_t *session)
+{
+ return session->tid++;
+}