aboutsummaryrefslogtreecommitdiff
path: root/drive-sdk/deps/bzle/src
diff options
context:
space:
mode:
Diffstat (limited to 'drive-sdk/deps/bzle/src')
-rw-r--r--drive-sdk/deps/bzle/src/CMakeLists.txt23
-rw-r--r--drive-sdk/deps/bzle/src/att.c1165
-rw-r--r--drive-sdk/deps/bzle/src/bluetooth.c867
-rw-r--r--drive-sdk/deps/bzle/src/btio.c1186
-rw-r--r--drive-sdk/deps/bzle/src/gatt.c929
-rw-r--r--drive-sdk/deps/bzle/src/gattrib.c763
-rw-r--r--drive-sdk/deps/bzle/src/hci.c2929
-rw-r--r--drive-sdk/deps/bzle/src/log.c146
-rw-r--r--drive-sdk/deps/bzle/src/log.h59
-rw-r--r--drive-sdk/deps/bzle/src/sdp.c4940
-rw-r--r--drive-sdk/deps/bzle/src/utils.c120
-rw-r--r--drive-sdk/deps/bzle/src/uuid.c278
12 files changed, 13405 insertions, 0 deletions
diff --git a/drive-sdk/deps/bzle/src/CMakeLists.txt b/drive-sdk/deps/bzle/src/CMakeLists.txt
new file mode 100644
index 0000000..a6f101a
--- /dev/null
+++ b/drive-sdk/deps/bzle/src/CMakeLists.txt
@@ -0,0 +1,23 @@
+# Include the directory itself as a path to include directories
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+
+include(FindGLIB2)
+
+include_directories(
+ ${bzle_SOURCE_DIR}/include
+ ${GLIB2_INCLUDE_DIRS}
+)
+
+set(bzle_SOURCES
+att.c bluetooth.c btio.c gatt.c gattrib.c hci.c log.h log.c sdp.c utils.c uuid.c
+)
+
+add_library(bzle ${bzle_SOURCES})
+target_link_libraries(bzle
+ ${GLIB2_LIBRARIES}
+ )
+
+install(TARGETS bzle
+ LIBRARY DESTINATION lib
+ ARCHIVE DESTINATION lib
+)
diff --git a/drive-sdk/deps/bzle/src/att.c b/drive-sdk/deps/bzle/src/att.c
new file mode 100644
index 0000000..802c9c8
--- /dev/null
+++ b/drive-sdk/deps/bzle/src/att.c
@@ -0,0 +1,1165 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2010 Nokia Corporation
+ * Copyright (C) 2010 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 <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include <bzle/bluetooth/bluetooth.h>
+
+#include <glib.h>
+
+#include <bzle/bluetooth/uuid.h>
+#include <bzle/gatt/att.h>
+
+const char *att_ecode2str(uint8_t status)
+{
+ switch (status) {
+ case ATT_ECODE_INVALID_HANDLE:
+ return "Invalid handle";
+ case ATT_ECODE_READ_NOT_PERM:
+ return "Attribute can't be read";
+ case ATT_ECODE_WRITE_NOT_PERM:
+ return "Attribute can't be written";
+ case ATT_ECODE_INVALID_PDU:
+ return "Attribute PDU was invalid";
+ case ATT_ECODE_AUTHENTICATION:
+ return "Attribute requires authentication before read/write";
+ case ATT_ECODE_REQ_NOT_SUPP:
+ return "Server doesn't support the request received";
+ case ATT_ECODE_INVALID_OFFSET:
+ return "Offset past the end of the attribute";
+ case ATT_ECODE_AUTHORIZATION:
+ return "Attribute requires authorization before read/write";
+ case ATT_ECODE_PREP_QUEUE_FULL:
+ return "Too many prepare writes have been queued";
+ case ATT_ECODE_ATTR_NOT_FOUND:
+ return "No attribute found within the given range";
+ case ATT_ECODE_ATTR_NOT_LONG:
+ return "Attribute can't be read/written using Read Blob Req";
+ case ATT_ECODE_INSUFF_ENCR_KEY_SIZE:
+ return "Encryption Key Size is insufficient";
+ case ATT_ECODE_INVAL_ATTR_VALUE_LEN:
+ return "Attribute value length is invalid";
+ case ATT_ECODE_UNLIKELY:
+ return "Request attribute has encountered an unlikely error";
+ case ATT_ECODE_INSUFF_ENC:
+ return "Encryption required before read/write";
+ case ATT_ECODE_UNSUPP_GRP_TYPE:
+ return "Attribute type is not a supported grouping attribute";
+ case ATT_ECODE_INSUFF_RESOURCES:
+ return "Insufficient Resources to complete the request";
+ case ATT_ECODE_IO:
+ return "Internal application error: I/O";
+ case ATT_ECODE_TIMEOUT:
+ return "A timeout occured";
+ case ATT_ECODE_ABORTED:
+ return "The operation was aborted";
+ default:
+ return "Unexpected error code";
+ }
+}
+
+void att_data_list_free(struct att_data_list *list)
+{
+ if (list == NULL)
+ return;
+
+ if (list->data) {
+ int i;
+ for (i = 0; i < list->num; i++)
+ g_free(list->data[i]);
+ }
+
+ g_free(list->data);
+ g_free(list);
+}
+
+struct att_data_list *att_data_list_alloc(uint16_t num, uint16_t len)
+{
+ struct att_data_list *list;
+ int i;
+
+ if (len > UINT8_MAX)
+ return NULL;
+
+ list = g_new0(struct att_data_list, 1);
+ list->len = len;
+ list->num = num;
+
+ list->data = g_malloc0(sizeof(uint8_t *) * num);
+
+ for (i = 0; i < num; i++)
+ list->data[i] = g_malloc0(sizeof(uint8_t) * len);
+
+ return list;
+}
+
+uint16_t enc_read_by_grp_req(uint16_t start, uint16_t end, bt_uuid_t *uuid,
+ uint8_t *pdu, size_t len)
+{
+ uint16_t uuid_len;
+
+ if (!uuid)
+ return 0;
+
+ if (uuid->type == BT_UUID16)
+ uuid_len = 2;
+ else if (uuid->type == BT_UUID128)
+ uuid_len = 16;
+ else
+ return 0;
+
+ /* Attribute Opcode (1 octet) */
+ pdu[0] = ATT_OP_READ_BY_GROUP_REQ;
+ /* Starting Handle (2 octets) */
+ att_put_u16(start, &pdu[1]);
+ /* Ending Handle (2 octets) */
+ att_put_u16(end, &pdu[3]);
+ /* Attribute Group Type (2 or 16 octet UUID) */
+ att_put_uuid(*uuid, &pdu[5]);
+
+ return 5 + uuid_len;
+}
+
+uint16_t dec_read_by_grp_req(const uint8_t *pdu, size_t len, uint16_t *start,
+ uint16_t *end, bt_uuid_t *uuid)
+{
+ const size_t min_len = sizeof(pdu[0]) + sizeof(*start) + sizeof(*end);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (start == NULL || end == NULL || uuid == NULL)
+ return 0;
+
+ if (pdu[0] != ATT_OP_READ_BY_GROUP_REQ)
+ return 0;
+
+ if (len != (min_len + 2) && len != (min_len + 16))
+ return 0;
+
+ *start = att_get_u16(&pdu[1]);
+ *end = att_get_u16(&pdu[3]);
+ if (len == min_len + 2)
+ *uuid = att_get_uuid16(&pdu[5]);
+ else
+ *uuid = att_get_uuid128(&pdu[5]);
+
+ return len;
+}
+
+uint16_t enc_read_by_grp_resp(struct att_data_list *list, uint8_t *pdu,
+ size_t len)
+{
+ int i;
+ uint16_t w;
+ uint8_t *ptr;
+
+ if (list == NULL)
+ return 0;
+
+ if (len < list->len + sizeof(uint8_t) * 2)
+ return 0;
+
+ pdu[0] = ATT_OP_READ_BY_GROUP_RESP;
+ pdu[1] = list->len;
+
+ ptr = &pdu[2];
+
+ for (i = 0, w = 2; i < list->num && w + list->len <= len; i++) {
+ memcpy(ptr, list->data[i], list->len);
+ ptr += list->len;
+ w += list->len;
+ }
+
+ return w;
+}
+
+struct att_data_list *dec_read_by_grp_resp(const uint8_t *pdu, size_t len)
+{
+ struct att_data_list *list;
+ const uint8_t *ptr;
+ uint16_t elen, num;
+ int i;
+
+ if (pdu[0] != ATT_OP_READ_BY_GROUP_RESP)
+ return NULL;
+
+ /* PDU must contain at least:
+ * - Attribute Opcode (1 octet)
+ * - Length (1 octet)
+ * - Attribute Data List (at least one entry):
+ * - Attribute Handle (2 octets)
+ * - End Group Handle (2 octets)
+ * - Attribute Value (at least 1 octet) */
+ if (len < 7)
+ return NULL;
+
+ elen = pdu[1];
+ /* Minimum Attribute Data List size */
+ if (elen < 5)
+ return NULL;
+
+ /* Reject incomplete Attribute Data List */
+ if ((len - 2) % elen)
+ return NULL;
+
+ num = (len - 2) / elen;
+ list = att_data_list_alloc(num, elen);
+ if (list == NULL)
+ return NULL;
+
+ ptr = &pdu[2];
+
+ for (i = 0; i < num; i++) {
+ memcpy(list->data[i], ptr, list->len);
+ ptr += list->len;
+ }
+
+ return list;
+}
+
+uint16_t enc_find_by_type_req(uint16_t start, uint16_t end, bt_uuid_t *uuid,
+ const uint8_t *value, size_t vlen,
+ uint8_t *pdu, size_t len)
+{
+ uint16_t min_len = sizeof(pdu[0]) + sizeof(start) + sizeof(end) +
+ sizeof(uint16_t);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (!uuid)
+ return 0;
+
+ if (uuid->type != BT_UUID16)
+ return 0;
+
+ if (vlen > len - min_len)
+ vlen = len - min_len;
+
+ pdu[0] = ATT_OP_FIND_BY_TYPE_REQ;
+ att_put_u16(start, &pdu[1]);
+ att_put_u16(end, &pdu[3]);
+ att_put_uuid16(*uuid, &pdu[5]);
+
+ if (vlen > 0) {
+ memcpy(&pdu[7], value, vlen);
+ return min_len + vlen;
+ }
+
+ return min_len;
+}
+
+uint16_t dec_find_by_type_req(const uint8_t *pdu, size_t len, uint16_t *start,
+ uint16_t *end, bt_uuid_t *uuid,
+ uint8_t *value, size_t *vlen)
+{
+ if (pdu == NULL)
+ return 0;
+
+ if (len < 7)
+ return 0;
+
+ /* Attribute Opcode (1 octet) */
+ if (pdu[0] != ATT_OP_FIND_BY_TYPE_REQ)
+ return 0;
+
+ /* First requested handle number (2 octets) */
+ *start = att_get_u16(&pdu[1]);
+ /* Last requested handle number (2 octets) */
+ *end = att_get_u16(&pdu[3]);
+ /* 16-bit UUID to find (2 octets) */
+ *uuid = att_get_uuid16(&pdu[5]);
+
+ /* Attribute value to find */
+ *vlen = len - 7;
+ if (*vlen > 0)
+ memcpy(value, pdu + 7, *vlen);
+
+ return len;
+}
+
+uint16_t enc_find_by_type_resp(GSList *matches, uint8_t *pdu, size_t len)
+{
+ GSList *l;
+ uint16_t offset;
+
+ if (!pdu)
+ return 0;
+
+ pdu[0] = ATT_OP_FIND_BY_TYPE_RESP;
+
+ for (l = matches, offset = 1;
+ l && len >= (offset + sizeof(uint16_t) * 2);
+ l = l->next, offset += sizeof(uint16_t) * 2) {
+ struct att_range *range = l->data;
+
+ att_put_u16(range->start, &pdu[offset]);
+ att_put_u16(range->end, &pdu[offset + 2]);
+ }
+
+ return offset;
+}
+
+GSList *dec_find_by_type_resp(const uint8_t *pdu, size_t len)
+{
+ struct att_range *range;
+ GSList *matches;
+ off_t offset;
+
+ /* PDU should contain at least:
+ * - Attribute Opcode (1 octet)
+ * - Handles Information List (at least one entry):
+ * - Found Attribute Handle (2 octets)
+ * - Group End Handle (2 octets) */
+ if (pdu == NULL || len < 5)
+ return NULL;
+
+ if (pdu[0] != ATT_OP_FIND_BY_TYPE_RESP)
+ return NULL;
+
+ /* Reject incomplete Handles Information List */
+ if ((len - 1) % 4)
+ return NULL;
+
+ for (offset = 1, matches = NULL;
+ len >= (offset + sizeof(uint16_t) * 2);
+ offset += sizeof(uint16_t) * 2) {
+ range = g_new0(struct att_range, 1);
+ range->start = att_get_u16(&pdu[offset]);
+ range->end = att_get_u16(&pdu[offset + 2]);
+
+ matches = g_slist_append(matches, range);
+ }
+
+ return matches;
+}
+
+uint16_t enc_read_by_type_req(uint16_t start, uint16_t end, bt_uuid_t *uuid,
+ uint8_t *pdu, size_t len)
+{
+ uint16_t uuid_len;
+
+ if (!uuid)
+ return 0;
+
+ if (uuid->type == BT_UUID16)
+ uuid_len = 2;
+ else if (uuid->type == BT_UUID128)
+ uuid_len = 16;
+ else
+ return 0;
+
+ /* Attribute Opcode (1 octet) */
+ pdu[0] = ATT_OP_READ_BY_TYPE_REQ;
+ /* Starting Handle (2 octets) */
+ att_put_u16(start, &pdu[1]);
+ /* Ending Handle (2 octets) */
+ att_put_u16(end, &pdu[3]);
+ /* Attribute Type (2 or 16 octet UUID) */
+ att_put_uuid(*uuid, &pdu[5]);
+
+ return 5 + uuid_len;
+}
+
+uint16_t dec_read_by_type_req(const uint8_t *pdu, size_t len, uint16_t *start,
+ uint16_t *end, bt_uuid_t *uuid)
+{
+ const size_t min_len = sizeof(pdu[0]) + sizeof(*start) + sizeof(*end);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (start == NULL || end == NULL || uuid == NULL)
+ return 0;
+
+ if (len != (min_len + 2) && len != (min_len + 16))
+ return 0;
+
+ if (pdu[0] != ATT_OP_READ_BY_TYPE_REQ)
+ return 0;
+
+ *start = att_get_u16(&pdu[1]);
+ *end = att_get_u16(&pdu[3]);
+
+ if (len == min_len + 2)
+ *uuid = att_get_uuid16(&pdu[5]);
+ else
+ *uuid = att_get_uuid128(&pdu[5]);
+
+ return len;
+}
+
+uint16_t enc_read_by_type_resp(struct att_data_list *list, uint8_t *pdu,
+ size_t len)
+{
+ uint8_t *ptr;
+ size_t i, w, l;
+
+ if (list == NULL)
+ return 0;
+
+ if (pdu == NULL)
+ return 0;
+
+ l = MIN(len - 2, list->len);
+
+ pdu[0] = ATT_OP_READ_BY_TYPE_RESP;
+ pdu[1] = l;
+ ptr = &pdu[2];
+
+ for (i = 0, w = 2; i < list->num && w + l <= len; i++) {
+ memcpy(ptr, list->data[i], l);
+ ptr += l;
+ w += l;
+ }
+
+ return w;
+}
+
+struct att_data_list *dec_read_by_type_resp(const uint8_t *pdu, size_t len)
+{
+ struct att_data_list *list;
+ const uint8_t *ptr;
+ uint16_t elen, num;
+ int i;
+
+ if (pdu[0] != ATT_OP_READ_BY_TYPE_RESP)
+ return NULL;
+
+ /* PDU must contain at least:
+ * - Attribute Opcode (1 octet)
+ * - Length (1 octet)
+ * - Attribute Data List (at least one entry):
+ * - Attribute Handle (2 octets)
+ * - Attribute Value (at least 1 octet) */
+ if (len < 5)
+ return NULL;
+
+ elen = pdu[1];
+ /* Minimum Attribute Data List size */
+ if (elen < 3)
+ return NULL;
+
+ /* Reject incomplete Attribute Data List */
+ if ((len - 2) % elen)
+ return NULL;
+
+ num = (len - 2) / elen;
+ list = att_data_list_alloc(num, elen);
+ if (list == NULL)
+ return NULL;
+
+ ptr = &pdu[2];
+
+ for (i = 0; i < num; i++) {
+ memcpy(list->data[i], ptr, list->len);
+ ptr += list->len;
+ }
+
+ return list;
+}
+
+uint16_t enc_write_cmd(uint16_t handle, const uint8_t *value, size_t vlen,
+ uint8_t *pdu, size_t len)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(handle);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (vlen > len - min_len)
+ vlen = len - min_len;
+
+ pdu[0] = ATT_OP_WRITE_CMD;
+ att_put_u16(handle, &pdu[1]);
+
+ if (vlen > 0) {
+ memcpy(&pdu[3], value, vlen);
+ return min_len + vlen;
+ }
+
+ return min_len;
+}
+
+uint16_t dec_write_cmd(const uint8_t *pdu, size_t len, uint16_t *handle,
+ uint8_t *value, size_t *vlen)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(*handle);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (value == NULL || vlen == NULL || handle == NULL)
+ return 0;
+
+ if (len < min_len)
+ return 0;
+
+ if (pdu[0] != ATT_OP_WRITE_CMD)
+ return 0;
+
+ *handle = att_get_u16(&pdu[1]);
+ memcpy(value, pdu + min_len, len - min_len);
+ *vlen = len - min_len;
+
+ return len;
+}
+
+uint16_t enc_write_req(uint16_t handle, const uint8_t *value, size_t vlen,
+ uint8_t *pdu, size_t len)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(handle);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (vlen > len - min_len)
+ vlen = len - min_len;
+
+ pdu[0] = ATT_OP_WRITE_REQ;
+ att_put_u16(handle, &pdu[1]);
+
+ if (vlen > 0) {
+ memcpy(&pdu[3], value, vlen);
+ return min_len + vlen;
+ }
+
+ return min_len;
+}
+
+uint16_t dec_write_req(const uint8_t *pdu, size_t len, uint16_t *handle,
+ uint8_t *value, size_t *vlen)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(*handle);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (value == NULL || vlen == NULL || handle == NULL)
+ return 0;
+
+ if (len < min_len)
+ return 0;
+
+ if (pdu[0] != ATT_OP_WRITE_REQ)
+ return 0;
+
+ *handle = att_get_u16(&pdu[1]);
+ *vlen = len - min_len;
+ if (*vlen > 0)
+ memcpy(value, pdu + min_len, *vlen);
+
+ return len;
+}
+
+uint16_t enc_write_resp(uint8_t *pdu)
+{
+ if (pdu == NULL)
+ return 0;
+
+ pdu[0] = ATT_OP_WRITE_RESP;
+
+ return sizeof(pdu[0]);
+}
+
+uint16_t dec_write_resp(const uint8_t *pdu, size_t len)
+{
+ if (pdu == NULL)
+ return 0;
+
+ if (pdu[0] != ATT_OP_WRITE_RESP)
+ return 0;
+
+ return len;
+}
+
+uint16_t enc_read_req(uint16_t handle, uint8_t *pdu, size_t len)
+{
+ if (pdu == NULL)
+ return 0;
+
+ /* Attribute Opcode (1 octet) */
+ pdu[0] = ATT_OP_READ_REQ;
+ /* Attribute Handle (2 octets) */
+ att_put_u16(handle, &pdu[1]);
+
+ return 3;
+}
+
+uint16_t enc_read_blob_req(uint16_t handle, uint16_t offset, uint8_t *pdu,
+ size_t len)
+{
+ if (pdu == NULL)
+ return 0;
+
+ /* Attribute Opcode (1 octet) */
+ pdu[0] = ATT_OP_READ_BLOB_REQ;
+ /* Attribute Handle (2 octets) */
+ att_put_u16(handle, &pdu[1]);
+ /* Value Offset (2 octets) */
+ att_put_u16(offset, &pdu[3]);
+
+ return 5;
+}
+
+uint16_t dec_read_req(const uint8_t *pdu, size_t len, uint16_t *handle)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(*handle);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (handle == NULL)
+ return 0;
+
+ if (len < min_len)
+ return 0;
+
+ if (pdu[0] != ATT_OP_READ_REQ)
+ return 0;
+
+ *handle = att_get_u16(&pdu[1]);
+
+ return min_len;
+}
+
+uint16_t dec_read_blob_req(const uint8_t *pdu, size_t len, uint16_t *handle,
+ uint16_t *offset)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(*handle) +
+ sizeof(*offset);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (handle == NULL)
+ return 0;
+
+ if (offset == NULL)
+ return 0;
+
+ if (len < min_len)
+ return 0;
+
+ if (pdu[0] != ATT_OP_READ_BLOB_REQ)
+ return 0;
+
+ *handle = att_get_u16(&pdu[1]);
+ *offset = att_get_u16(&pdu[3]);
+
+ return min_len;
+}
+
+uint16_t enc_read_resp(uint8_t *value, size_t vlen, uint8_t *pdu, size_t len)
+{
+ if (pdu == NULL)
+ return 0;
+
+ /* If the attribute value length is longer than the allowed PDU size,
+ * send only the octets that fit on the PDU. The remaining octets can
+ * be requested using the Read Blob Request. */
+ if (vlen > len - 1)
+ vlen = len - 1;
+
+ pdu[0] = ATT_OP_READ_RESP;
+
+ memcpy(pdu + 1, value, vlen);
+
+ return vlen + 1;
+}
+
+uint16_t enc_read_blob_resp(uint8_t *value, size_t vlen, uint16_t offset,
+ uint8_t *pdu, size_t len)
+{
+ if (pdu == NULL)
+ return 0;
+
+ vlen -= offset;
+ if (vlen > len - 1)
+ vlen = len - 1;
+
+ pdu[0] = ATT_OP_READ_BLOB_RESP;
+
+ memcpy(pdu + 1, &value[offset], vlen);
+
+ return vlen + 1;
+}
+
+ssize_t dec_read_resp(const uint8_t *pdu, size_t len, uint8_t *value,
+ size_t vlen)
+{
+ if (pdu == NULL)
+ return -EINVAL;
+
+ if (pdu[0] != ATT_OP_READ_RESP)
+ return -EINVAL;
+
+ if (value == NULL)
+ return len - 1;
+
+ if (vlen < (len - 1))
+ return -ENOBUFS;
+
+ memcpy(value, pdu + 1, len - 1);
+
+ return len - 1;
+}
+
+uint16_t enc_error_resp(uint8_t opcode, uint16_t handle, uint8_t status,
+ uint8_t *pdu, size_t len)
+{
+ /* Attribute Opcode (1 octet) */
+ pdu[0] = ATT_OP_ERROR;
+ /* Request Opcode In Error (1 octet) */
+ pdu[1] = opcode;
+ /* Attribute Handle In Error (2 octets) */
+ att_put_u16(handle, &pdu[2]);
+ /* Error Code (1 octet) */
+ pdu[4] = status;
+
+ return 5;
+}
+
+uint16_t enc_find_info_req(uint16_t start, uint16_t end, uint8_t *pdu,
+ size_t len)
+{
+ if (pdu == NULL)
+ return 0;
+
+ /* Attribute Opcode (1 octet) */
+ pdu[0] = ATT_OP_FIND_INFO_REQ;
+ /* Starting Handle (2 octets) */
+ att_put_u16(start, &pdu[1]);
+ /* Ending Handle (2 octets) */
+ att_put_u16(end, &pdu[3]);
+
+ return 5;
+}
+
+uint16_t dec_find_info_req(const uint8_t *pdu, size_t len, uint16_t *start,
+ uint16_t *end)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(*start) + sizeof(*end);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (len < min_len)
+ return 0;
+
+ if (start == NULL || end == NULL)
+ return 0;
+
+ if (pdu[0] != ATT_OP_FIND_INFO_REQ)
+ return 0;
+
+ *start = att_get_u16(&pdu[1]);
+ *end = att_get_u16(&pdu[3]);
+
+ return min_len;
+}
+
+uint16_t enc_find_info_resp(uint8_t format, struct att_data_list *list,
+ uint8_t *pdu, size_t len)
+{
+ uint8_t *ptr;
+ size_t i, w;
+
+ if (pdu == NULL)
+ return 0;
+
+ if (list == NULL)
+ return 0;
+
+ if (len < list->len + sizeof(uint8_t) * 2)
+ return 0;
+
+ pdu[0] = ATT_OP_FIND_INFO_RESP;
+ pdu[1] = format;
+ ptr = (void *) &pdu[2];
+
+ for (i = 0, w = 2; i < list->num && w + list->len <= len; i++) {
+ memcpy(ptr, list->data[i], list->len);
+ ptr += list->len;
+ w += list->len;
+ }
+
+ return w;
+}
+
+struct att_data_list *dec_find_info_resp(const uint8_t *pdu, size_t len,
+ uint8_t *format)
+{
+ struct att_data_list *list;
+ uint8_t *ptr;
+ uint16_t elen, num;
+ int i;
+
+ if (pdu == NULL)
+ return 0;
+
+ if (format == NULL)
+ return 0;
+
+ if (pdu[0] != ATT_OP_FIND_INFO_RESP)
+ return 0;
+
+ *format = pdu[1];
+ elen = sizeof(pdu[0]) + sizeof(*format);
+ if (*format == 0x01)
+ elen += 2;
+ else if (*format == 0x02)
+ elen += 16;
+
+ num = (len - 2) / elen;
+
+ ptr = (void *) &pdu[2];
+
+ list = att_data_list_alloc(num, elen);
+ if (list == NULL)
+ return NULL;
+
+ for (i = 0; i < num; i++) {
+ memcpy(list->data[i], ptr, list->len);
+ ptr += list->len;
+ }
+
+ return list;
+}
+
+uint16_t enc_notification(uint16_t handle, uint8_t *value, size_t vlen,
+ uint8_t *pdu, size_t len)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(uint16_t);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (len < (vlen + min_len))
+ return 0;
+
+ pdu[0] = ATT_OP_HANDLE_NOTIFY;
+ att_put_u16(handle, &pdu[1]);
+ memcpy(&pdu[3], value, vlen);
+
+ return vlen + min_len;
+}
+
+uint16_t enc_indication(uint16_t handle, uint8_t *value, size_t vlen,
+ uint8_t *pdu, size_t len)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(uint16_t);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (len < (vlen + min_len))
+ return 0;
+
+ pdu[0] = ATT_OP_HANDLE_IND;
+ att_put_u16(handle, &pdu[1]);
+ memcpy(&pdu[3], value, vlen);
+
+ return vlen + min_len;
+}
+
+uint16_t dec_indication(const uint8_t *pdu, size_t len, uint16_t *handle,
+ uint8_t *value, size_t vlen)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(uint16_t);
+ uint16_t dlen;
+
+ if (pdu == NULL)
+ return 0;
+
+ if (pdu[0] != ATT_OP_HANDLE_IND)
+ return 0;
+
+ if (len < min_len)
+ return 0;
+
+ dlen = MIN(len - min_len, vlen);
+
+ if (handle)
+ *handle = att_get_u16(&pdu[1]);
+
+ memcpy(value, &pdu[3], dlen);
+
+ return dlen;
+}
+
+uint16_t enc_confirmation(uint8_t *pdu, size_t len)
+{
+ if (pdu == NULL)
+ return 0;
+
+ /* Attribute Opcode (1 octet) */
+ pdu[0] = ATT_OP_HANDLE_CNF;
+
+ return 1;
+}
+
+uint16_t enc_mtu_req(uint16_t mtu, uint8_t *pdu, size_t len)
+{
+ if (pdu == NULL)
+ return 0;
+
+ /* Attribute Opcode (1 octet) */
+ pdu[0] = ATT_OP_MTU_REQ;
+ /* Client Rx MTU (2 octets) */
+ att_put_u16(mtu, &pdu[1]);
+
+ return 3;
+}
+
+uint16_t dec_mtu_req(const uint8_t *pdu, size_t len, uint16_t *mtu)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(*mtu);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (mtu == NULL)
+ return 0;
+
+ if (len < min_len)
+ return 0;
+
+ if (pdu[0] != ATT_OP_MTU_REQ)
+ return 0;
+
+ *mtu = att_get_u16(&pdu[1]);
+
+ return min_len;
+}
+
+uint16_t enc_mtu_resp(uint16_t mtu, uint8_t *pdu, size_t len)
+{
+ if (pdu == NULL)
+ return 0;
+
+ /* Attribute Opcode (1 octet) */
+ pdu[0] = ATT_OP_MTU_RESP;
+ /* Server Rx MTU (2 octets) */
+ att_put_u16(mtu, &pdu[1]);
+
+ return 3;
+}
+
+uint16_t dec_mtu_resp(const uint8_t *pdu, size_t len, uint16_t *mtu)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(*mtu);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (mtu == NULL)
+ return 0;
+
+ if (len < min_len)
+ return 0;
+
+ if (pdu[0] != ATT_OP_MTU_RESP)
+ return 0;
+
+ *mtu = att_get_u16(&pdu[1]);
+
+ return min_len;
+}
+
+uint16_t enc_prep_write_req(uint16_t handle, uint16_t offset,
+ const uint8_t *value, size_t vlen,
+ uint8_t *pdu, size_t len)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(handle) +
+ sizeof(offset);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (vlen > len - min_len)
+ vlen = len - min_len;
+
+ pdu[0] = ATT_OP_PREP_WRITE_REQ;
+ att_put_u16(handle, &pdu[1]);
+ att_put_u16(offset, &pdu[3]);
+
+ if (vlen > 0) {
+ memcpy(&pdu[5], value, vlen);
+ return min_len + vlen;
+ }
+
+ return min_len;
+}
+
+uint16_t dec_prep_write_req(const uint8_t *pdu, size_t len, uint16_t *handle,
+ uint16_t *offset, uint8_t *value, size_t *vlen)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(*handle) +
+ sizeof(*offset);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (handle == NULL || offset == NULL || value == NULL || vlen == NULL)
+ return 0;
+
+ if (len < min_len)
+ return 0;
+
+ if (pdu[0] != ATT_OP_PREP_WRITE_REQ)
+ return 0;
+
+ *handle = att_get_u16(&pdu[1]);
+ *offset = att_get_u16(&pdu[3]);
+
+ *vlen = len - min_len;
+ if (*vlen > 0)
+ memcpy(value, pdu + min_len, *vlen);
+
+ return len;
+}
+
+uint16_t enc_prep_write_resp(uint16_t handle, uint16_t offset,
+ const uint8_t *value, size_t vlen,
+ uint8_t *pdu, size_t len)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(handle) +
+ sizeof(offset);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (vlen > len - min_len)
+ vlen = len - min_len;
+
+ pdu[0] = ATT_OP_PREP_WRITE_RESP;
+ att_put_u16(handle, &pdu[1]);
+ att_put_u16(offset, &pdu[3]);
+
+ if (vlen > 0) {
+ memcpy(&pdu[5], value, vlen);
+ return min_len + vlen;
+ }
+
+ return min_len;
+}
+
+uint16_t dec_prep_write_resp(const uint8_t *pdu, size_t len, uint16_t *handle,
+ uint16_t *offset, uint8_t *value, size_t *vlen)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(*handle) +
+ sizeof(*offset);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (handle == NULL || offset == NULL || value == NULL || vlen == NULL)
+ return 0;
+
+ if (len < min_len)
+ return 0;
+
+ if (pdu[0] != ATT_OP_PREP_WRITE_REQ)
+ return 0;
+
+ *handle = att_get_u16(&pdu[1]);
+ *offset = att_get_u16(&pdu[3]);
+ *vlen = len - min_len;
+ if (*vlen > 0)
+ memcpy(value, pdu + min_len, *vlen);
+
+ return len;
+}
+
+uint16_t enc_exec_write_req(uint8_t flags, uint8_t *pdu, size_t len)
+{
+ if (pdu == NULL)
+ return 0;
+
+ if (flags > 1)
+ return 0;
+
+ /* Attribute Opcode (1 octet) */
+ pdu[0] = ATT_OP_EXEC_WRITE_REQ;
+ /* Flags (1 octet) */
+ pdu[1] = flags;
+
+ return 2;
+}
+
+uint16_t dec_exec_write_req(const uint8_t *pdu, size_t len, uint8_t *flags)
+{
+ const uint16_t min_len = sizeof(pdu[0]) + sizeof(*flags);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (flags == NULL)
+ return 0;
+
+ if (len < min_len)
+ return 0;
+
+ if (pdu[0] != ATT_OP_EXEC_WRITE_REQ)
+ return 0;
+
+ *flags = pdu[1];
+
+ return min_len;
+}
+
+uint16_t enc_exec_write_resp(uint8_t *pdu)
+{
+ if (pdu == NULL)
+ return 0;
+
+ /* Attribute Opcode (1 octet) */
+ pdu[0] = ATT_OP_EXEC_WRITE_RESP;
+
+ return 1;
+}
+
+uint16_t dec_exec_write_resp(const uint8_t *pdu, size_t len)
+{
+ const uint16_t min_len = sizeof(pdu[0]);
+
+ if (pdu == NULL)
+ return 0;
+
+ if (len < min_len)
+ return 0;
+
+ if (pdu[0] != ATT_OP_EXEC_WRITE_RESP)
+ return 0;
+
+ return len;
+}
diff --git a/drive-sdk/deps/bzle/src/bluetooth.c b/drive-sdk/deps/bzle/src/bluetooth.c
new file mode 100644
index 0000000..af80c79
--- /dev/null
+++ b/drive-sdk/deps/bzle/src/bluetooth.c
@@ -0,0 +1,867 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2000-2001 Qualcomm Incorporated
+ * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
+ * Copyright (C) 2002-2010 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 <bzle/bluetooth/bluetooth.h>
+#include <bzle/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)
+{
+ bdaddr_t b;
+ bdaddr_t *ba = bt_malloc(sizeof(*ba));
+
+ if (ba) {
+ str2ba(str, &b);
+ baswap(ba, &b);
+ }
+
+ return ba;
+}
+
+int ba2str(const bdaddr_t *ba, char *str)
+{
+ return sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
+ ba->b[5], ba->b[4], ba->b[3], ba->b[2], ba->b[1], ba->b[0]);
+}
+
+int str2ba(const char *str, bdaddr_t *ba)
+{
+ int i;
+
+ if (bachk(str) < 0) {
+ memset(ba, 0, sizeof(*ba));
+ return -1;
+ }
+
+ for (i = 5; i >= 0; i--, str += 3)
+ ba->b[i] = strtol(str, NULL, 16);
+
+ return 0;
+}
+
+int ba2oui(const bdaddr_t *ba, char *str)
+{
+ return sprintf(str, "%2.2X-%2.2X-%2.2X", ba->b[5], ba->b[4], ba->b[3]);
+}
+
+int bachk(const char *str)
+{
+ if (!str)
+ return -1;
+
+ if (strlen(str) != 17)
+ return -1;
+
+ while (*str) {
+ if (!isxdigit(*str++))
+ return -1;
+
+ if (!isxdigit(*str++))
+ return -1;
+
+ if (*str == 0)
+ break;
+
+ if (*str++ != ':')
+ return -1;
+ }
+
+ 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;
+ }
+}
+
+const 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 "Ceva, Inc. (formerly 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 & Schwarz 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 "Synopsis";
+ 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 Taiwan, Inc.";
+ case 66:
+ return "CONWISE Technology Corporation Ltd";
+ case 67:
+ return "PARROT SA";
+ case 68:
+ return "Socket Mobile";
+ case 69:
+ return "Atheros Communications, Inc.";
+ case 70:
+ return "MediaTek, Inc.";
+ case 71:
+ return "Bluegiga";
+ 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 Licensing Ltd.";
+ case 80:
+ return "SiRF Technology";
+ case 81:
+ return "Tzero Technologies, Inc.";
+ case 82:
+ return "J&M Corporation";
+ case 83:
+ return "Free2move AB";
+ case 84:
+ return "3DiJoy Corporation";
+ case 85:
+ return "Plantronics, Inc.";
+ case 86:
+ return "Sony Ericsson Mobile Communications";
+ case 87:
+ return "Harman International Industries, Inc.";
+ case 88:
+ return "Vizio, Inc.";
+ case 89:
+ return "Nordic Semiconductor ASA";
+ case 90:
+ return "EM Microelectronic-Marin SA";
+ case 91:
+ return "Ralink Technology Corporation";
+ case 92:
+ return "Belkin International, Inc.";
+ case 93:
+ return "Realtek Semiconductor Corporation";
+ case 94:
+ return "Stonestreet One, LLC";
+ case 95:
+ return "Wicentric, Inc.";
+ case 96:
+ return "RivieraWaves S.A.S";
+ case 97:
+ return "RDA Microelectronics";
+ case 98:
+ return "Gibson Guitars";
+ case 99:
+ return "MiCommand Inc.";
+ case 100:
+ return "Band XI International, LLC";
+ case 101:
+ return "Hewlett-Packard Company";
+ case 102:
+ return "9Solutions Oy";
+ case 103:
+ return "GN Netcom A/S";
+ case 104:
+ return "General Motors";
+ case 105:
+ return "A&D Engineering, Inc.";
+ case 106:
+ return "MindTree Ltd.";
+ case 107:
+ return "Polar Electro OY";
+ case 108:
+ return "Beautiful Enterprise Co., Ltd.";
+ case 109:
+ return "BriarTek, Inc.";
+ case 110:
+ return "Summit Data Communications, Inc.";
+ case 111:
+ return "Sound ID";
+ case 112:
+ return "Monster, LLC";
+ case 113:
+ return "connectBlue AB";
+ case 114:
+ return "ShangHai Super Smart Electronics Co. Ltd.";
+ case 115:
+ return "Group Sense Ltd.";
+ case 116:
+ return "Zomm, LLC";
+ case 117:
+ return "Samsung Electronics Co. Ltd.";
+ case 118:
+ return "Creative Technology Ltd.";
+ case 119:
+ return "Laird Technologies";
+ case 120:
+ return "Nike, Inc.";
+ case 121:
+ return "lesswire AG";
+ case 122:
+ return "MStar Semiconductor, Inc.";
+ case 123:
+ return "Hanlynn Technologies";
+ case 124:
+ return "A & R Cambridge";
+ case 125:
+ return "Seers Technology Co. Ltd";
+ case 126:
+ return "Sports Tracking Technologies Ltd.";
+ case 127:
+ return "Autonet Mobile";
+ case 128:
+ return "DeLorme Publishing Company, Inc.";
+ case 129:
+ return "WuXi Vimicro";
+ case 130:
+ return "Sennheiser Communications A/S";
+ case 131:
+ return "TimeKeeping Systems, Inc.";
+ case 132:
+ return "Ludus Helsinki Ltd.";
+ case 133:
+ return "BlueRadios, Inc.";
+ case 134:
+ return "equinox AG";
+ case 135:
+ return "Garmin International, Inc.";
+ case 136:
+ return "Ecotest";
+ case 137:
+ return "GN ReSound A/S";
+ case 138:
+ return "Jawbone";
+ case 139:
+ return "Topcorn Positioning Systems, LLC";
+ case 140:
+ return "Qualcomm Retail Solutions, Inc. (formerly Qualcomm Labs, Inc.)";
+ case 141:
+ return "Zscan Software";
+ case 142:
+ return "Quintic Corp.";
+ case 143:
+ return "Stollman E+V GmbH";
+ case 144:
+ return "Funai Electric Co., Ltd.";
+ case 145:
+ return "Advanced PANMOBIL Systems GmbH & Co. KG";
+ case 146:
+ return "ThinkOptics, Inc.";
+ case 147:
+ return "Universal Electronics, Inc.";
+ case 148:
+ return "Airoha Technology Corp.";
+ case 149:
+ return "NEC Lighting, Ltd.";
+ case 150:
+ return "ODM Technology, Inc.";
+ case 151:
+ return "ConnecteDevice Ltd.";
+ case 152:
+ return "zer01.tv GmbH";
+ case 153:
+ return "i.Tech Dynamic Global Distribution Ltd.";
+ case 154:
+ return "Alpwise";
+ case 155:
+ return "Jiangsu Toppower Automotive Electronics Co., Ltd.";
+ case 156:
+ return "Colorfy, Inc.";
+ case 157:
+ return "Geoforce Inc.";
+ case 158:
+ return "Bose Corporation";
+ case 159:
+ return "Suunto Oy";
+ case 160:
+ return "Kensington Computer Products Group";
+ case 161:
+ return "SR-Medizinelektronik";
+ case 162:
+ return "Vertu Corporation Limited";
+ case 163:
+ return "Meta Watch Ltd.";
+ case 164:
+ return "LINAK A/S";
+ case 165:
+ return "OTL Dynamics LLC";
+ case 166:
+ return "Panda Ocean Inc.";
+ case 167:
+ return "Visteon Corporation";
+ case 168:
+ return "ARP Devices Limited";
+ case 169:
+ return "Magneti Marelli S.p.A";
+ case 170:
+ return "CAEN RFID srl";
+ case 171:
+ return "Ingenieur-Systemgruppe Zahn GmbH";
+ case 172:
+ return "Green Throttle Games";
+ case 173:
+ return "Peter Systemtechnik GmbH";
+ case 174:
+ return "Omegawave Oy";
+ case 175:
+ return "Cinetix";
+ case 176:
+ return "Passif Semiconductor Corp";
+ case 177:
+ return "Saris Cycling Group, Inc";
+ case 178:
+ return "Bekey A/S";
+ case 179:
+ return "Clarinox Technologies Pty. Ltd.";
+ case 180:
+ return "BDE Technology Co., Ltd.";
+ case 181:
+ return "Swirl Networks";
+ case 182:
+ return "Meso international";
+ case 183:
+ return "TreLab Ltd";
+ case 184:
+ return "Qualcomm Innovation Center, Inc. (QuIC)";
+ case 185:
+ return "Johnson Controls, Inc.";
+ case 186:
+ return "Starkey Laboratories Inc.";
+ case 187:
+ return "S-Power Electronics Limited";
+ case 188:
+ return "Ace Sensor Inc";
+ case 189:
+ return "Aplix Corporation";
+ case 190:
+ return "AAMP of America";
+ case 191:
+ return "Stalmart Technology Limited";
+ case 192:
+ return "AMICCOM Electronics Corporation";
+ case 193:
+ return "Shenzhen Excelsecu Data Technology Co.,Ltd";
+ case 194:
+ return "Geneq Inc.";
+ case 195:
+ return "adidas AG";
+ case 196:
+ return "LG Electronics";
+ case 197:
+ return "Onset Computer Corporation";
+ case 198:
+ return "Selfly BV";
+ case 199:
+ return "Quuppa Oy.";
+ case 200:
+ return "GeLo Inc";
+ case 201:
+ return "Evluma";
+ case 202:
+ return "MC10";
+ case 203:
+ return "Binauric SE";
+ case 204:
+ return "Beats Electronics";
+ case 205:
+ return "Microchip Technology Inc.";
+ case 206:
+ return "Elgato Systems GmbH";
+ case 207:
+ return "ARCHOS SA";
+ case 208:
+ return "Dexcom, Inc.";
+ case 209:
+ return "Polar Electro Europe B.V.";
+ case 210:
+ return "Dialog Semiconductor B.V.";
+ case 211:
+ return "Taixingbang Technology (HK) Co,. LTD.";
+ case 212:
+ return "Kawantech";
+ case 213:
+ return "Austco Communication Systems";
+ case 214:
+ return "Timex Group USA, Inc.";
+ case 215:
+ return "Qualcomm Technologies, Inc.";
+ case 216:
+ return "Qualcomm Connected Experiences, Inc.";
+ case 217:
+ return "Voyetra Turtle Beach";
+ case 218:
+ return "txtr GmbH";
+ case 219:
+ return "Biosentronics";
+ case 220:
+ return "Procter & Gamble";
+ case 221:
+ return "Hosiden Corporation";
+ case 222:
+ return "Muzik LLC";
+ case 223:
+ return "Misfit Wearables Corp";
+ case 224:
+ return "Google";
+ case 225:
+ return "Danlers Ltd";
+ case 226:
+ return "Semilink Inc";
+ case 227:
+ return "inMusic Brands, Inc";
+ case 228:
+ return "L.S. Research Inc.";
+ case 229:
+ return "Eden Software Consultants Ltd.";
+ case 230:
+ return "Freshtemp";
+ case 231:
+ return "KS Technologies";
+ case 232:
+ return "ACTS Technologies";
+ case 233:
+ return "Vtrack Systems";
+ case 234:
+ return "Nielsen-Kellerman Company";
+ case 235:
+ return "Server Technology, Inc.";
+ case 236:
+ return "BioResearch Associates";
+ case 237:
+ return "Jolly Logic, LLC";
+ case 238:
+ return "Above Average Outcomes, Inc.";
+ case 239:
+ return "Bitsplitters GmbH";
+ case 240:
+ return "PayPal, Inc.";
+ case 241:
+ return "Witron Technology Limited";
+ case 242:
+ return "Morse Project Inc.";
+ case 243:
+ return "Kent Displays Inc.";
+ case 244:
+ return "Nautilus Inc.";
+ case 245:
+ return "Smartifier Oy";
+ case 246:
+ return "Elcometer Limited";
+ case 247:
+ return "VSN Technologies Inc.";
+ case 248:
+ return "AceUni Corp., Ltd.";
+ case 249:
+ return "StickNFind";
+ case 250:
+ return "Crystal Code AB";
+ case 251:
+ return "KOUKAAM a.s.";
+ case 252:
+ return "Delphi Corporation";
+ case 253:
+ return "ValenceTech Limited";
+ case 254:
+ return "Reserved";
+ case 255:
+ return "Typo Products, LLC";
+ case 256:
+ return "TomTom International BV";
+ case 257:
+ return "Fugoo, Inc";
+ case 258:
+ return "Keiser Corporation";
+ case 259:
+ return "Bang & Olufsen A/S";
+ case 260:
+ return "PLUS Locations Systems Pty Ltd";
+ case 261:
+ return "Ubiquitous Computing Technology Corporation";
+ case 262:
+ return "Innovative Yachtter Solutions";
+ case 263:
+ return "William Demant Holding A/S";
+ case 264:
+ return "Chicony Electronics Co., Ltd.";
+ case 265:
+ return "Atus BV";
+ case 266:
+ return "Codegate Ltd.";
+ case 267:
+ return "ERi, Inc.";
+ case 268:
+ return "Transducers Direct, LLC";
+ case 269:
+ return "Fujitsu Ten Limited";
+ case 270:
+ return "Audi AG";
+ case 271:
+ return "HiSilicon Technologies Co., Ltd.";
+ case 272:
+ return "Nippon Seiki Co., Ltd.";
+ case 273:
+ return "Steelseries ApS";
+ case 274:
+ return "vyzybl Inc.";
+ case 275:
+ return "Openbrain Technologies, Co., Ltd.";
+ case 276:
+ return "Xensr";
+ case 277:
+ return "e.solutions";
+ case 278:
+ return "1OAK Technologies";
+ case 279:
+ return "Wimoto Technologies Inc";
+ case 280:
+ return "Radius Networks, Inc.";
+ case 281:
+ return "Wize Technology Co., Ltd.";
+ case 282:
+ return "Qualcomm Labs, Inc.";
+ case 283:
+ return "Aruba Networks";
+ case 284:
+ return "Baidu";
+ case 285:
+ return "Arendi AG";
+ case 286:
+ return "Skoda Auto a.s.";
+ case 287:
+ return "Volkswagon AG";
+ case 288:
+ return "Porsche AG";
+ case 289:
+ return "Sino Wealth Electronic Ltd.";
+ case 290:
+ return "AirTurn, Inc.";
+ case 291:
+ return "Kinsa, Inc.";
+ case 292:
+ return "HID Global";
+ case 293:
+ return "SEAT es";
+ case 294:
+ return "Promethean Ltd.";
+ case 295:
+ return "Salutica Allied Solutions";
+ case 296:
+ return "GPSI Group Pty Ltd";
+ case 297:
+ return "Nimble Devices Oy";
+ case 298:
+ return "Changzhou Yongse Infotech Co., Ltd";
+ case 65535:
+ return "internal use";
+ default:
+ return "not assigned";
+ }
+}
diff --git a/drive-sdk/deps/bzle/src/btio.c b/drive-sdk/deps/bzle/src/btio.c
new file mode 100644
index 0000000..6ad7e7a
--- /dev/null
+++ b/drive-sdk/deps/bzle/src/btio.c
@@ -0,0 +1,1186 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2009-2010 Marcel Holtmann <marcel@holtmann.org>
+ * Copyright (C) 2009-2010 Nokia Corporation
+ *
+ *
+ * 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
+ *
+ */
+
+/*
+ * Remove support for RF_COMM and SCO communication types,
+ * since these are not used by Bluetooth LE.
+ *
+ * 01-Mar-2014 Brian Chapados <chapados@anki.com>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <poll.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <bzle/bluetooth/bluetooth.h>
+#include <bzle/bluetooth/l2cap.h>
+
+#include <glib.h>
+
+#include <bzle/bluetooth/btio.h>
+
+#ifndef BT_FLUSHABLE
+#define BT_FLUSHABLE 8
+#endif
+
+#define ERROR_FAILED(gerr, str, err) \
+ g_set_error(gerr, BT_IO_ERROR, err, \
+ str ": %s (%d)", strerror(err), err)
+
+#define DEFAULT_DEFER_TIMEOUT 30
+
+typedef enum {
+ BT_IO_L2CAP,
+ BT_IO_RFCOMM,
+ BT_IO_SCO,
+ BT_IO_INVALID,
+} BtIOType;
+
+struct set_opts {
+ bdaddr_t src;
+ bdaddr_t dst;
+ BtIOType type;
+ uint8_t src_type;
+ uint8_t dst_type;
+ int defer;
+ int sec_level;
+ uint8_t channel;
+ uint16_t psm;
+ uint16_t cid;
+ uint16_t mtu;
+ uint16_t imtu;
+ uint16_t omtu;
+ int master;
+ uint8_t mode;
+ int flushable;
+ uint32_t priority;
+ uint16_t voice;
+};
+
+struct connect {
+ BtIOConnect connect;
+ gpointer user_data;
+ GDestroyNotify destroy;
+};
+
+struct accept {
+ BtIOConnect connect;
+ gpointer user_data;
+ GDestroyNotify destroy;
+};
+
+struct server {
+ BtIOConnect connect;
+ BtIOConfirm confirm;
+ gpointer user_data;
+ GDestroyNotify destroy;
+};
+
+static BtIOType bt_io_get_type(GIOChannel *io, GError **gerr)
+{
+ int sk = g_io_channel_unix_get_fd(io);
+ int domain, proto, err;
+ socklen_t len;
+
+ domain = 0;
+ len = sizeof(domain);
+ err = getsockopt(sk, SOL_SOCKET, SO_DOMAIN, &domain, &len);
+ if (err < 0) {
+ ERROR_FAILED(gerr, "getsockopt(SO_DOMAIN)", errno);
+ return BT_IO_INVALID;
+ }
+
+ if (domain != AF_BLUETOOTH) {
+ g_set_error(gerr, BT_IO_ERROR, EINVAL,
+ "BtIO socket domain not AF_BLUETOOTH");
+ return BT_IO_INVALID;
+ }
+
+ proto = 0;
+ len = sizeof(proto);
+ err = getsockopt(sk, SOL_SOCKET, SO_PROTOCOL, &proto, &len);
+ if (err < 0) {
+ ERROR_FAILED(gerr, "getsockopt(SO_PROTOCOL)", errno);
+ return BT_IO_INVALID;
+ }
+
+ switch (proto) {
+ case BTPROTO_RFCOMM:
+ return BT_IO_RFCOMM;
+ case BTPROTO_SCO:
+ return BT_IO_SCO;
+ case BTPROTO_L2CAP:
+ return BT_IO_L2CAP;
+ default:
+ g_set_error(gerr, BT_IO_ERROR, EINVAL,
+ "Unknown BtIO socket type");
+ return BT_IO_INVALID;
+ }
+}
+
+static void server_remove(struct server *server)
+{
+ if (server->destroy)
+ server->destroy(server->user_data);
+ g_free(server);
+}
+
+static void connect_remove(struct connect *conn)
+{
+ if (conn->destroy)
+ conn->destroy(conn->user_data);
+ g_free(conn);
+}
+
+static void accept_remove(struct accept *accept)
+{
+ if (accept->destroy)
+ accept->destroy(accept->user_data);
+ g_free(accept);
+}
+
+static gboolean check_nval(GIOChannel *io)
+{
+ struct pollfd fds;
+
+ memset(&fds, 0, sizeof(fds));
+ fds.fd = g_io_channel_unix_get_fd(io);
+ fds.events = POLLNVAL;
+
+ if (poll(&fds, 1, 0) > 0 && (fds.revents & POLLNVAL))
+ return TRUE;
+
+ return FALSE;
+}
+
+static gboolean accept_cb(GIOChannel *io, GIOCondition cond,
+ gpointer user_data)
+{
+ struct accept *accept = user_data;
+ GError *gerr = NULL;
+
+ /* If the user aborted this accept attempt */
+ if ((cond & G_IO_NVAL) || check_nval(io))
+ return FALSE;
+
+ if (cond & (G_IO_HUP | G_IO_ERR)) {
+ int err, sk_err, sock = g_io_channel_unix_get_fd(io);
+ socklen_t len = sizeof(sk_err);
+
+ if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &sk_err, &len) < 0)
+ err = -errno;
+ else
+ err = -sk_err;
+
+ if (err < 0)
+ ERROR_FAILED(&gerr, "HUP or ERR on socket", -err);
+ }
+
+ accept->connect(io, gerr, accept->user_data);
+
+ g_clear_error(&gerr);
+
+ return FALSE;
+}
+
+static gboolean connect_cb(GIOChannel *io, GIOCondition cond,
+ gpointer user_data)
+{
+ struct connect *conn = user_data;
+ GError *gerr = NULL;
+ int err, sk_err, sock;
+ socklen_t len = sizeof(sk_err);
+
+ /* If the user aborted this connect attempt */
+ if ((cond & G_IO_NVAL) || check_nval(io))
+ return FALSE;
+
+ sock = g_io_channel_unix_get_fd(io);
+
+ if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &sk_err, &len) < 0)
+ err = -errno;
+ else
+ err = -sk_err;
+
+ if (err < 0)
+ ERROR_FAILED(&gerr, "connect error", -err);
+
+ conn->connect(io, gerr, conn->user_data);
+
+ g_clear_error(&gerr);
+
+ return FALSE;
+}
+
+static gboolean server_cb(GIOChannel *io, GIOCondition cond,
+ gpointer user_data)
+{
+ struct server *server = user_data;
+ int srv_sock, cli_sock;
+ GIOChannel *cli_io;
+
+ /* If the user closed the server */
+ if ((cond & G_IO_NVAL) || check_nval(io))
+ return FALSE;
+
+ srv_sock = g_io_channel_unix_get_fd(io);
+
+ cli_sock = accept(srv_sock, NULL, NULL);
+ if (cli_sock < 0)
+ return TRUE;
+
+ cli_io = g_io_channel_unix_new(cli_sock);
+
+ g_io_channel_set_close_on_unref(cli_io, TRUE);
+ g_io_channel_set_flags(cli_io, G_IO_FLAG_NONBLOCK, NULL);
+
+ if (server->confirm)
+ server->confirm(cli_io, server->user_data);
+ else
+ server->connect(cli_io, NULL, server->user_data);
+
+ g_io_channel_unref(cli_io);
+
+ return TRUE;
+}
+
+static void server_add(GIOChannel *io, BtIOConnect connect,
+ BtIOConfirm confirm, gpointer user_data,
+ GDestroyNotify destroy)
+{
+ struct server *server;
+ GIOCondition cond;
+
+ server = g_new0(struct server, 1);
+ server->connect = connect;
+ server->confirm = confirm;
+ server->user_data = user_data;
+ server->destroy = destroy;
+
+ cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
+ g_io_add_watch_full(io, G_PRIORITY_DEFAULT, cond, server_cb, server,
+ (GDestroyNotify) server_remove);
+}
+
+static void connect_add(GIOChannel *io, BtIOConnect connect,
+ gpointer user_data, GDestroyNotify destroy)
+{
+ struct connect *conn;
+ GIOCondition cond;
+
+ conn = g_new0(struct connect, 1);
+ conn->connect = connect;
+ conn->user_data = user_data;
+ conn->destroy = destroy;
+
+ cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
+ g_io_add_watch_full(io, G_PRIORITY_DEFAULT, cond, connect_cb, conn,
+ (GDestroyNotify) connect_remove);
+}
+
+static void accept_add(GIOChannel *io, BtIOConnect connect, gpointer user_data,
+ GDestroyNotify destroy)
+{
+ struct accept *accept;
+ GIOCondition cond;
+
+ accept = g_new0(struct accept, 1);
+ accept->connect = connect;
+ accept->user_data = user_data;
+ accept->destroy = destroy;
+
+ cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
+ g_io_add_watch_full(io, G_PRIORITY_DEFAULT, cond, accept_cb, accept,
+ (GDestroyNotify) accept_remove);
+}
+
+static int l2cap_bind(int sock, const bdaddr_t *src, uint8_t src_type,
+ uint16_t psm, uint16_t cid, GError **err)
+{
+ struct sockaddr_l2 addr;
+
+ memset(&addr, 0, sizeof(addr));
+ addr.l2_family = AF_BLUETOOTH;
+ bacpy(&addr.l2_bdaddr, src);
+
+ if (cid)
+ addr.l2_cid = htobs(cid);
+ else
+ addr.l2_psm = htobs(psm);
+
+ addr.l2_bdaddr_type = src_type;
+
+ if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ int error = -errno;
+ ERROR_FAILED(err, "l2cap_bind", errno);
+ return error;
+ }
+
+ return 0;
+}
+
+static int l2cap_connect(int sock, const bdaddr_t *dst, uint8_t dst_type,
+ uint16_t psm, uint16_t cid)
+{
+ int err;
+ struct sockaddr_l2 addr;
+
+ memset(&addr, 0, sizeof(addr));
+ addr.l2_family = AF_BLUETOOTH;
+ bacpy(&addr.l2_bdaddr, dst);
+ if (cid)
+ addr.l2_cid = htobs(cid);
+ else
+ addr.l2_psm = htobs(psm);
+
+ addr.l2_bdaddr_type = dst_type;
+
+ err = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
+ if (err < 0 && !(errno == EAGAIN || errno == EINPROGRESS))
+ return -errno;
+
+ return 0;
+}
+
+static int l2cap_set_master(int sock, int master)
+{
+ int flags;
+ socklen_t len;
+
+ len = sizeof(flags);
+ if (getsockopt(sock, SOL_L2CAP, L2CAP_LM, &flags, &len) < 0)
+ return -errno;
+
+ if (master) {
+ if (flags & L2CAP_LM_MASTER)
+ return 0;
+ flags |= L2CAP_LM_MASTER;
+ } else {
+ if (!(flags & L2CAP_LM_MASTER))
+ return 0;
+ flags &= ~L2CAP_LM_MASTER;
+ }
+
+ if (setsockopt(sock, SOL_L2CAP, L2CAP_LM, &flags, sizeof(flags)) < 0)
+ return -errno;
+
+ return 0;
+}
+
+static int l2cap_set_lm(int sock, int level)
+{
+ int lm_map[] = {
+ 0,
+ L2CAP_LM_AUTH,
+ L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT,
+ L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT | L2CAP_LM_SECURE,
+ }, opt = lm_map[level];
+
+ if (setsockopt(sock, SOL_L2CAP, L2CAP_LM, &opt, sizeof(opt)) < 0)
+ return -errno;
+
+ return 0;
+}
+
+static gboolean set_sec_level(int sock, BtIOType type, int level, GError **err)
+{
+ struct bt_security sec;
+ int ret;
+
+ if (level < BT_SECURITY_LOW || level > BT_SECURITY_HIGH) {
+ g_set_error(err, BT_IO_ERROR, EINVAL,
+ "Valid security level range is %d-%d",
+ BT_SECURITY_LOW, BT_SECURITY_HIGH);
+ return FALSE;
+ }
+
+ memset(&sec, 0, sizeof(sec));
+ sec.level = level;
+
+ if (setsockopt(sock, SOL_BLUETOOTH, BT_SECURITY, &sec,
+ sizeof(sec)) == 0)
+ return TRUE;
+
+ if (errno != ENOPROTOOPT) {
+ ERROR_FAILED(err, "setsockopt(BT_SECURITY)", errno);
+ return FALSE;
+ }
+
+ if (type == BT_IO_L2CAP) {
+ ERROR_FAILED(err, "unsupported type for Bluetooth LE", EPERM);
+ return FALSE;
+ }
+ ret = l2cap_set_lm(sock, level);
+
+ if (ret < 0) {
+ ERROR_FAILED(err, "setsockopt(LM)", -ret);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static int l2cap_get_lm(int sock, int *sec_level)
+{
+ int opt;
+ socklen_t len;
+
+ len = sizeof(opt);
+ if (getsockopt(sock, SOL_L2CAP, L2CAP_LM, &opt, &len) < 0)
+ return -errno;
+
+ *sec_level = 0;
+
+ if (opt & L2CAP_LM_AUTH)
+ *sec_level = BT_SECURITY_LOW;
+ if (opt & L2CAP_LM_ENCRYPT)
+ *sec_level = BT_SECURITY_MEDIUM;
+ if (opt & L2CAP_LM_SECURE)
+ *sec_level = BT_SECURITY_HIGH;
+
+ return 0;
+}
+
+static gboolean get_sec_level(int sock, BtIOType type, int *level,
+ GError **err)
+{
+ struct bt_security sec;
+ socklen_t len;
+ int ret;
+
+ memset(&sec, 0, sizeof(sec));
+ len = sizeof(sec);
+ if (getsockopt(sock, SOL_BLUETOOTH, BT_SECURITY, &sec, &len) == 0) {
+ *level = sec.level;
+ return TRUE;
+ }
+
+ if (errno != ENOPROTOOPT) {
+ ERROR_FAILED(err, "getsockopt(BT_SECURITY)", errno);
+ return FALSE;
+ }
+
+ if (type == BT_IO_L2CAP) {
+ ERROR_FAILED(err, "unsupported type for Bluetooth LE", EPERM);
+ return FALSE;
+ }
+ ret = l2cap_get_lm(sock, level);
+
+ if (ret < 0) {
+ ERROR_FAILED(err, "getsockopt(LM)", -ret);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static int l2cap_set_flushable(int sock, gboolean flushable)
+{
+ int f;
+
+ f = flushable;
+ if (setsockopt(sock, SOL_BLUETOOTH, BT_FLUSHABLE, &f, sizeof(f)) < 0)
+ return -errno;
+
+ return 0;
+}
+
+static int set_priority(int sock, uint32_t prio)
+{
+ if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)) < 0)
+ return -errno;
+
+ return 0;
+}
+
+static gboolean get_key_size(int sock, int *size, GError **err)
+{
+ struct bt_security sec;
+ socklen_t len;
+
+ memset(&sec, 0, sizeof(sec));
+ len = sizeof(sec);
+ if (getsockopt(sock, SOL_BLUETOOTH, BT_SECURITY, &sec, &len) == 0) {
+ *size = sec.key_size;
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static gboolean set_l2opts(int sock, uint16_t imtu, uint16_t omtu,
+ uint8_t mode, GError **err)
+{
+ struct l2cap_options l2o;
+ socklen_t len;
+
+ memset(&l2o, 0, sizeof(l2o));
+ len = sizeof(l2o);
+ if (getsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &len) < 0) {
+ ERROR_FAILED(err, "getsockopt(L2CAP_OPTIONS)", errno);
+ return FALSE;
+ }
+
+ if (imtu)
+ l2o.imtu = imtu;
+ if (omtu)
+ l2o.omtu = omtu;
+ if (mode)
+ l2o.mode = mode;
+
+ if (setsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, &l2o, sizeof(l2o)) < 0) {
+ ERROR_FAILED(err, "setsockopt(L2CAP_OPTIONS)", errno);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean set_le_imtu(int sock, uint16_t imtu, GError **err)
+{
+ if (setsockopt(sock, SOL_BLUETOOTH, BT_RCVMTU, &imtu,
+ sizeof(imtu)) < 0) {
+ ERROR_FAILED(err, "setsockopt(BT_RCVMTU)", errno);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean l2cap_set(int sock, uint8_t src_type, int sec_level,
+ uint16_t imtu, uint16_t omtu, uint8_t mode,
+ int master, int flushable, uint32_t priority,
+ GError **err)
+{
+ if (imtu || omtu || mode) {
+ gboolean ret;
+
+ if (src_type == BDADDR_BREDR)
+ ret = set_l2opts(sock, imtu, omtu, mode, err);
+ else
+ ret = set_le_imtu(sock, imtu, err);
+
+ if (!ret)
+ return ret;
+ }
+
+ if (master >= 0 && l2cap_set_master(sock, master) < 0) {
+ ERROR_FAILED(err, "l2cap_set_master", errno);
+ return FALSE;
+ }
+
+ if (flushable >= 0 && l2cap_set_flushable(sock, flushable) < 0) {
+ ERROR_FAILED(err, "l2cap_set_flushable", errno);
+ return FALSE;
+ }
+
+ if (priority > 0 && set_priority(sock, priority) < 0) {
+ ERROR_FAILED(err, "set_priority", errno);
+ return FALSE;
+ }
+
+ if (sec_level && !set_sec_level(sock, BT_IO_L2CAP, sec_level, err))
+ return FALSE;
+
+ return TRUE;
+}
+
+static gboolean parse_set_opts(struct set_opts *opts, GError **err,
+ BtIOOption opt1, va_list args)
+{
+ BtIOOption opt = opt1;
+ const char *str;
+
+ memset(opts, 0, sizeof(*opts));
+
+ /* Set defaults */
+ opts->type = BT_IO_L2CAP;
+ opts->defer = DEFAULT_DEFER_TIMEOUT;
+ opts->master = -1;
+ opts->mode = L2CAP_MODE_BASIC;
+ opts->flushable = -1;
+ opts->priority = 0;
+ opts->src_type = BDADDR_BREDR;
+ opts->dst_type = BDADDR_BREDR;
+
+ while (opt != BT_IO_OPT_INVALID) {
+ switch (opt) {
+ case BT_IO_OPT_SOURCE:
+ str = va_arg(args, const char *);
+ str2ba(str, &opts->src);
+ break;
+ case BT_IO_OPT_SOURCE_BDADDR:
+ bacpy(&opts->src, va_arg(args, const bdaddr_t *));
+ break;
+ case BT_IO_OPT_SOURCE_TYPE:
+ opts->src_type = va_arg(args, int);
+ break;
+ case BT_IO_OPT_DEST:
+ str2ba(va_arg(args, const char *), &opts->dst);
+ break;
+ case BT_IO_OPT_DEST_BDADDR:
+ bacpy(&opts->dst, va_arg(args, const bdaddr_t *));
+ break;
+ case BT_IO_OPT_DEST_TYPE:
+ opts->dst_type = va_arg(args, int);
+ break;
+ case BT_IO_OPT_DEFER_TIMEOUT:
+ opts->defer = va_arg(args, int);
+ break;
+ case BT_IO_OPT_SEC_LEVEL:
+ opts->sec_level = va_arg(args, int);
+ break;
+ case BT_IO_OPT_CHANNEL:
+ opts->type = BT_IO_RFCOMM;
+ opts->channel = va_arg(args, int);
+ break;
+ case BT_IO_OPT_PSM:
+ opts->type = BT_IO_L2CAP;
+ opts->psm = va_arg(args, int);
+ break;
+ case BT_IO_OPT_CID:
+ opts->type = BT_IO_L2CAP;
+ opts->cid = va_arg(args, int);
+ break;
+ case BT_IO_OPT_MTU:
+ opts->mtu = va_arg(args, int);
+ opts->imtu = opts->mtu;
+ opts->omtu = opts->mtu;
+ break;
+ case BT_IO_OPT_OMTU:
+ opts->omtu = va_arg(args, int);
+ if (!opts->mtu)
+ opts->mtu = opts->omtu;
+ break;
+ case BT_IO_OPT_IMTU:
+ opts->imtu = va_arg(args, int);
+ if (!opts->mtu)
+ opts->mtu = opts->imtu;
+ break;
+ case BT_IO_OPT_MASTER:
+ opts->master = va_arg(args, gboolean);
+ break;
+ case BT_IO_OPT_MODE:
+ opts->mode = va_arg(args, int);
+ break;
+ case BT_IO_OPT_FLUSHABLE:
+ opts->flushable = va_arg(args, gboolean);
+ break;
+ case BT_IO_OPT_PRIORITY:
+ opts->priority = va_arg(args, int);
+ break;
+ case BT_IO_OPT_VOICE:
+ opts->voice = va_arg(args, int);
+ break;
+ default:
+ g_set_error(err, BT_IO_ERROR, EINVAL,
+ "Unknown option %d", opt);
+ return FALSE;
+ }
+
+ opt = va_arg(args, int);
+ }
+
+ return TRUE;
+}
+
+static gboolean get_peers(int sock, struct sockaddr *src, struct sockaddr *dst,
+ socklen_t len, GError **err)
+{
+ socklen_t olen;
+
+ memset(src, 0, len);
+ olen = len;
+ if (getsockname(sock, src, &olen) < 0) {
+ ERROR_FAILED(err, "getsockname", errno);
+ return FALSE;
+ }
+
+ memset(dst, 0, len);
+ olen = len;
+ if (getpeername(sock, dst, &olen) < 0) {
+ ERROR_FAILED(err, "getpeername", errno);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static int l2cap_get_info(int sock, uint16_t *handle, uint8_t *dev_class)
+{
+ struct l2cap_conninfo info;
+ socklen_t len;
+
+ len = sizeof(info);
+ if (getsockopt(sock, SOL_L2CAP, L2CAP_CONNINFO, &info, &len) < 0)
+ return -errno;
+
+ if (handle)
+ *handle = info.hci_handle;
+
+ if (dev_class)
+ memcpy(dev_class, info.dev_class, 3);
+
+ return 0;
+}
+
+static int l2cap_get_flushable(int sock, gboolean *flushable)
+{
+ int f;
+ socklen_t len;
+
+ f = 0;
+ len = sizeof(f);
+ if (getsockopt(sock, SOL_BLUETOOTH, BT_FLUSHABLE, &f, &len) < 0)
+ return -errno;
+
+ if (f)
+ *flushable = TRUE;
+ else
+ *flushable = FALSE;
+
+ return 0;
+}
+
+static int get_priority(int sock, uint32_t *prio)
+{
+ socklen_t len;
+
+ len = sizeof(*prio);
+ if (getsockopt(sock, SOL_SOCKET, SO_PRIORITY, prio, &len) < 0)
+ return -errno;
+
+ return 0;
+}
+
+static gboolean l2cap_get(int sock, GError **err, BtIOOption opt1,
+ va_list args)
+{
+ BtIOOption opt = opt1;
+ struct sockaddr_l2 src, dst;
+ struct l2cap_options l2o;
+ int flags;
+ uint8_t dev_class[3];
+ uint16_t handle;
+ socklen_t len;
+ gboolean flushable = FALSE;
+ uint32_t priority;
+
+ if (!get_peers(sock, (struct sockaddr *) &src,
+ (struct sockaddr *) &dst, sizeof(src), err))
+ return FALSE;
+
+ memset(&l2o, 0, sizeof(l2o));
+
+ if (src.l2_bdaddr_type != BDADDR_BREDR) {
+ len = sizeof(l2o.imtu);
+ if (getsockopt(sock, SOL_BLUETOOTH, BT_RCVMTU,
+ &l2o.imtu, &len) == 0)
+ goto parse_opts;
+
+ /* Non-LE CoC enabled kernels will return one of these
+ * in which case we need to fall back to L2CAP_OPTIONS.
+ */
+ if (errno != EPROTONOSUPPORT && errno != ENOPROTOOPT) {
+ ERROR_FAILED(err, "getsockopt(BT_RCVMTU)", errno);
+ return FALSE;
+ }
+ }
+
+ len = sizeof(l2o);
+ if (getsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &len) < 0) {
+ ERROR_FAILED(err, "getsockopt(L2CAP_OPTIONS)", errno);
+ return FALSE;
+ }
+
+parse_opts:
+ while (opt != BT_IO_OPT_INVALID) {
+ switch (opt) {
+ case BT_IO_OPT_SOURCE:
+ ba2str(&src.l2_bdaddr, va_arg(args, char *));
+ break;
+ case BT_IO_OPT_SOURCE_BDADDR:
+ bacpy(va_arg(args, bdaddr_t *), &src.l2_bdaddr);
+ break;
+ case BT_IO_OPT_DEST:
+ ba2str(&dst.l2_bdaddr, va_arg(args, char *));
+ break;
+ case BT_IO_OPT_DEST_BDADDR:
+ bacpy(va_arg(args, bdaddr_t *), &dst.l2_bdaddr);
+ break;
+ case BT_IO_OPT_DEST_TYPE:
+ *(va_arg(args, uint8_t *)) = dst.l2_bdaddr_type;
+ break;
+ case BT_IO_OPT_DEFER_TIMEOUT:
+ len = sizeof(int);
+ if (getsockopt(sock, SOL_BLUETOOTH, BT_DEFER_SETUP,
+ va_arg(args, int *), &len) < 0) {
+ ERROR_FAILED(err, "getsockopt(DEFER_SETUP)",
+ errno);
+ return FALSE;
+ }
+ break;
+ case BT_IO_OPT_SEC_LEVEL:
+ if (!get_sec_level(sock, BT_IO_L2CAP,
+ va_arg(args, int *), err))
+ return FALSE;
+ break;
+ case BT_IO_OPT_KEY_SIZE:
+ if (!get_key_size(sock, va_arg(args, int *), err))
+ return FALSE;
+ break;
+ case BT_IO_OPT_PSM:
+ *(va_arg(args, uint16_t *)) = src.l2_psm ?
+ btohs(src.l2_psm) : btohs(dst.l2_psm);
+ break;
+ case BT_IO_OPT_CID:
+ *(va_arg(args, uint16_t *)) = src.l2_cid ?
+ btohs(src.l2_cid) : btohs(dst.l2_cid);
+ break;
+ case BT_IO_OPT_OMTU:
+ if (src.l2_bdaddr_type == BDADDR_BREDR) {
+ *(va_arg(args, uint16_t *)) = l2o.omtu;
+ break;
+ }
+
+ if (getsockopt(sock, SOL_BLUETOOTH, BT_SNDMTU,
+ &l2o.omtu, &len) < 0) {
+ ERROR_FAILED(err, "getsockopt(BT_RCVMTU)",
+ errno);
+ return FALSE;
+ }
+
+ *(va_arg(args, uint16_t *)) = l2o.omtu;
+ break;
+ case BT_IO_OPT_IMTU:
+ *(va_arg(args, uint16_t *)) = l2o.imtu;
+ break;
+ case BT_IO_OPT_MASTER:
+ len = sizeof(flags);
+ if (getsockopt(sock, SOL_L2CAP, L2CAP_LM, &flags,
+ &len) < 0) {
+ ERROR_FAILED(err, "getsockopt(L2CAP_LM)",
+ errno);
+ return FALSE;
+ }
+ *(va_arg(args, gboolean *)) =
+ (flags & L2CAP_LM_MASTER) ? TRUE : FALSE;
+ break;
+ case BT_IO_OPT_HANDLE:
+ if (l2cap_get_info(sock, &handle, dev_class) < 0) {
+ ERROR_FAILED(err, "L2CAP_CONNINFO", errno);
+ return FALSE;
+ }
+ *(va_arg(args, uint16_t *)) = handle;
+ break;
+ case BT_IO_OPT_CLASS:
+ if (l2cap_get_info(sock, &handle, dev_class) < 0) {
+ ERROR_FAILED(err, "L2CAP_CONNINFO", errno);
+ return FALSE;
+ }
+ memcpy(va_arg(args, uint8_t *), dev_class, 3);
+ break;
+ case BT_IO_OPT_MODE:
+ *(va_arg(args, uint8_t *)) = l2o.mode;
+ break;
+ case BT_IO_OPT_FLUSHABLE:
+ if (l2cap_get_flushable(sock, &flushable) < 0) {
+ ERROR_FAILED(err, "get_flushable", errno);
+ return FALSE;
+ }
+ *(va_arg(args, gboolean *)) = flushable;
+ break;
+ case BT_IO_OPT_PRIORITY:
+ if (get_priority(sock, &priority) < 0) {
+ ERROR_FAILED(err, "get_priority", errno);
+ return FALSE;
+ }
+ *(va_arg(args, uint32_t *)) = priority;
+ break;
+ default:
+ g_set_error(err, BT_IO_ERROR, EINVAL,
+ "Unknown option %d", opt);
+ return FALSE;
+ }
+
+ opt = va_arg(args, int);
+ }
+
+ return TRUE;
+}
+
+static gboolean get_valist(GIOChannel *io, BtIOType type, GError **err,
+ BtIOOption opt1, va_list args)
+{
+ int sock;
+
+ sock = g_io_channel_unix_get_fd(io);
+
+ switch (type) {
+ case BT_IO_L2CAP:
+ return l2cap_get(sock, err, opt1, args);
+ case BT_IO_SCO:
+ case BT_IO_RFCOMM:
+ g_set_error(err, BT_IO_ERROR, EINVAL,
+ "Unsupported BtIO type %d for Bluetooth LE", type);
+ return FALSE;
+ default:
+ g_set_error(err, BT_IO_ERROR, EINVAL,
+ "Unknown BtIO type %d", type);
+ return FALSE;
+ }
+}
+
+gboolean bt_io_accept(GIOChannel *io, BtIOConnect connect, gpointer user_data,
+ GDestroyNotify destroy, GError **err)
+{
+ int sock;
+ char c;
+ struct pollfd pfd;
+
+ sock = g_io_channel_unix_get_fd(io);
+
+ memset(&pfd, 0, sizeof(pfd));
+ pfd.fd = sock;
+ pfd.events = POLLOUT;
+
+ if (poll(&pfd, 1, 0) < 0) {
+ ERROR_FAILED(err, "poll", errno);
+ return FALSE;
+ }
+
+ if (!(pfd.revents & POLLOUT)) {
+ if (read(sock, &c, 1) < 0) {
+ ERROR_FAILED(err, "read", errno);
+ return FALSE;
+ }
+ }
+
+ accept_add(io, connect, user_data, destroy);
+
+ return TRUE;
+}
+
+gboolean bt_io_set(GIOChannel *io, GError **err, BtIOOption opt1, ...)
+{
+ va_list args;
+ gboolean ret;
+ struct set_opts opts;
+ int sock;
+ BtIOType type;
+
+ va_start(args, opt1);
+ ret = parse_set_opts(&opts, err, opt1, args);
+ va_end(args);
+
+ if (!ret)
+ return ret;
+
+ type = bt_io_get_type(io, err);
+ if (type == BT_IO_INVALID)
+ return FALSE;
+
+ sock = g_io_channel_unix_get_fd(io);
+
+ switch (type) {
+ case BT_IO_L2CAP:
+ return l2cap_set(sock, opts.src_type, opts.sec_level, opts.imtu,
+ opts.omtu, opts.mode, opts.master,
+ opts.flushable, opts.priority, err);
+ case BT_IO_SCO:
+ case BT_IO_RFCOMM:
+ g_set_error(err, BT_IO_ERROR, EINVAL,
+ "Unsupported BtIO type %d for Bluetooth LE", type);
+ return FALSE;
+ default:
+ g_set_error(err, BT_IO_ERROR, EINVAL,
+ "Unknown BtIO type %d", type);
+ return FALSE;
+ }
+
+}
+
+gboolean bt_io_get(GIOChannel *io, GError **err, BtIOOption opt1, ...)
+{
+ va_list args;
+ gboolean ret;
+ BtIOType type;
+
+ type = bt_io_get_type(io, err);
+ if (type == BT_IO_INVALID)
+ return FALSE;
+
+ va_start(args, opt1);
+ ret = get_valist(io, type, err, opt1, args);
+ va_end(args);
+
+ return ret;
+}
+
+static GIOChannel *create_io(gboolean server, struct set_opts *opts,
+ GError **err)
+{
+ int sock;
+ GIOChannel *io;
+
+ switch (opts->type) {
+ case BT_IO_L2CAP:
+ sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
+ if (sock < 0) {
+ ERROR_FAILED(err, "socket(SEQPACKET, L2CAP)", errno);
+ return NULL;
+ }
+ if (l2cap_bind(sock, &opts->src, opts->src_type,
+ server ? opts->psm : 0, opts->cid, err) < 0)
+ goto failed;
+ if (!l2cap_set(sock, opts->src_type, opts->sec_level,
+ opts->imtu, opts->omtu, opts->mode,
+ opts->master, opts->flushable, opts->priority,
+ err))
+ goto failed;
+ break;
+ case BT_IO_SCO:
+ case BT_IO_RFCOMM:
+ g_set_error(err, BT_IO_ERROR, EINVAL,
+ "Unsupported BtIO type %d for Bluetooth LE", opts->type);
+ return NULL;
+ default:
+ g_set_error(err, BT_IO_ERROR, EINVAL,
+ "Unknown BtIO type %d", opts->type);
+ return NULL;
+ }
+
+ io = g_io_channel_unix_new(sock);
+
+ g_io_channel_set_close_on_unref(io, TRUE);
+ g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
+
+ return io;
+
+failed:
+ close(sock);
+
+ return NULL;
+}
+
+GIOChannel *bt_io_connect(BtIOConnect connect, gpointer user_data,
+ GDestroyNotify destroy, GError **gerr,
+ BtIOOption opt1, ...)
+{
+ GIOChannel *io;
+ va_list args;
+ struct set_opts opts;
+ int err, sock;
+ gboolean ret;
+
+ va_start(args, opt1);
+ ret = parse_set_opts(&opts, gerr, opt1, args);
+ va_end(args);
+
+ if (ret == FALSE)
+ return NULL;
+
+ io = create_io(FALSE, &opts, gerr);
+ if (io == NULL)
+ return NULL;
+
+ sock = g_io_channel_unix_get_fd(io);
+
+ switch (opts.type) {
+ case BT_IO_L2CAP:
+ err = l2cap_connect(sock, &opts.dst, opts.dst_type,
+ opts.psm, opts.cid);
+ break;
+ case BT_IO_SCO:
+ case BT_IO_RFCOMM:
+ g_set_error(gerr, BT_IO_ERROR, EINVAL,
+ "Unsupported BtIO type %d for Bluetooth LE", opts.type);
+ return NULL;
+ default:
+ g_set_error(gerr, BT_IO_ERROR, EINVAL,
+ "Unknown BtIO type %d", opts.type);
+ return NULL;
+ }
+
+ if (err < 0) {
+ ERROR_FAILED(gerr, "connect", -err);
+ g_io_channel_unref(io);
+ return NULL;
+ }
+
+ connect_add(io, connect, user_data, destroy);
+
+ return io;
+}
+
+GIOChannel *bt_io_listen(BtIOConnect connect, BtIOConfirm confirm,
+ gpointer user_data, GDestroyNotify destroy,
+ GError **err, BtIOOption opt1, ...)
+{
+ GIOChannel *io;
+ va_list args;
+ struct set_opts opts;
+ int sock;
+ gboolean ret;
+
+ va_start(args, opt1);
+ ret = parse_set_opts(&opts, err, opt1, args);
+ va_end(args);
+
+ if (ret == FALSE)
+ return NULL;
+
+ io = create_io(TRUE, &opts, err);
+ if (io == NULL)
+ return NULL;
+
+ sock = g_io_channel_unix_get_fd(io);
+
+ if (confirm)
+ setsockopt(sock, SOL_BLUETOOTH, BT_DEFER_SETUP, &opts.defer,
+ sizeof(opts.defer));
+
+ if (listen(sock, 5) < 0) {
+ ERROR_FAILED(err, "listen", errno);
+ g_io_channel_unref(io);
+ return NULL;
+ }
+
+ server_add(io, connect, confirm, user_data, destroy);
+
+ return io;
+}
+
+GQuark bt_io_error_quark(void)
+{
+ return g_quark_from_static_string("bt-io-error-quark");
+}
diff --git a/drive-sdk/deps/bzle/src/gatt.c b/drive-sdk/deps/bzle/src/gatt.c
new file mode 100644
index 0000000..3c0fab0
--- /dev/null
+++ b/drive-sdk/deps/bzle/src/gatt.c
@@ -0,0 +1,929 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2010 Nokia Corporation
+ * Copyright (C) 2010 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 <stdint.h>
+#include <stdlib.h>
+#include <glib.h>
+#include <bzle/bluetooth/sdp.h>
+#include <bzle/bluetooth/sdp_lib.h>
+
+#include <bzle/bluetooth/uuid.h>
+#include <bzle/gatt/att.h>
+#include <bzle/gatt/gattrib.h>
+#include <bzle/gatt/gatt.h>
+
+struct discover_primary {
+ GAttrib *attrib;
+ bt_uuid_t uuid;
+ GSList *primaries;
+ gatt_cb_t cb;
+ void *user_data;
+};
+
+/* Used for the Included Services Discovery (ISD) procedure */
+struct included_discovery {
+ GAttrib *attrib;
+ int refs;
+ int err;
+ uint16_t end_handle;
+ GSList *includes;
+ gatt_cb_t cb;
+ void *user_data;
+};
+
+struct included_uuid_query {
+ struct included_discovery *isd;
+ struct gatt_included *included;
+};
+
+struct discover_char {
+ GAttrib *attrib;
+ bt_uuid_t *uuid;
+ uint16_t end;
+ GSList *characteristics;
+ gatt_cb_t cb;
+ void *user_data;
+};
+
+static void discover_primary_free(struct discover_primary *dp)
+{
+ g_slist_free(dp->primaries);
+ g_attrib_unref(dp->attrib);
+ g_free(dp);
+}
+
+static struct included_discovery *isd_ref(struct included_discovery *isd)
+{
+ __sync_fetch_and_add(&isd->refs, 1);
+
+ return isd;
+}
+
+static void isd_unref(struct included_discovery *isd)
+{
+ if (__sync_sub_and_fetch(&isd->refs, 1) > 0)
+ return;
+
+ if (isd->err)
+ isd->cb(isd->err, NULL, isd->user_data);
+ else
+ isd->cb(isd->err, isd->includes, isd->user_data);
+
+ g_slist_free_full(isd->includes, g_free);
+ g_attrib_unref(isd->attrib);
+ g_free(isd);
+}
+
+static void discover_char_free(struct discover_char *dc)
+{
+ g_slist_free_full(dc->characteristics, g_free);
+ g_attrib_unref(dc->attrib);
+ g_free(dc->uuid);
+ g_free(dc);
+}
+
+static guint16 encode_discover_primary(uint16_t start, uint16_t end,
+ bt_uuid_t *uuid, uint8_t *pdu, size_t len)
+{
+ bt_uuid_t prim;
+ guint16 plen;
+
+ bt_uuid16_create(&prim, GATT_PRIM_SVC_UUID);
+
+ if (uuid == NULL) {
+ /* Discover all primary services */
+ plen = enc_read_by_grp_req(start, end, &prim, pdu, len);
+ } else {
+ uint16_t u16;
+ uint128_t u128;
+ const void *value;
+ size_t vlen;
+
+ /* Discover primary service by service UUID */
+
+ if (uuid->type == BT_UUID16) {
+ u16 = htobs(uuid->value.u16);
+ value = &u16;
+ vlen = sizeof(u16);
+ } else {
+ htob128(&uuid->value.u128, &u128);
+ value = &u128;
+ vlen = sizeof(u128);
+ }
+
+ plen = enc_find_by_type_req(start, end, &prim, value, vlen,
+ pdu, len);
+ }
+
+ return plen;
+}
+
+static void primary_by_uuid_cb(guint8 status, const guint8 *ipdu,
+ guint16 iplen, gpointer user_data)
+
+{
+ struct discover_primary *dp = user_data;
+ GSList *ranges, *last;
+ struct att_range *range;
+ uint8_t *buf;
+ guint16 oplen;
+ int err = 0;
+ size_t buflen;
+
+ if (status) {
+ err = status == ATT_ECODE_ATTR_NOT_FOUND ? 0 : status;
+ goto done;
+ }
+
+ ranges = dec_find_by_type_resp(ipdu, iplen);
+ if (ranges == NULL)
+ goto done;
+
+ dp->primaries = g_slist_concat(dp->primaries, ranges);
+
+ last = g_slist_last(ranges);
+ range = last->data;
+
+ if (range->end == 0xffff)
+ goto done;
+
+ buf = g_attrib_get_buffer(dp->attrib, &buflen);
+ oplen = encode_discover_primary(range->end + 1, 0xffff, &dp->uuid,
+ buf, buflen);
+
+ if (oplen == 0)
+ goto done;
+
+ g_attrib_send(dp->attrib, 0, buf, oplen, primary_by_uuid_cb, dp, NULL);
+ return;
+
+done:
+ dp->cb(err, dp->primaries, dp->user_data);
+ discover_primary_free(dp);
+}
+
+static void primary_all_cb(guint8 status, const guint8 *ipdu, guint16 iplen,
+ gpointer user_data)
+{
+ struct discover_primary *dp = user_data;
+ struct att_data_list *list;
+ unsigned int i, err;
+ uint16_t start, end;
+
+ if (status) {
+ err = status == ATT_ECODE_ATTR_NOT_FOUND ? 0 : status;
+ goto done;
+ }
+
+ list = dec_read_by_grp_resp(ipdu, iplen);
+ if (list == NULL) {
+ err = ATT_ECODE_IO;
+ goto done;
+ }
+
+ for (i = 0, end = 0; i < list->num; i++) {
+ const uint8_t *data = list->data[i];
+ struct gatt_primary *primary;
+ bt_uuid_t uuid;
+
+ start = att_get_u16(&data[0]);
+ end = att_get_u16(&data[2]);
+
+ if (list->len == 6) {
+ bt_uuid_t uuid16 = att_get_uuid16(&data[4]);
+ bt_uuid_to_uuid128(&uuid16, &uuid);
+ } else if (list->len == 20) {
+ uuid = att_get_uuid128(&data[4]);
+ } else {
+ /* Skipping invalid data */
+ continue;
+ }
+
+ primary = g_try_new0(struct gatt_primary, 1);
+ if (!primary) {
+ att_data_list_free(list);
+ err = ATT_ECODE_INSUFF_RESOURCES;
+ goto done;
+ }
+ primary->range.start = start;
+ primary->range.end = end;
+ bt_uuid_to_string(&uuid, primary->uuid, sizeof(primary->uuid));
+ dp->primaries = g_slist_append(dp->primaries, primary);
+ }
+
+ att_data_list_free(list);
+ err = 0;
+
+ if (end != 0xffff) {
+ size_t buflen;
+ uint8_t *buf = g_attrib_get_buffer(dp->attrib, &buflen);
+ guint16 oplen = encode_discover_primary(end + 1, 0xffff, NULL,
+ buf, buflen);
+
+ g_attrib_send(dp->attrib, 0, buf, oplen, primary_all_cb,
+ dp, NULL);
+
+ return;
+ }
+
+done:
+ dp->cb(err, dp->primaries, dp->user_data);
+ discover_primary_free(dp);
+}
+
+guint gatt_discover_primary(GAttrib *attrib, bt_uuid_t *uuid, gatt_cb_t func,
+ gpointer user_data)
+{
+ struct discover_primary *dp;
+ size_t buflen;
+ uint8_t *buf = g_attrib_get_buffer(attrib, &buflen);
+ GAttribResultFunc cb;
+ guint16 plen;
+
+ plen = encode_discover_primary(0x0001, 0xffff, uuid, buf, buflen);
+ if (plen == 0)
+ return 0;
+
+ dp = g_try_new0(struct discover_primary, 1);
+ if (dp == NULL)
+ return 0;
+
+ dp->attrib = g_attrib_ref(attrib);
+ dp->cb = func;
+ dp->user_data = user_data;
+
+ if (uuid) {
+ dp->uuid = *uuid;
+ cb = primary_by_uuid_cb;
+ } else
+ cb = primary_all_cb;
+
+ return g_attrib_send(attrib, 0, buf, plen, cb, dp, NULL);
+}
+
+static void resolve_included_uuid_cb(uint8_t status, const uint8_t *pdu,
+ uint16_t len, gpointer user_data)
+{
+ struct included_uuid_query *query = user_data;
+ struct included_discovery *isd = query->isd;
+ struct gatt_included *incl = query->included;
+ unsigned int err = status;
+ bt_uuid_t uuid;
+ size_t buflen;
+ uint8_t *buf;
+
+ if (err)
+ goto done;
+
+ buf = g_attrib_get_buffer(isd->attrib, &buflen);
+ if (dec_read_resp(pdu, len, buf, buflen) != 16) {
+ err = ATT_ECODE_IO;
+ goto done;
+ }
+
+ uuid = att_get_uuid128(buf);
+ bt_uuid_to_string(&uuid, incl->uuid, sizeof(incl->uuid));
+ isd->includes = g_slist_append(isd->includes, incl);
+
+done:
+ if (err)
+ g_free(incl);
+
+ if (isd->err == 0)
+ isd->err = err;
+
+ isd_unref(isd);
+
+ g_free(query);
+}
+
+static guint resolve_included_uuid(struct included_discovery *isd,
+ struct gatt_included *incl)
+{
+ struct included_uuid_query *query;
+ size_t buflen;
+ uint8_t *buf = g_attrib_get_buffer(isd->attrib, &buflen);
+ guint16 oplen = enc_read_req(incl->range.start, buf, buflen);
+
+ query = g_new0(struct included_uuid_query, 1);
+ query->isd = isd_ref(isd);
+ query->included = incl;
+
+ return g_attrib_send(isd->attrib, 0, buf, oplen,
+ resolve_included_uuid_cb, query, NULL);
+}
+
+static struct gatt_included *included_from_buf(const uint8_t *buf, gsize len)
+{
+ struct gatt_included *incl = g_new0(struct gatt_included, 1);
+
+ incl->handle = att_get_u16(&buf[0]);
+ incl->range.start = att_get_u16(&buf[2]);
+ incl->range.end = att_get_u16(&buf[4]);
+
+ if (len == 8) {
+ bt_uuid_t uuid128;
+ bt_uuid_t uuid16 = att_get_uuid16(&buf[6]);
+
+ bt_uuid_to_uuid128(&uuid16, &uuid128);
+ bt_uuid_to_string(&uuid128, incl->uuid, sizeof(incl->uuid));
+ }
+
+ return incl;
+}
+
+static void find_included_cb(uint8_t status, const uint8_t *pdu, uint16_t len,
+ gpointer user_data);
+
+static guint find_included(struct included_discovery *isd, uint16_t start)
+{
+ bt_uuid_t uuid;
+ size_t buflen;
+ uint8_t *buf = g_attrib_get_buffer(isd->attrib, &buflen);
+ guint16 oplen;
+
+ bt_uuid16_create(&uuid, GATT_INCLUDE_UUID);
+ oplen = enc_read_by_type_req(start, isd->end_handle, &uuid,
+ buf, buflen);
+
+ return g_attrib_send(isd->attrib, 0, buf, oplen, find_included_cb,
+ isd_ref(isd), (GDestroyNotify) isd_unref);
+}
+
+static void find_included_cb(uint8_t status, const uint8_t *pdu, uint16_t len,
+ gpointer user_data)
+{
+ struct included_discovery *isd = user_data;
+ uint16_t last_handle = isd->end_handle;
+ unsigned int err = status;
+ struct att_data_list *list;
+ int i;
+
+ if (err == ATT_ECODE_ATTR_NOT_FOUND)
+ err = 0;
+
+ if (status)
+ goto done;
+
+ list = dec_read_by_type_resp(pdu, len);
+ if (list == NULL) {
+ err = ATT_ECODE_IO;
+ goto done;
+ }
+
+ if (list->len != 6 && list->len != 8) {
+ err = ATT_ECODE_IO;
+ att_data_list_free(list);
+ goto done;
+ }
+
+ for (i = 0; i < list->num; i++) {
+ struct gatt_included *incl;
+
+ incl = included_from_buf(list->data[i], list->len);
+ last_handle = incl->handle;
+
+ /* 128 bit UUID, needs resolving */
+ if (list->len == 6) {
+ resolve_included_uuid(isd, incl);
+ continue;
+ }
+
+ isd->includes = g_slist_append(isd->includes, incl);
+ }
+
+ att_data_list_free(list);
+
+ if (last_handle < isd->end_handle)
+ find_included(isd, last_handle + 1);
+
+done:
+ if (isd->err == 0)
+ isd->err = err;
+}
+
+unsigned int gatt_find_included(GAttrib *attrib, uint16_t start, uint16_t end,
+ gatt_cb_t func, gpointer user_data)
+{
+ struct included_discovery *isd;
+
+ isd = g_new0(struct included_discovery, 1);
+ isd->attrib = g_attrib_ref(attrib);
+ isd->end_handle = end;
+ isd->cb = func;
+ isd->user_data = user_data;
+
+ return find_included(isd, start);
+}
+
+static void char_discovered_cb(guint8 status, const guint8 *ipdu, guint16 iplen,
+ gpointer user_data)
+{
+ struct discover_char *dc = user_data;
+ struct att_data_list *list;
+ unsigned int i, err = ATT_ECODE_ATTR_NOT_FOUND;
+ uint16_t last = 0;
+
+ if (status) {
+ err = status;
+ goto done;
+ }
+
+ list = dec_read_by_type_resp(ipdu, iplen);
+ if (list == NULL) {
+ err = ATT_ECODE_IO;
+ goto done;
+ }
+
+ for (i = 0; i < list->num; i++) {
+ uint8_t *value = list->data[i];
+ struct gatt_char *chars;
+ bt_uuid_t uuid;
+
+ last = att_get_u16(value);
+
+ if (list->len == 7) {
+ bt_uuid_t uuid16 = att_get_uuid16(&value[5]);
+ bt_uuid_to_uuid128(&uuid16, &uuid);
+ } else
+ uuid = att_get_uuid128(&value[5]);
+
+ if (dc->uuid && bt_uuid_cmp(dc->uuid, &uuid))
+ continue;
+
+ chars = g_try_new0(struct gatt_char, 1);
+ if (!chars) {
+ err = ATT_ECODE_INSUFF_RESOURCES;
+ goto done;
+ }
+
+ chars->handle = last;
+ chars->properties = value[2];
+ chars->value_handle = att_get_u16(&value[3]);
+ bt_uuid_to_string(&uuid, chars->uuid, sizeof(chars->uuid));
+ dc->characteristics = g_slist_append(dc->characteristics,
+ chars);
+ }
+
+ att_data_list_free(list);
+
+ if (last != 0 && (last + 1 < dc->end)) {
+ bt_uuid_t uuid;
+ guint16 oplen;
+ size_t buflen;
+ uint8_t *buf;
+
+ buf = g_attrib_get_buffer(dc->attrib, &buflen);
+
+ bt_uuid16_create(&uuid, GATT_CHARAC_UUID);
+
+ oplen = enc_read_by_type_req(last + 1, dc->end, &uuid, buf,
+ buflen);
+
+ if (oplen == 0)
+ return;
+
+ g_attrib_send(dc->attrib, 0, buf, oplen, char_discovered_cb,
+ dc, NULL);
+
+ return;
+ }
+
+done:
+ err = (dc->characteristics ? 0 : err);
+
+ dc->cb(err, dc->characteristics, dc->user_data);
+ discover_char_free(dc);
+}
+
+guint gatt_discover_char(GAttrib *attrib, uint16_t start, uint16_t end,
+ bt_uuid_t *uuid, gatt_cb_t func,
+ gpointer user_data)
+{
+ size_t buflen;
+ uint8_t *buf = g_attrib_get_buffer(attrib, &buflen);
+ struct discover_char *dc;
+ bt_uuid_t type_uuid;
+ guint16 plen;
+
+ bt_uuid16_create(&type_uuid, GATT_CHARAC_UUID);
+
+ plen = enc_read_by_type_req(start, end, &type_uuid, buf, buflen);
+ if (plen == 0)
+ return 0;
+
+ dc = g_try_new0(struct discover_char, 1);
+ if (dc == NULL)
+ return 0;
+
+ dc->attrib = g_attrib_ref(attrib);
+ dc->cb = func;
+ dc->user_data = user_data;
+ dc->end = end;
+ dc->uuid = g_memdup(uuid, sizeof(bt_uuid_t));
+
+ return g_attrib_send(attrib, 0, buf, plen, char_discovered_cb,
+ dc, NULL);
+}
+
+guint gatt_read_char_by_uuid(GAttrib *attrib, uint16_t start, uint16_t end,
+ bt_uuid_t *uuid, GAttribResultFunc func,
+ gpointer user_data)
+{
+ size_t buflen;
+ uint8_t *buf = g_attrib_get_buffer(attrib, &buflen);
+ guint16 plen;
+
+ plen = enc_read_by_type_req(start, end, uuid, buf, buflen);
+ if (plen == 0)
+ return 0;
+
+ return g_attrib_send(attrib, 0, buf, plen, func, user_data, NULL);
+}
+
+struct read_long_data {
+ GAttrib *attrib;
+ GAttribResultFunc func;
+ gpointer user_data;
+ guint8 *buffer;
+ guint16 size;
+ guint16 handle;
+ guint id;
+ int ref;
+};
+
+static void read_long_destroy(gpointer user_data)
+{
+ struct read_long_data *long_read = user_data;
+
+ if (__sync_sub_and_fetch(&long_read->ref, 1) > 0)
+ return;
+
+ if (long_read->buffer != NULL)
+ g_free(long_read->buffer);
+
+ g_free(long_read);
+}
+
+static void read_blob_helper(guint8 status, const guint8 *rpdu, guint16 rlen,
+ gpointer user_data)
+{
+ struct read_long_data *long_read = user_data;
+ uint8_t *buf;
+ size_t buflen;
+ guint8 *tmp;
+ guint16 plen;
+ guint id;
+
+ if (status != 0 || rlen == 1) {
+ status = 0;
+ goto done;
+ }
+
+ tmp = g_try_realloc(long_read->buffer, long_read->size + rlen - 1);
+
+ if (tmp == NULL) {
+ status = ATT_ECODE_INSUFF_RESOURCES;
+ goto done;
+ }
+
+ memcpy(&tmp[long_read->size], &rpdu[1], rlen - 1);
+ long_read->buffer = tmp;
+ long_read->size += rlen - 1;
+
+ buf = g_attrib_get_buffer(long_read->attrib, &buflen);
+ if (rlen < buflen)
+ goto done;
+
+ plen = enc_read_blob_req(long_read->handle, long_read->size - 1,
+ buf, buflen);
+ id = g_attrib_send(long_read->attrib, long_read->id, buf, plen,
+ read_blob_helper, long_read, read_long_destroy);
+
+ if (id != 0) {
+ __sync_fetch_and_add(&long_read->ref, 1);
+ return;
+ }
+
+ status = ATT_ECODE_IO;
+
+done:
+ long_read->func(status, long_read->buffer, long_read->size,
+ long_read->user_data);
+}
+
+static void read_char_helper(guint8 status, const guint8 *rpdu,
+ guint16 rlen, gpointer user_data)
+{
+ struct read_long_data *long_read = user_data;
+ size_t buflen;
+ uint8_t *buf = g_attrib_get_buffer(long_read->attrib, &buflen);
+ guint16 plen;
+ guint id;
+
+ if (status != 0 || rlen < buflen)
+ goto done;
+
+ long_read->buffer = g_malloc(rlen);
+ if (long_read->buffer == NULL) {
+ status = ATT_ECODE_INSUFF_RESOURCES;
+ goto done;
+ }
+
+ memcpy(long_read->buffer, rpdu, rlen);
+ long_read->size = rlen;
+
+ plen = enc_read_blob_req(long_read->handle, rlen - 1, buf, buflen);
+
+ id = g_attrib_send(long_read->attrib, long_read->id, buf, plen,
+ read_blob_helper, long_read, read_long_destroy);
+ if (id != 0) {
+ __sync_fetch_and_add(&long_read->ref, 1);
+ return;
+ }
+
+ status = ATT_ECODE_IO;
+
+done:
+ long_read->func(status, rpdu, rlen, long_read->user_data);
+}
+
+guint gatt_read_char(GAttrib *attrib, uint16_t handle, GAttribResultFunc func,
+ gpointer user_data)
+{
+ uint8_t *buf;
+ size_t buflen;
+ guint16 plen;
+ guint id;
+ struct read_long_data *long_read;
+
+ long_read = g_try_new0(struct read_long_data, 1);
+
+ if (long_read == NULL)
+ return 0;
+
+ long_read->attrib = attrib;
+ long_read->func = func;
+ long_read->user_data = user_data;
+ long_read->handle = handle;
+
+ buf = g_attrib_get_buffer(attrib, &buflen);
+ plen = enc_read_req(handle, buf, buflen);
+ id = g_attrib_send(attrib, 0, buf, plen, read_char_helper,
+ long_read, read_long_destroy);
+ if (id == 0)
+ g_free(long_read);
+ else {
+ __sync_fetch_and_add(&long_read->ref, 1);
+ long_read->id = id;
+ }
+
+ return id;
+}
+
+struct write_long_data {
+ GAttrib *attrib;
+ GAttribResultFunc func;
+ gpointer user_data;
+ guint16 handle;
+ uint16_t offset;
+ uint8_t *value;
+ size_t vlen;
+};
+
+static guint execute_write(GAttrib *attrib, uint8_t flags,
+ GAttribResultFunc func, gpointer user_data)
+{
+ uint8_t *buf;
+ size_t buflen;
+ guint16 plen;
+
+ buf = g_attrib_get_buffer(attrib, &buflen);
+ plen = enc_exec_write_req(flags, buf, buflen);
+ if (plen == 0)
+ return 0;
+
+ return g_attrib_send(attrib, 0, buf, plen, func, user_data, NULL);
+}
+
+static guint prepare_write(struct write_long_data *long_write);
+
+static void prepare_write_cb(guint8 status, const guint8 *rpdu, guint16 rlen,
+ gpointer user_data)
+{
+ struct write_long_data *long_write = user_data;
+
+ if (status != 0) {
+ long_write->func(status, rpdu, rlen, long_write->user_data);
+ return;
+ }
+
+ /* Skip Prepare Write Response PDU header (5 bytes) */
+ long_write->offset += rlen - 5;
+
+ if (long_write->offset == long_write->vlen) {
+ execute_write(long_write->attrib, ATT_WRITE_ALL_PREP_WRITES,
+ long_write->func, long_write->user_data);
+ g_free(long_write->value);
+ g_free(long_write);
+
+ return;
+ }
+
+ prepare_write(long_write);
+}
+
+static guint prepare_write(struct write_long_data *long_write)
+{
+ GAttrib *attrib = long_write->attrib;
+ uint16_t handle = long_write->handle;
+ uint16_t offset = long_write->offset;
+ uint8_t *buf, *value = long_write->value + offset;
+ size_t buflen, vlen = long_write->vlen - offset;
+ guint16 plen;
+
+ buf = g_attrib_get_buffer(attrib, &buflen);
+
+ plen = enc_prep_write_req(handle, offset, value, vlen, buf, buflen);
+ if (plen == 0)
+ return 0;
+
+ return g_attrib_send(attrib, 0, buf, plen, prepare_write_cb, long_write,
+ NULL);
+}
+
+guint gatt_write_char(GAttrib *attrib, uint16_t handle, uint8_t *value,
+ size_t vlen, GAttribResultFunc func, gpointer user_data)
+{
+ uint8_t *buf;
+ size_t buflen;
+ struct write_long_data *long_write;
+
+ buf = g_attrib_get_buffer(attrib, &buflen);
+
+ /* Use Write Request if payload fits on a single transfer, including 3
+ * bytes for the header. */
+ if (vlen <= buflen - 3) {
+ uint16_t plen;
+
+ plen = enc_write_req(handle, value, vlen, buf, buflen);
+ if (plen == 0)
+ return 0;
+
+ return g_attrib_send(attrib, 0, buf, plen, func, user_data,
+ NULL);
+ }
+
+ /* Write Long Characteristic Values */
+ long_write = g_try_new0(struct write_long_data, 1);
+ if (long_write == NULL)
+ return 0;
+
+ long_write->attrib = attrib;
+ long_write->func = func;
+ long_write->user_data = user_data;
+ long_write->handle = handle;
+ long_write->value = g_memdup(value, vlen);
+ long_write->vlen = vlen;
+
+ return prepare_write(long_write);
+}
+
+guint gatt_exchange_mtu(GAttrib *attrib, uint16_t mtu, GAttribResultFunc func,
+ gpointer user_data)
+{
+ uint8_t *buf;
+ size_t buflen;
+ guint16 plen;
+
+ buf = g_attrib_get_buffer(attrib, &buflen);
+ plen = enc_mtu_req(mtu, buf, buflen);
+ return g_attrib_send(attrib, 0, buf, plen, func, user_data, NULL);
+}
+
+guint gatt_discover_char_desc(GAttrib *attrib, uint16_t start, uint16_t end,
+ GAttribResultFunc func, gpointer user_data)
+{
+ uint8_t *buf;
+ size_t buflen;
+ guint16 plen;
+
+ buf = g_attrib_get_buffer(attrib, &buflen);
+ plen = enc_find_info_req(start, end, buf, buflen);
+ if (plen == 0)
+ return 0;
+
+ return g_attrib_send(attrib, 0, buf, plen, func, user_data, NULL);
+}
+
+guint gatt_write_cmd(GAttrib *attrib, uint16_t handle, uint8_t *value, int vlen,
+ GDestroyNotify notify, gpointer user_data)
+{
+ uint8_t *buf;
+ size_t buflen;
+ guint16 plen;
+
+ buf = g_attrib_get_buffer(attrib, &buflen);
+ plen = enc_write_cmd(handle, value, vlen, buf, buflen);
+ return g_attrib_send(attrib, 0, buf, plen, NULL, user_data, notify);
+}
+
+static sdp_data_t *proto_seq_find(sdp_list_t *proto_list)
+{
+ sdp_list_t *list;
+ uuid_t proto;
+
+ sdp_uuid16_create(&proto, ATT_UUID);
+
+ for (list = proto_list; list; list = list->next) {
+ sdp_list_t *p;
+ for (p = list->data; p; p = p->next) {
+ sdp_data_t *seq = p->data;
+ if (seq && seq->dtd == SDP_UUID16 &&
+ sdp_uuid16_cmp(&proto, &seq->val.uuid) == 0)
+ return seq->next;
+ }
+ }
+
+ return NULL;
+}
+
+static gboolean parse_proto_params(sdp_list_t *proto_list, uint16_t *psm,
+ uint16_t *start, uint16_t *end)
+{
+ sdp_data_t *seq1, *seq2;
+
+ if (psm)
+ *psm = sdp_get_proto_port(proto_list, L2CAP_UUID);
+
+ /* Getting start and end handle */
+ seq1 = proto_seq_find(proto_list);
+ if (!seq1 || seq1->dtd != SDP_UINT16)
+ return FALSE;
+
+ seq2 = seq1->next;
+ if (!seq2 || seq2->dtd != SDP_UINT16)
+ return FALSE;
+
+ if (start)
+ *start = seq1->val.uint16;
+
+ if (end)
+ *end = seq2->val.uint16;
+
+ return TRUE;
+}
+
+gboolean gatt_parse_record(const sdp_record_t *rec,
+ uuid_t *prim_uuid, uint16_t *psm,
+ uint16_t *start, uint16_t *end)
+{
+ sdp_list_t *list;
+ uuid_t uuid;
+ gboolean ret;
+
+ if (sdp_get_service_classes(rec, &list) < 0)
+ return FALSE;
+
+ memcpy(&uuid, list->data, sizeof(uuid));
+ sdp_list_free(list, free);
+
+ if (sdp_get_access_protos(rec, &list) < 0)
+ return FALSE;
+
+ ret = parse_proto_params(list, psm, start, end);
+
+ sdp_list_foreach(list, (sdp_list_func_t) sdp_list_free, NULL);
+ sdp_list_free(list, NULL);
+
+ /* FIXME: replace by bt_uuid_t after uuid_t/sdp code cleanup */
+ if (ret && prim_uuid)
+ memcpy(prim_uuid, &uuid, sizeof(uuid_t));
+
+ return ret;
+}
diff --git a/drive-sdk/deps/bzle/src/gattrib.c b/drive-sdk/deps/bzle/src/gattrib.c
new file mode 100644
index 0000000..f865956
--- /dev/null
+++ b/drive-sdk/deps/bzle/src/gattrib.c
@@ -0,0 +1,763 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2010 Nokia Corporation
+ * Copyright (C) 2010 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 <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+#include <glib.h>
+
+#include <stdio.h>
+
+#include <bzle/bluetooth/bluetooth.h>
+
+#include <bzle/bluetooth/btio.h>
+#include <bzle/bluetooth/uuid.h>
+#include <bzle/gatt/att.h>
+#include <bzle/gatt/gattrib.h>
+
+#include "log.h"
+
+#define GATT_TIMEOUT 30
+
+struct _GAttrib {
+ GIOChannel *io;
+ int refs;
+ uint8_t *buf;
+ size_t buflen;
+ guint read_watch;
+ guint write_watch;
+ guint timeout_watch;
+ GQueue *requests;
+ GQueue *responses;
+ GSList *events;
+ guint next_cmd_id;
+ GDestroyNotify destroy;
+ gpointer destroy_user_data;
+ bool stale;
+};
+
+struct command {
+ guint id;
+ guint8 opcode;
+ guint8 *pdu;
+ guint16 len;
+ guint8 expected;
+ bool sent;
+ GAttribResultFunc func;
+ gpointer user_data;
+ GDestroyNotify notify;
+};
+
+struct event {
+ guint id;
+ guint8 expected;
+ guint16 handle;
+ GAttribNotifyFunc func;
+ gpointer user_data;
+ GDestroyNotify notify;
+};
+
+static guint8 opcode2expected(guint8 opcode)
+{
+ switch (opcode) {
+ case ATT_OP_MTU_REQ:
+ return ATT_OP_MTU_RESP;
+
+ case ATT_OP_FIND_INFO_REQ:
+ return ATT_OP_FIND_INFO_RESP;
+
+ case ATT_OP_FIND_BY_TYPE_REQ:
+ return ATT_OP_FIND_BY_TYPE_RESP;
+
+ case ATT_OP_READ_BY_TYPE_REQ:
+ return ATT_OP_READ_BY_TYPE_RESP;
+
+ case ATT_OP_READ_REQ:
+ return ATT_OP_READ_RESP;
+
+ case ATT_OP_READ_BLOB_REQ:
+ return ATT_OP_READ_BLOB_RESP;
+
+ case ATT_OP_READ_MULTI_REQ:
+ return ATT_OP_READ_MULTI_RESP;
+
+ case ATT_OP_READ_BY_GROUP_REQ:
+ return ATT_OP_READ_BY_GROUP_RESP;
+
+ case ATT_OP_WRITE_REQ:
+ return ATT_OP_WRITE_RESP;
+
+ case ATT_OP_PREP_WRITE_REQ:
+ return ATT_OP_PREP_WRITE_RESP;
+
+ case ATT_OP_EXEC_WRITE_REQ:
+ return ATT_OP_EXEC_WRITE_RESP;
+
+ case ATT_OP_HANDLE_IND:
+ return ATT_OP_HANDLE_CNF;
+ }
+
+ return 0;
+}
+
+static bool is_response(guint8 opcode)
+{
+ switch (opcode) {
+ case ATT_OP_ERROR:
+ case ATT_OP_MTU_RESP:
+ case ATT_OP_FIND_INFO_RESP:
+ case ATT_OP_FIND_BY_TYPE_RESP:
+ case ATT_OP_READ_BY_TYPE_RESP:
+ case ATT_OP_READ_RESP:
+ case ATT_OP_READ_BLOB_RESP:
+ case ATT_OP_READ_MULTI_RESP:
+ case ATT_OP_READ_BY_GROUP_RESP:
+ case ATT_OP_WRITE_RESP:
+ case ATT_OP_PREP_WRITE_RESP:
+ case ATT_OP_EXEC_WRITE_RESP:
+ case ATT_OP_HANDLE_CNF:
+ return true;
+ }
+
+ return false;
+}
+
+GAttrib *g_attrib_ref(GAttrib *attrib)
+{
+ int refs;
+
+ if (!attrib)
+ return NULL;
+
+ refs = __sync_add_and_fetch(&attrib->refs, 1);
+
+ DBG("%p: ref=%d", attrib, refs);
+
+ return attrib;
+}
+
+static void command_destroy(struct command *cmd)
+{
+ if (cmd->notify)
+ cmd->notify(cmd->user_data);
+
+ g_free(cmd->pdu);
+ g_free(cmd);
+}
+
+static void event_destroy(struct event *evt)
+{
+ if (evt->notify)
+ evt->notify(evt->user_data);
+
+ g_free(evt);
+}
+
+static void attrib_destroy(GAttrib *attrib)
+{
+ GSList *l;
+ struct command *c;
+
+ while ((c = g_queue_pop_head(attrib->requests)))
+ command_destroy(c);
+
+ while ((c = g_queue_pop_head(attrib->responses)))
+ command_destroy(c);
+
+ g_queue_free(attrib->requests);
+ attrib->requests = NULL;
+
+ g_queue_free(attrib->responses);
+ attrib->responses = NULL;
+
+ for (l = attrib->events; l; l = l->next)
+ event_destroy(l->data);
+
+ g_slist_free(attrib->events);
+ attrib->events = NULL;
+
+ if (attrib->timeout_watch > 0)
+ g_source_remove(attrib->timeout_watch);
+
+ if (attrib->write_watch > 0)
+ g_source_remove(attrib->write_watch);
+
+ if (attrib->read_watch > 0)
+ g_source_remove(attrib->read_watch);
+
+ if (attrib->io)
+ g_io_channel_unref(attrib->io);
+
+ g_free(attrib->buf);
+
+ if (attrib->destroy)
+ attrib->destroy(attrib->destroy_user_data);
+
+ g_free(attrib);
+}
+
+void g_attrib_unref(GAttrib *attrib)
+{
+ int refs;
+
+ if (!attrib)
+ return;
+
+ refs = __sync_sub_and_fetch(&attrib->refs, 1);
+
+ DBG("%p: ref=%d", attrib, refs);
+
+ if (refs > 0)
+ return;
+
+ attrib_destroy(attrib);
+}
+
+GIOChannel *g_attrib_get_channel(GAttrib *attrib)
+{
+ if (!attrib)
+ return NULL;
+
+ return attrib->io;
+}
+
+gboolean g_attrib_set_destroy_function(GAttrib *attrib,
+ GDestroyNotify destroy, gpointer user_data)
+{
+ if (attrib == NULL)
+ return FALSE;
+
+ attrib->destroy = destroy;
+ attrib->destroy_user_data = user_data;
+
+ return TRUE;
+}
+
+static gboolean disconnect_timeout(gpointer data)
+{
+ struct _GAttrib *attrib = data;
+ struct command *c;
+
+ g_attrib_ref(attrib);
+
+ c = g_queue_pop_head(attrib->requests);
+ if (c == NULL)
+ goto done;
+
+ if (c->func)
+ c->func(ATT_ECODE_TIMEOUT, NULL, 0, c->user_data);
+
+ command_destroy(c);
+
+ while ((c = g_queue_pop_head(attrib->requests))) {
+ if (c->func)
+ c->func(ATT_ECODE_ABORTED, NULL, 0, c->user_data);
+ command_destroy(c);
+ }
+
+done:
+ attrib->stale = true;
+
+ g_attrib_unref(attrib);
+
+ return FALSE;
+}
+
+static gboolean can_write_data(GIOChannel *io, GIOCondition cond,
+ gpointer data)
+{
+ struct _GAttrib *attrib = data;
+ struct command *cmd;
+ GError *gerr = NULL;
+ gsize len;
+ GIOStatus iostat;
+ GQueue *queue;
+
+ if (attrib->stale)
+ return FALSE;
+
+ if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
+ return FALSE;
+
+ queue = attrib->responses;
+ cmd = g_queue_peek_head(queue);
+ if (cmd == NULL) {
+ queue = attrib->requests;
+ cmd = g_queue_peek_head(queue);
+ }
+ if (cmd == NULL)
+ return FALSE;
+
+ /*
+ * Verify that we didn't already send this command. This can only
+ * happen with elementes from attrib->requests.
+ */
+ if (cmd->sent)
+ return FALSE;
+
+ iostat = g_io_channel_write_chars(io, (char *) cmd->pdu, cmd->len,
+ &len, &gerr);
+ if (iostat != G_IO_STATUS_NORMAL) {
+ if (gerr) {
+ error("%s", gerr->message);
+ g_error_free(gerr);
+ }
+
+ return FALSE;
+ }
+
+ if (cmd->expected == 0) {
+ g_queue_pop_head(queue);
+ command_destroy(cmd);
+
+ return TRUE;
+ }
+
+ cmd->sent = true;
+
+ if (attrib->timeout_watch == 0)
+ attrib->timeout_watch = g_timeout_add_seconds(GATT_TIMEOUT,
+ disconnect_timeout, attrib);
+
+ return FALSE;
+}
+
+static void destroy_sender(gpointer data)
+{
+ struct _GAttrib *attrib = data;
+
+ attrib->write_watch = 0;
+ g_attrib_unref(attrib);
+}
+
+static void wake_up_sender(struct _GAttrib *attrib)
+{
+ if (attrib->write_watch > 0)
+ return;
+
+ attrib = g_attrib_ref(attrib);
+ attrib->write_watch = g_io_add_watch_full(attrib->io,
+ G_PRIORITY_DEFAULT, G_IO_OUT,
+ can_write_data, attrib, destroy_sender);
+}
+
+static bool match_event(struct event *evt, const uint8_t *pdu, gsize len)
+{
+ guint16 handle;
+
+ if (evt->expected == GATTRIB_ALL_EVENTS)
+ return true;
+
+ if (!is_response(pdu[0]) && evt->expected == GATTRIB_ALL_REQS)
+ return true;
+
+ if (evt->expected == pdu[0] && evt->handle == GATTRIB_ALL_HANDLES)
+ return true;
+
+ if (len < 3)
+ return false;
+
+ handle = att_get_u16(&pdu[1]);
+
+ if (evt->expected == pdu[0] && evt->handle == handle)
+ return true;
+
+ return false;
+}
+
+static gboolean received_data(GIOChannel *io, GIOCondition cond, gpointer data)
+{
+ struct _GAttrib *attrib = data;
+ struct command *cmd = NULL;
+ GSList *l;
+ uint8_t buf[512], status;
+ gsize len;
+ GIOStatus iostat;
+
+ if (attrib->stale)
+ return FALSE;
+
+ if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
+ attrib->read_watch = 0;
+ return FALSE;
+ }
+
+ memset(buf, 0, sizeof(buf));
+
+ iostat = g_io_channel_read_chars(io, (char *) buf, sizeof(buf),
+ &len, NULL);
+ if (iostat != G_IO_STATUS_NORMAL) {
+ status = ATT_ECODE_IO;
+ goto done;
+ }
+
+ for (l = attrib->events; l; l = l->next) {
+ struct event *evt = l->data;
+
+ if (match_event(evt, buf, len))
+ evt->func(buf, len, evt->user_data);
+ }
+
+ if (!is_response(buf[0]))
+ return TRUE;
+
+ if (attrib->timeout_watch > 0) {
+ g_source_remove(attrib->timeout_watch);
+ attrib->timeout_watch = 0;
+ }
+
+ cmd = g_queue_pop_head(attrib->requests);
+ if (cmd == NULL) {
+ /* Keep the watch if we have events to report */
+ return attrib->events != NULL;
+ }
+
+ if (buf[0] == ATT_OP_ERROR) {
+ status = buf[4];
+ goto done;
+ }
+
+ if (cmd->expected != buf[0]) {
+ status = ATT_ECODE_IO;
+ goto done;
+ }
+
+ status = 0;
+
+done:
+ if (!g_queue_is_empty(attrib->requests) ||
+ !g_queue_is_empty(attrib->responses))
+ wake_up_sender(attrib);
+
+ if (cmd) {
+ if (cmd->func)
+ cmd->func(status, buf, len, cmd->user_data);
+
+ command_destroy(cmd);
+ }
+
+ return TRUE;
+}
+
+GAttrib *g_attrib_new(GIOChannel *io)
+{
+ struct _GAttrib *attrib;
+ uint16_t imtu;
+ uint16_t att_mtu;
+ uint16_t cid;
+ GError *gerr = NULL;
+
+ g_io_channel_set_encoding(io, NULL, NULL);
+ g_io_channel_set_buffered(io, FALSE);
+
+ bt_io_get(io, &gerr, BT_IO_OPT_IMTU, &imtu,
+ BT_IO_OPT_CID, &cid, BT_IO_OPT_INVALID);
+ if (gerr) {
+ error("%s", gerr->message);
+ g_error_free(gerr);
+ return NULL;
+ }
+
+ attrib = g_try_new0(struct _GAttrib, 1);
+ if (attrib == NULL)
+ return NULL;
+
+ att_mtu = (cid == ATT_CID) ? ATT_DEFAULT_LE_MTU : imtu;
+
+ attrib->buf = g_malloc0(att_mtu);
+ attrib->buflen = att_mtu;
+
+ attrib->io = g_io_channel_ref(io);
+ attrib->requests = g_queue_new();
+ attrib->responses = g_queue_new();
+
+ attrib->read_watch = g_io_add_watch(attrib->io,
+ G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+ received_data, attrib);
+
+ return g_attrib_ref(attrib);
+}
+
+guint g_attrib_send(GAttrib *attrib, guint id, const guint8 *pdu, guint16 len,
+ GAttribResultFunc func, gpointer user_data,
+ GDestroyNotify notify)
+{
+ struct command *c;
+ GQueue *queue;
+ uint8_t opcode;
+
+ if (attrib->stale)
+ return 0;
+
+ c = g_try_new0(struct command, 1);
+ if (c == NULL)
+ return 0;
+
+ opcode = pdu[0];
+
+ c->opcode = opcode;
+ c->expected = opcode2expected(opcode);
+ c->pdu = g_malloc(len);
+ memcpy(c->pdu, pdu, len);
+ c->len = len;
+ c->func = func;
+ c->user_data = user_data;
+ c->notify = notify;
+
+ if (is_response(opcode))
+ queue = attrib->responses;
+ else
+ queue = attrib->requests;
+
+ if (id) {
+ c->id = id;
+ if (!is_response(opcode))
+ g_queue_push_head(queue, c);
+ else
+ /* Don't re-order responses even if an ID is given */
+ g_queue_push_tail(queue, c);
+ } else {
+ c->id = ++attrib->next_cmd_id;
+ g_queue_push_tail(queue, c);
+ }
+
+ /*
+ * If a command was added to the queue and it was empty before, wake up
+ * the sender. If the sender was already woken up by the second queue,
+ * wake_up_sender will just return.
+ */
+ if (g_queue_get_length(queue) == 1)
+ wake_up_sender(attrib);
+
+ return c->id;
+}
+
+static int command_cmp_by_id(gconstpointer a, gconstpointer b)
+{
+ const struct command *cmd = a;
+ guint id = GPOINTER_TO_UINT(b);
+
+ return cmd->id - id;
+}
+
+gboolean g_attrib_cancel(GAttrib *attrib, guint id)
+{
+ GList *l = NULL;
+ struct command *cmd;
+ GQueue *queue;
+
+ if (attrib == NULL)
+ return FALSE;
+
+ queue = attrib->requests;
+ if (queue)
+ l = g_queue_find_custom(queue, GUINT_TO_POINTER(id),
+ command_cmp_by_id);
+ if (l == NULL) {
+ queue = attrib->responses;
+ if (!queue)
+ return FALSE;
+ l = g_queue_find_custom(queue, GUINT_TO_POINTER(id),
+ command_cmp_by_id);
+ }
+
+ if (l == NULL)
+ return FALSE;
+
+ cmd = l->data;
+
+ if (cmd == g_queue_peek_head(queue) && cmd->sent)
+ cmd->func = NULL;
+ else {
+ g_queue_remove(queue, cmd);
+ command_destroy(cmd);
+ }
+
+ return TRUE;
+}
+
+static gboolean cancel_all_per_queue(GQueue *queue)
+{
+ struct command *c, *head = NULL;
+ gboolean first = TRUE;
+
+ if (queue == NULL)
+ return FALSE;
+
+ while ((c = g_queue_pop_head(queue))) {
+ if (first && c->sent) {
+ /* If the command was sent ignore its callback ... */
+ c->func = NULL;
+ head = c;
+ continue;
+ }
+
+ first = FALSE;
+ command_destroy(c);
+ }
+
+ if (head) {
+ /* ... and put it back in the queue */
+ g_queue_push_head(queue, head);
+ }
+
+ return TRUE;
+}
+
+gboolean g_attrib_cancel_all(GAttrib *attrib)
+{
+ gboolean ret;
+
+ if (attrib == NULL)
+ return FALSE;
+
+ ret = cancel_all_per_queue(attrib->requests);
+ ret = cancel_all_per_queue(attrib->responses) && ret;
+
+ return ret;
+}
+
+gboolean g_attrib_set_debug(GAttrib *attrib,
+ GAttribDebugFunc func, gpointer user_data)
+{
+ return TRUE;
+}
+
+uint8_t *g_attrib_get_buffer(GAttrib *attrib, size_t *len)
+{
+ if (len == NULL)
+ return NULL;
+
+ *len = attrib->buflen;
+
+ return attrib->buf;
+}
+
+gboolean g_attrib_set_mtu(GAttrib *attrib, int mtu)
+{
+ if (mtu < ATT_DEFAULT_LE_MTU)
+ return FALSE;
+
+ attrib->buf = g_realloc(attrib->buf, mtu);
+
+ attrib->buflen = mtu;
+
+ return TRUE;
+}
+
+guint g_attrib_register(GAttrib *attrib, guint8 opcode, guint16 handle,
+ GAttribNotifyFunc func, gpointer user_data,
+ GDestroyNotify notify)
+{
+ static guint next_evt_id = 0;
+ struct event *event;
+
+ event = g_try_new0(struct event, 1);
+ if (event == NULL)
+ return 0;
+
+ event->expected = opcode;
+ event->handle = handle;
+ event->func = func;
+ event->user_data = user_data;
+ event->notify = notify;
+ event->id = ++next_evt_id;
+
+ attrib->events = g_slist_append(attrib->events, event);
+
+ return event->id;
+}
+
+static int event_cmp_by_id(gconstpointer a, gconstpointer b)
+{
+ const struct event *evt = a;
+ guint id = GPOINTER_TO_UINT(b);
+
+ return evt->id - id;
+}
+
+gboolean g_attrib_is_encrypted(GAttrib *attrib)
+{
+ BtIOSecLevel sec_level;
+
+ if (!bt_io_get(attrib->io, NULL,
+ BT_IO_OPT_SEC_LEVEL, &sec_level,
+ BT_IO_OPT_INVALID))
+ return FALSE;
+
+ return sec_level > BT_IO_SEC_LOW;
+}
+
+gboolean g_attrib_unregister(GAttrib *attrib, guint id)
+{
+ struct event *evt;
+ GSList *l;
+
+ if (id == 0) {
+ warn("%s: invalid id", __func__);
+ return FALSE;
+ }
+
+ l = g_slist_find_custom(attrib->events, GUINT_TO_POINTER(id),
+ event_cmp_by_id);
+ if (l == NULL)
+ return FALSE;
+
+ evt = l->data;
+
+ attrib->events = g_slist_remove(attrib->events, evt);
+
+ if (evt->notify)
+ evt->notify(evt->user_data);
+
+ g_free(evt);
+
+ return TRUE;
+}
+
+gboolean g_attrib_unregister_all(GAttrib *attrib)
+{
+ GSList *l;
+
+ if (attrib->events == NULL)
+ return FALSE;
+
+ for (l = attrib->events; l; l = l->next) {
+ struct event *evt = l->data;
+
+ if (evt->notify)
+ evt->notify(evt->user_data);
+
+ g_free(evt);
+ }
+
+ g_slist_free(attrib->events);
+ attrib->events = NULL;
+
+ return TRUE;
+}
diff --git a/drive-sdk/deps/bzle/src/hci.c b/drive-sdk/deps/bzle/src/hci.c
new file mode 100644
index 0000000..3579bfd
--- /dev/null
+++ b/drive-sdk/deps/bzle/src/hci.c
@@ -0,0 +1,2929 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2000-2001 Qualcomm Incorporated
+ * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
+ * Copyright (C) 2002-2010 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 <bzle/bluetooth/bluetooth.h>
+#include <bzle/bluetooth/hci.h>
+#include <bzle/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_bustostr(int bus)
+{
+ switch (bus) {
+ 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";
+ }
+}
+
+char *hci_dtypetostr(int type)
+{
+ return hci_bustostr(type & 0x0f);
+}
+
+char *hci_typetostr(int type)
+{
+ switch (type) {
+ case HCI_BREDR:
+ return "BR/EDR";
+ case HCI_AMP:
+ return "AMP";
+ 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 },
+ { 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 Response 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 Capability Request Negative Reply", 163 },
+ { "Read Encryption Key Size", 164 },
+ { "Reserved", 165 },
+ { "Reserved", 166 },
+ { "Reserved", 167 },
+
+ { "Create Physical Link", 168 },
+ { "Accept Physical Link", 169 },
+ { "Disconnect Physical Link", 170 },
+ { "Create Logical Link", 171 },
+ { "Accept Logical Link", 172 },
+ { "Disconnect Logical Link", 173 },
+ { "Logical Link Cancel", 174 },
+ { "Flow Specification Modify", 175 },
+
+ { "Read Logical Link Accept Timeout", 176 },
+ { "Write Logical Link Accept Timeout", 177 },
+ { "Set Event Mask Page 2", 178 },
+ { "Read Location Data", 179 },
+ { "Write Location Data", 180 },
+ { "Read Local AMP Info", 181 },
+ { "Read Local AMP_ASSOC", 182 },
+ { "Write Remote AMP_ASSOC", 183 },
+
+ { "Read Flow Control Mode", 184 },
+ { "Write Flow Control Mode", 185 },
+ { "Read Data Block Size", 186 },
+ { "Reserved", 187 },
+ { "Reserved", 188 },
+ { "Enable AMP Receiver Reports", 189 },
+ { "AMP Test End", 190 },
+ { "AMP Test Command", 191 },
+
+ { "Read Enhanced Transmit Power Level", 192 },
+ { "Reserved", 193 },
+ { "Read Best Effort Flush Timeout", 194 },
+ { "Write Best Effort Flush Timeout", 195 },
+ { "Short Range Mode", 196 },
+ { "Read LE Host Support", 197 },
+ { "Write LE Host Support", 198 },
+ { "Reserved", 199 },
+
+ { "LE Set Event Mask", 200 },
+ { "LE Read Buffer Size", 201 },
+ { "LE Read Local Supported Features", 202 },
+ { "Reserved", 203 },
+ { "LE Set Random Address", 204 },
+ { "LE Set Advertising Parameters", 205 },
+ { "LE Read Advertising Channel TX Power", 206 },
+ { "LE Set Advertising Data", 207 },
+
+ { "LE Set Scan Response Data", 208 },
+ { "LE Set Advertise Enable", 209 },
+ { "LE Set Scan Parameters", 210 },
+ { "LE Set Scan Enable", 211 },
+ { "LE Create Connection", 212 },
+ { "LE Create Connection Cancel", 213 },
+ { "LE Read White List Size", 214 },
+ { "LE Clear White List", 215 },
+
+ { "LE Add Device To White List", 216 },
+ { "LE Remove Device From White List", 217 },
+ { "LE Connection Update", 218 },
+ { "LE Set Host Channel Classification", 219 },
+ { "LE Read Channel Map", 220 },
+ { "LE Read Remote Used Features", 221 },
+ { "LE Encrypt", 222 },
+ { "LE Rand", 223 },
+
+ { "LE Start Encryption", 224 },
+ { "LE Long Term Key Request Reply", 225 },
+ { "LE Long Term Key Request Negative Reply", 226 },
+ { "LE Read Supported States", 227 },
+ { "LE Receiver Test", 228 },
+ { "LE Transmitter Test", 229 },
+ { "LE Test End", 230 },
+ { "Reserved", 231 },
+
+ { NULL }
+};
+
+char *hci_cmdtostr(unsigned int cmd)
+{
+ return hci_uint2str(commands_map, cmd);
+}
+
+char *hci_commandstostr(uint8_t *commands, char *pref, int width)
+{
+ unsigned int maxwidth = width - 3;
+ 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) > maxwidth) {
+ 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 },
+ { "3.0", 0x05 },
+ { "4.0", 0x06 },
+ { "4.1", 0x07 },
+ { 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);
+}
+
+static hci_map pal_map[] = {
+ { "3.0", 0x01 },
+ { NULL }
+};
+
+char *pal_vertostr(unsigned int ver)
+{
+ return hci_uint2str(pal_map, ver);
+}
+
+int pal_strtover(char *str, unsigned int *ver)
+{
+ return hci_str2uint(pal_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 */
+ { "<BR/EDR not supp.>", LMP_NO_BREDR }, /* Bit 5 */
+ { "<LE support>", LMP_LE }, /* 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 */
+ { "<LE and BR/EDR>", LMP_LE_BREDR }, /* 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 */
+ { "<EPC>", LMP_EPC }, /* 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)
+{
+ unsigned int maxwidth = width - 1;
+ 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) > maxwidth) {
+ 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 | SOCK_CLOEXEC, 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 | SOCK_CLOEXEC, 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 | SOCK_CLOEXEC, 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 | SOCK_CLOEXEC, 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 olen;
+ hci_event_hdr *hdr;
+ int err, try;
+
+ olen = sizeof(of);
+ if (getsockopt(dd, SOL_HCI, HCI_FILTER, &of, &olen) < 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(EVT_LE_META_EVENT, &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;
+ evt_le_meta_event *me;
+ remote_name_req_cp *cp;
+ int len;
+
+ 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;
+
+ case EVT_LE_META_EVENT:
+ me = (void *) ptr;
+
+ if (me->subevent != r->event)
+ continue;
+
+ len -= 1;
+ r->rlen = MIN(len, r->rlen);
+ memcpy(r->rparam, me->data, 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_le_add_white_list(int dd, const bdaddr_t *bdaddr, uint8_t type, int to)
+{
+ struct hci_request rq;
+ le_add_device_to_white_list_cp cp;
+ uint8_t status;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.bdaddr_type = type;
+ bacpy(&cp.bdaddr, bdaddr);
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LE_CTL;
+ rq.ocf = OCF_LE_ADD_DEVICE_TO_WHITE_LIST;
+ rq.cparam = &cp;
+ rq.clen = LE_ADD_DEVICE_TO_WHITE_LIST_CP_SIZE;
+ rq.rparam = &status;
+ rq.rlen = 1;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_le_rm_white_list(int dd, const bdaddr_t *bdaddr, uint8_t type, int to)
+{
+ struct hci_request rq;
+ le_remove_device_from_white_list_cp cp;
+ uint8_t status;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.bdaddr_type = type;
+ bacpy(&cp.bdaddr, bdaddr);
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LE_CTL;
+ rq.ocf = OCF_LE_REMOVE_DEVICE_FROM_WHITE_LIST;
+ rq.cparam = &cp;
+ rq.clen = LE_REMOVE_DEVICE_FROM_WHITE_LIST_CP_SIZE;
+ rq.rparam = &status;
+ rq.rlen = 1;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_le_read_white_list_size(int dd, uint8_t *size, int to)
+{
+ struct hci_request rq;
+ le_read_white_list_size_rp rp;
+
+ memset(&rp, 0, sizeof(rp));
+ memset(&rq, 0, sizeof(rq));
+
+ rq.ogf = OGF_LE_CTL;
+ rq.ocf = OCF_LE_READ_WHITE_LIST_SIZE;
+ rq.rparam = &rp;
+ rq.rlen = LE_READ_WHITE_LIST_SIZE_RP_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ if (size)
+ *size = rp.size;
+
+ return 0;
+}
+
+int hci_le_clear_white_list(int dd, int to)
+{
+ struct hci_request rq;
+ uint8_t status;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LE_CTL;
+ rq.ocf = OCF_LE_CLEAR_WHITE_LIST;
+ rq.rparam = &status;
+ rq.rlen = 1;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (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;
+ }
+
+ if (features)
+ 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;
+ }
+
+ if (max_page)
+ *max_page = rp.max_page_num;
+
+ if (features)
+ 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;
+ }
+
+ if (commands)
+ 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;
+ }
+
+ if (features)
+ 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;
+ }
+
+ if (max_page)
+ *max_page = rp.max_page_num;
+
+ if (features)
+ 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;
+ }
+
+ if (bdaddr)
+ 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, HCI_MAX_EIR_LENGTH);
+
+ 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, HCI_MAX_EIR_LENGTH);
+
+ 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_inq_response_tx_power_level(int dd, int8_t *level, int to)
+{
+ read_inq_response_tx_power_level_rp rp;
+ struct hci_request rq;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_HOST_CTL;
+ rq.ocf = OCF_READ_INQ_RESPONSE_TX_POWER_LEVEL;
+ rq.rparam = &rp;
+ rq.rlen = READ_INQ_RESPONSE_TX_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_inquiry_transmit_power_level(int dd, int8_t *level, int to)
+{
+ return hci_read_inq_response_tx_power_level(dd, level, to);
+}
+
+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_le_set_scan_enable(int dd, uint8_t enable, uint8_t filter_dup, int to)
+{
+ struct hci_request rq;
+ le_set_scan_enable_cp scan_cp;
+ uint8_t status;
+
+ memset(&scan_cp, 0, sizeof(scan_cp));
+ scan_cp.enable = enable;
+ scan_cp.filter_dup = filter_dup;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LE_CTL;
+ rq.ocf = OCF_LE_SET_SCAN_ENABLE;
+ rq.cparam = &scan_cp;
+ rq.clen = LE_SET_SCAN_ENABLE_CP_SIZE;
+ rq.rparam = &status;
+ rq.rlen = 1;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_le_set_scan_parameters(int dd, uint8_t type,
+ uint16_t interval, uint16_t window,
+ uint8_t own_type, uint8_t filter, int to)
+{
+ struct hci_request rq;
+ le_set_scan_parameters_cp param_cp;
+ uint8_t status;
+
+ memset(&param_cp, 0, sizeof(param_cp));
+ param_cp.type = type;
+ param_cp.interval = interval;
+ param_cp.window = window;
+ param_cp.own_bdaddr_type = own_type;
+ param_cp.filter = filter;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LE_CTL;
+ rq.ocf = OCF_LE_SET_SCAN_PARAMETERS;
+ rq.cparam = &param_cp;
+ rq.clen = LE_SET_SCAN_PARAMETERS_CP_SIZE;
+ rq.rparam = &status;
+ rq.rlen = 1;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_le_set_advertise_enable(int dd, uint8_t enable, int to)
+{
+ struct hci_request rq;
+ le_set_advertise_enable_cp adv_cp;
+ uint8_t status;
+
+ memset(&adv_cp, 0, sizeof(adv_cp));
+ adv_cp.enable = enable;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LE_CTL;
+ rq.ocf = OCF_LE_SET_ADVERTISE_ENABLE;
+ rq.cparam = &adv_cp;
+ rq.clen = LE_SET_ADVERTISE_ENABLE_CP_SIZE;
+ rq.rparam = &status;
+ rq.rlen = 1;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
+
+int hci_le_create_conn(int dd, uint16_t interval, uint16_t window,
+ uint8_t initiator_filter, uint8_t peer_bdaddr_type,
+ bdaddr_t peer_bdaddr, uint8_t own_bdaddr_type,
+ uint16_t min_interval, uint16_t max_interval,
+ uint16_t latency, uint16_t supervision_timeout,
+ uint16_t min_ce_length, uint16_t max_ce_length,
+ uint16_t *handle, int to)
+{
+ struct hci_request rq;
+ le_create_connection_cp create_conn_cp;
+ evt_le_connection_complete conn_complete_rp;
+
+ memset(&create_conn_cp, 0, sizeof(create_conn_cp));
+ create_conn_cp.interval = interval;
+ create_conn_cp.window = window;
+ create_conn_cp.initiator_filter = initiator_filter;
+ create_conn_cp.peer_bdaddr_type = peer_bdaddr_type;
+ create_conn_cp.peer_bdaddr = peer_bdaddr;
+ create_conn_cp.own_bdaddr_type = own_bdaddr_type;
+ create_conn_cp.min_interval = min_interval;
+ create_conn_cp.max_interval = max_interval;
+ create_conn_cp.latency = latency;
+ create_conn_cp.supervision_timeout = supervision_timeout;
+ create_conn_cp.min_ce_length = min_ce_length;
+ create_conn_cp.max_ce_length = max_ce_length;
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LE_CTL;
+ rq.ocf = OCF_LE_CREATE_CONN;
+ rq.event = EVT_LE_CONN_COMPLETE;
+ rq.cparam = &create_conn_cp;
+ rq.clen = LE_CREATE_CONN_CP_SIZE;
+ rq.rparam = &conn_complete_rp;
+ rq.rlen = EVT_CONN_COMPLETE_SIZE;
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (conn_complete_rp.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ if (handle)
+ *handle = conn_complete_rp.handle;
+
+ return 0;
+}
+
+int hci_le_conn_update(int dd, uint16_t handle, uint16_t min_interval,
+ uint16_t max_interval, uint16_t latency,
+ uint16_t supervision_timeout, int to)
+{
+ evt_le_connection_update_complete evt;
+ le_connection_update_cp cp;
+ struct hci_request rq;
+
+ memset(&cp, 0, sizeof(cp));
+ cp.handle = handle;
+ cp.min_interval = min_interval;
+ cp.max_interval = max_interval;
+ cp.latency = latency;
+ cp.supervision_timeout = supervision_timeout;
+ cp.min_ce_length = htobs(0x0001);
+ cp.max_ce_length = htobs(0x0001);
+
+ memset(&rq, 0, sizeof(rq));
+ rq.ogf = OGF_LE_CTL;
+ rq.ocf = OCF_LE_CONN_UPDATE;
+ rq.cparam = &cp;
+ rq.clen = LE_CONN_UPDATE_CP_SIZE;
+ rq.event = EVT_LE_CONN_UPDATE_COMPLETE;
+ rq.rparam = &evt;
+ rq.rlen = sizeof(evt);
+
+ if (hci_send_req(dd, &rq, to) < 0)
+ return -1;
+
+ if (evt.status) {
+ errno = EIO;
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/drive-sdk/deps/bzle/src/log.c b/drive-sdk/deps/bzle/src/log.c
new file mode 100644
index 0000000..19b3450
--- /dev/null
+++ b/drive-sdk/deps/bzle/src/log.c
@@ -0,0 +1,146 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2010 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 <stdarg.h>
+#include <syslog.h>
+
+#include <glib.h>
+
+#include "log.h"
+
+#define VERSION "5.15"
+
+void info(const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+
+ vsyslog(LOG_INFO, format, ap);
+
+ va_end(ap);
+}
+
+void warn(const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+
+ vsyslog(LOG_WARNING, format, ap);
+
+ va_end(ap);
+}
+
+void error(const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+
+ vsyslog(LOG_ERR, format, ap);
+
+ va_end(ap);
+}
+
+void btd_debug(const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+
+ vsyslog(LOG_DEBUG, format, ap);
+
+ va_end(ap);
+}
+
+extern struct btd_debug_desc __start___debug[];
+extern struct btd_debug_desc __stop___debug[];
+
+static char **enabled = NULL;
+
+static gboolean is_enabled(struct btd_debug_desc *desc)
+{
+ int i;
+
+ if (enabled == NULL)
+ return 0;
+
+ for (i = 0; enabled[i] != NULL; i++)
+ if (desc->file != NULL && g_pattern_match_simple(enabled[i],
+ desc->file) == TRUE)
+ return 1;
+
+ return 0;
+}
+
+void __btd_enable_debug(struct btd_debug_desc *start,
+ struct btd_debug_desc *stop)
+{
+ struct btd_debug_desc *desc;
+
+ if (start == NULL || stop == NULL)
+ return;
+
+ for (desc = start; desc < stop; desc++) {
+ if (is_enabled(desc))
+ desc->flags |= BTD_DEBUG_FLAG_PRINT;
+ }
+}
+
+void __btd_toggle_debug(void)
+{
+ struct btd_debug_desc *desc;
+
+ for (desc = __start___debug; desc < __stop___debug; desc++)
+ desc->flags |= BTD_DEBUG_FLAG_PRINT;
+}
+
+void __btd_log_init(const char *debug, int detach)
+{
+ int option = LOG_NDELAY | LOG_PID;
+
+ if (debug != NULL)
+ enabled = g_strsplit_set(debug, ":, ", 0);
+
+ __btd_enable_debug(__start___debug, __stop___debug);
+
+ if (!detach)
+ option |= LOG_PERROR;
+
+ openlog("bluetoothd", option, LOG_DAEMON);
+
+ syslog(LOG_INFO, "Bluetooth daemon %s", VERSION);
+}
+
+void __btd_log_cleanup(void)
+{
+ closelog();
+
+ g_strfreev(enabled);
+}
diff --git a/drive-sdk/deps/bzle/src/log.h b/drive-sdk/deps/bzle/src/log.h
new file mode 100644
index 0000000..bf9eac2
--- /dev/null
+++ b/drive-sdk/deps/bzle/src/log.h
@@ -0,0 +1,59 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2004-2010 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
+ *
+ */
+
+void info(const char *format, ...) __attribute__((format(printf, 1, 2)));
+void warn(const char *format, ...) __attribute__((format(printf, 1, 2)));
+void error(const char *format, ...) __attribute__((format(printf, 1, 2)));
+
+void btd_debug(const char *format, ...) __attribute__((format(printf, 1, 2)));
+
+void __btd_log_init(const char *debug, int detach);
+void __btd_log_cleanup(void);
+void __btd_toggle_debug(void);
+
+struct btd_debug_desc {
+ const char *file;
+#define BTD_DEBUG_FLAG_DEFAULT (0)
+#define BTD_DEBUG_FLAG_PRINT (1 << 0)
+ unsigned int flags;
+} __attribute__((aligned(8)));
+
+void __btd_enable_debug(struct btd_debug_desc *start,
+ struct btd_debug_desc *stop);
+
+/**
+ * DBG:
+ * @fmt: format string
+ * @arg...: list of arguments
+ *
+ * Simple macro around btd_debug() which also include the function
+ * name it is called in.
+ */
+#define DBG(fmt, arg...) do { \
+ static struct btd_debug_desc __btd_debug_desc \
+ __attribute__((used, section("__debug"), aligned(8))) = { \
+ .file = __FILE__, .flags = BTD_DEBUG_FLAG_DEFAULT, \
+ }; \
+ if (__btd_debug_desc.flags & BTD_DEBUG_FLAG_PRINT) \
+ btd_debug("%s:%s() " fmt, __FILE__, __func__ , ## arg); \
+} while (0)
diff --git a/drive-sdk/deps/bzle/src/sdp.c b/drive-sdk/deps/bzle/src/sdp.c
new file mode 100644
index 0000000..1b0939e
--- /dev/null
+++ b/drive-sdk/deps/bzle/src/sdp.c
@@ -0,0 +1,4940 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2001-2002 Nokia Corporation
+ * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
+ * Copyright (C) 2002-2010 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 <netinet/in.h>
+
+#include <bzle/bluetooth/bluetooth.h>
+#include <bzle/bluetooth/hci.h>
+#include <bzle/bluetooth/hci_lib.h>
+#include <bzle/bluetooth/l2cap.h>
+#include <bzle/bluetooth/sdp.h>
+#include <bzle/bluetooth/sdp_lib.h>
+
+#define SDPINF(fmt, arg...) syslog(LOG_INFO, fmt "\n", ## arg)
+#define SDPERR(fmt, arg...) syslog(LOG_ERR, "%s: " fmt "\n", __func__ , ## arg)
+
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+
+#ifdef SDP_DEBUG
+#define SDPDBG(fmt, arg...) syslog(LOG_DEBUG, "%s: " fmt "\n", __func__ , ## arg)
+#else
+#define SDPDBG(fmt...)
+#endif
+
+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
+
+/* match MTU used by RFCOMM */
+#define SDP_LARGE_L2CAP_MTU 1013
+
+static sdp_data_t *sdp_copy_seq(sdp_data_t *data);
+static int sdp_attr_add_new_with_length(sdp_record_t *rec,
+ uint16_t attr, uint8_t dtd, const void *value, uint32_t len);
+static int sdp_gen_buffer(sdp_buf_t *buf, sdp_data_t *d);
+
+/* 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" },
+ { ATT_UUID, "ATT" },
+ { 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" },
+ { AV_REMOTE_CONTROLLER_SVCLASS_ID, "AV Remote Controller" },
+ { 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" },
+ { MAP_MSE_SVCLASS_ID, "Message Access - MAS" },
+ { MAP_MCE_SVCLASS_ID, "Message Access - MNS" },
+ { MAP_SVCLASS_ID, "Message 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" },
+ { HDP_SVCLASS_ID, "HDP" },
+ { HDP_SOURCE_SVCLASS_ID, "HDP Source" },
+ { HDP_SINK_SVCLASS_ID, "HDP Sink" },
+ { GENERIC_ACCESS_SVCLASS_ID, "Generic Access" },
+ { GENERIC_ATTRIB_SVCLASS_ID, "Generic Attribute" },
+ { 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, "%s", str2);
+ break;
+ case SDP_UUID32:
+ str2 = string_lookup(message, uuid->value.uuid32);
+ snprintf(str, n, "%s", str2);
+ break;
+ case SDP_UUID128:
+ str2 = string_lookup_uuid(message, uuid);
+ snprintf(str, n, "%s", 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");
+ return;
+ }
+ if (uuid->type == SDP_UUID16) {
+ SDPDBG(" uint16_t : 0x%.4x", uuid->value.uuid16);
+ } else if (uuid->type == SDP_UUID32) {
+ SDPDBG(" uint32_t : 0x%.8x", 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-%.4x-%.4x-%.4x-%.8x%.4x",
+ ntohl(data0), ntohs(data1), ntohs(data2),
+ ntohs(data3), ntohl(data4), ntohs(data5));
+ } else
+ SDPERR("Enum type of UUID not set");
+}
+#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");
+ free(d);
+ d = NULL;
+ }
+ break;
+ case SDP_URL_STR32:
+ case SDP_TEXT_STR32:
+ SDPERR("Strings of size > USHRT_MAX not supported");
+ 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(SDP_SEQ8, seq);
+}
+
+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 || !SDP_IS_SEQ(data->dtd))
+ 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 = *ptr++;
+
+ switch (dtd) {
+ case SDP_SEQ8:
+ case SDP_ALT8:
+ case SDP_TEXT_STR8:
+ case SDP_URL_STR8:
+ *ptr = (uint8_t) length;
+ break;
+ case SDP_SEQ16:
+ case SDP_ALT16:
+ case SDP_TEXT_STR16:
+ case SDP_URL_STR16:
+ bt_put_be16(length, ptr);
+ break;
+ case SDP_SEQ32:
+ case SDP_ALT32:
+ case SDP_TEXT_STR32:
+ case SDP_URL_STR32:
+ bt_put_be32(length, ptr);
+ break;
+ }
+}
+
+static int sdp_get_data_type_size(uint8_t dtd)
+{
+ int size = sizeof(uint8_t);
+
+ switch (dtd) {
+ case SDP_SEQ8:
+ case SDP_TEXT_STR8:
+ case SDP_URL_STR8:
+ case SDP_ALT8:
+ size += sizeof(uint8_t);
+ break;
+ case SDP_SEQ16:
+ case SDP_TEXT_STR16:
+ case SDP_URL_STR16:
+ case SDP_ALT16:
+ size += sizeof(uint16_t);
+ break;
+ case SDP_SEQ32:
+ case SDP_TEXT_STR32:
+ case SDP_URL_STR32:
+ case SDP_ALT32:
+ size += sizeof(uint32_t);
+ break;
+ }
+
+ return size;
+}
+
+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_be16(attr, p);
+ 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) {
+ if (buf->data)
+ n += sdp_gen_pdu(buf, d);
+ else
+ n += sdp_gen_buffer(buf, d);
+ }
+
+ return n;
+}
+
+static int sdp_get_data_size(sdp_buf_t *buf, sdp_data_t *d)
+{
+ uint32_t data_size = 0;
+ uint8_t dtd = d->dtd;
+
+ switch (dtd) {
+ case SDP_DATA_NIL:
+ break;
+ case SDP_UINT8:
+ data_size = sizeof(uint8_t);
+ break;
+ case SDP_UINT16:
+ data_size = sizeof(uint16_t);
+ break;
+ case SDP_UINT32:
+ data_size = sizeof(uint32_t);
+ break;
+ case SDP_UINT64:
+ data_size = sizeof(uint64_t);
+ break;
+ case SDP_UINT128:
+ data_size = sizeof(uint128_t);
+ break;
+ case SDP_INT8:
+ case SDP_BOOL:
+ data_size = sizeof(int8_t);
+ break;
+ case SDP_INT16:
+ data_size = sizeof(int16_t);
+ break;
+ case SDP_INT32:
+ data_size = sizeof(int32_t);
+ break;
+ case SDP_INT64:
+ data_size = sizeof(int64_t);
+ break;
+ case SDP_INT128:
+ 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:
+ data_size = d->unitSize - sizeof(uint8_t);
+ break;
+ case SDP_SEQ8:
+ case SDP_SEQ16:
+ case SDP_SEQ32:
+ data_size = get_data_size(buf, d);
+ break;
+ case SDP_ALT8:
+ case SDP_ALT16:
+ case SDP_ALT32:
+ data_size = get_data_size(buf, d);
+ break;
+ case SDP_UUID16:
+ data_size = sizeof(uint16_t);
+ break;
+ case SDP_UUID32:
+ data_size = sizeof(uint32_t);
+ break;
+ case SDP_UUID128:
+ data_size = sizeof(uint128_t);
+ break;
+ default:
+ break;
+ }
+
+ return data_size;
+}
+
+static int sdp_gen_buffer(sdp_buf_t *buf, sdp_data_t *d)
+{
+ int orig = buf->buf_size;
+
+ if (buf->buf_size == 0 && d->dtd == 0) {
+ /* create initial sequence */
+ buf->buf_size += sizeof(uint8_t);
+
+ /* reserve space for sequence size */
+ buf->buf_size += sizeof(uint8_t);
+ }
+
+ /* attribute length */
+ buf->buf_size += sizeof(uint8_t) + sizeof(uint16_t);
+
+ buf->buf_size += sdp_get_data_type_size(d->dtd);
+ buf->buf_size += sdp_get_data_size(buf, d);
+
+ if (buf->buf_size > UCHAR_MAX && d->dtd == SDP_SEQ8)
+ buf->buf_size += sizeof(uint8_t);
+
+ return buf->buf_size - orig;
+}
+
+int sdp_gen_pdu(sdp_buf_t *buf, sdp_data_t *d)
+{
+ uint32_t pdu_size, data_size;
+ unsigned char *src = NULL, is_seq = 0, is_alt = 0;
+ uint16_t u16;
+ uint32_t u32;
+ uint64_t u64;
+ uint128_t u128;
+ uint8_t *seqp = buf->data + buf->data_size;
+ uint32_t orig_data_size = buf->data_size;
+
+recalculate:
+ pdu_size = sdp_get_data_type_size(d->dtd);
+ buf->data_size += pdu_size;
+
+ data_size = sdp_get_data_size(buf, d);
+ if (data_size > UCHAR_MAX && d->dtd == SDP_SEQ8) {
+ buf->data_size = orig_data_size;
+ d->dtd = SDP_SEQ16;
+ goto recalculate;
+ }
+
+ *seqp = d->dtd;
+
+ switch (d->dtd) {
+ case SDP_DATA_NIL:
+ break;
+ case SDP_UINT8:
+ src = &d->val.uint8;
+ break;
+ case SDP_UINT16:
+ u16 = htons(d->val.uint16);
+ src = (unsigned char *) &u16;
+ break;
+ case SDP_UINT32:
+ u32 = htonl(d->val.uint32);
+ src = (unsigned char *) &u32;
+ break;
+ case SDP_UINT64:
+ u64 = hton64(d->val.uint64);
+ src = (unsigned char *) &u64;
+ break;
+ case SDP_UINT128:
+ hton128(&d->val.uint128, &u128);
+ src = (unsigned char *) &u128;
+ break;
+ case SDP_INT8:
+ case SDP_BOOL:
+ src = (unsigned char *) &d->val.int8;
+ break;
+ case SDP_INT16:
+ u16 = htons(d->val.int16);
+ src = (unsigned char *) &u16;
+ break;
+ case SDP_INT32:
+ u32 = htonl(d->val.int32);
+ src = (unsigned char *) &u32;
+ break;
+ case SDP_INT64:
+ u64 = hton64(d->val.int64);
+ src = (unsigned char *) &u64;
+ break;
+ case SDP_INT128:
+ hton128(&d->val.int128, &u128);
+ src = (unsigned char *) &u128;
+ 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;
+ sdp_set_seq_len(seqp, data_size);
+ break;
+ case SDP_SEQ8:
+ case SDP_SEQ16:
+ case SDP_SEQ32:
+ is_seq = 1;
+ sdp_set_seq_len(seqp, data_size);
+ break;
+ case SDP_ALT8:
+ case SDP_ALT16:
+ case SDP_ALT32:
+ is_alt = 1;
+ sdp_set_seq_len(seqp, data_size);
+ break;
+ case SDP_UUID16:
+ u16 = htons(d->val.uuid.value.uuid16);
+ src = (unsigned char *) &u16;
+ break;
+ case SDP_UUID32:
+ u32 = htonl(d->val.uuid.value.uuid32);
+ src = (unsigned char *) &u32;
+ break;
+ case SDP_UUID128:
+ src = (unsigned char *) &d->val.uuid.value.uuid128;
+ break;
+ default:
+ break;
+ }
+
+ if (!is_seq && !is_alt) {
+ if (src && buf->buf_size >= buf->data_size + data_size) {
+ memcpy(buf->data + buf->data_size, src, data_size);
+ buf->data_size += data_size;
+ } else if (d->dtd != SDP_DATA_NIL) {
+ SDPDBG("Gen PDU : Can't copy from invalid source or dest");
+ }
+ }
+
+ 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);
+}
+
+static void sdp_attr_size(void *value, void *udata)
+{
+ sdp_gen_buffer((sdp_buf_t *)udata, (sdp_data_t *)value);
+}
+
+int sdp_gen_record_pdu(const sdp_record_t *rec, sdp_buf_t *buf)
+{
+ memset(buf, 0, sizeof(sdp_buf_t));
+ sdp_list_foreach(rec->attrlist, sdp_attr_size, buf);
+
+ buf->data = malloc(buf->buf_size);
+ if (!buf->data)
+ return -ENOMEM;
+ 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(const uint8_t *p, int bufsize, uuid_t *uuid, int *scanned)
+{
+ uint8_t type;
+
+ if (bufsize < (int) 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", type);
+ return -1;
+ }
+ p += sizeof(uint8_t);
+ *scanned += sizeof(uint8_t);
+ bufsize -= sizeof(uint8_t);
+ if (type == SDP_UUID16) {
+ if (bufsize < (int) sizeof(uint16_t)) {
+ SDPERR("Not enough room for 16-bit UUID");
+ return -1;
+ }
+ sdp_uuid16_create(uuid, bt_get_be16(p));
+ *scanned += sizeof(uint16_t);
+ } else if (type == SDP_UUID32) {
+ if (bufsize < (int) sizeof(uint32_t)) {
+ SDPERR("Not enough room for 32-bit UUID");
+ return -1;
+ }
+ sdp_uuid32_create(uuid, bt_get_be32(p));
+ *scanned += sizeof(uint32_t);
+ } else {
+ if (bufsize < (int) sizeof(uint128_t)) {
+ SDPERR("Not enough room for 128-bit UUID");
+ return -1;
+ }
+ sdp_uuid128_create(uuid, p);
+ *scanned += sizeof(uint128_t);
+ }
+ return 0;
+}
+
+static sdp_data_t *extract_int(const void *p, int bufsize, int *len)
+{
+ sdp_data_t *d;
+
+ if (bufsize < (int) sizeof(uint8_t)) {
+ SDPERR("Unexpected end of packet");
+ return NULL;
+ }
+
+ d = malloc(sizeof(sdp_data_t));
+ if (!d)
+ return NULL;
+
+ SDPDBG("Extracting integer");
+ 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 < (int) 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 < (int) sizeof(uint16_t)) {
+ SDPERR("Unexpected end of packet");
+ free(d);
+ return NULL;
+ }
+ *len += sizeof(uint16_t);
+ d->val.uint16 = bt_get_be16(p);
+ break;
+ case SDP_INT32:
+ case SDP_UINT32:
+ if (bufsize < (int) sizeof(uint32_t)) {
+ SDPERR("Unexpected end of packet");
+ free(d);
+ return NULL;
+ }
+ *len += sizeof(uint32_t);
+ d->val.uint32 = bt_get_be32(p);
+ break;
+ case SDP_INT64:
+ case SDP_UINT64:
+ if (bufsize < (int) sizeof(uint64_t)) {
+ SDPERR("Unexpected end of packet");
+ free(d);
+ return NULL;
+ }
+ *len += sizeof(uint64_t);
+ d->val.uint64 = bt_get_be64(p);
+ break;
+ case SDP_INT128:
+ case SDP_UINT128:
+ if (bufsize < (int) 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));
+
+ if (!d)
+ return NULL;
+
+ SDPDBG("Extracting UUID");
+ memset(d, 0, sizeof(sdp_data_t));
+ if (sdp_uuid_extract(p, bufsize, &d->val.uuid, len) < 0) {
+ free(d);
+ return NULL;
+ }
+ d->dtd = *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 < (int) sizeof(uint8_t)) {
+ SDPERR("Unexpected end of packet");
+ return NULL;
+ }
+
+ d = malloc(sizeof(sdp_data_t));
+ if (!d)
+ return NULL;
+
+ 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 < (int) 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 < (int) sizeof(uint16_t)) {
+ SDPERR("Unexpected end of packet");
+ free(d);
+ return NULL;
+ }
+ n = bt_get_be16(p);
+ p += sizeof(uint16_t);
+ *len += sizeof(uint16_t);
+ bufsize -= sizeof(uint16_t);
+ break;
+ default:
+ SDPERR("Sizeof text string > UINT16_MAX");
+ free(d);
+ return NULL;
+ }
+
+ 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);
+ SDPDBG("Str : %s", 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(const uint8_t *buf, int bufsize, uint8_t *dtdp, int *size)
+{
+ uint8_t dtd;
+ int scanned = sizeof(uint8_t);
+
+ if (bufsize < (int) 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 < (int) 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 < (int) sizeof(uint16_t)) {
+ SDPERR("Unexpected end of packet");
+ return 0;
+ }
+ *size = bt_get_be16(buf);
+ scanned += sizeof(uint16_t);
+ break;
+ case SDP_SEQ32:
+ case SDP_ALT32:
+ if (bufsize < (int) sizeof(uint32_t)) {
+ SDPERR("Unexpected end of packet");
+ return 0;
+ }
+ *size = bt_get_be32(buf);
+ scanned += sizeof(uint32_t);
+ break;
+ default:
+ SDPERR("Unknown sequence type, aborting");
+ return 0;
+ }
+ return scanned;
+}
+
+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));
+
+ if (!d)
+ return NULL;
+
+ SDPDBG("Extracting SEQ");
+ memset(d, 0, sizeof(sdp_data_t));
+ *len = sdp_extract_seqtype(p, bufsize, &d->dtd, &seqlen);
+ SDPDBG("Sequence Type : 0x%x length : 0x%x", 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;
+ prev = NULL;
+ while (n < seqlen) {
+ int attrlen = 0;
+ curr = sdp_extract_attr(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(const uint8_t *p, int bufsize, int *size,
+ sdp_record_t *rec)
+{
+ sdp_data_t *elem;
+ int n = 0;
+ uint8_t dtd;
+
+ if (bufsize < (int) 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", dtd);
+ return NULL;
+ }
+ *size += n;
+ return elem;
+}
+
+#ifdef SDP_DEBUG
+static void attr_print_func(void *value, void *userData)
+{
+ sdp_data_t *d = (sdp_data_t *)value;
+
+ SDPDBG("=====================================");
+ SDPDBG("ATTRIBUTE IDENTIFIER : 0x%x", d->attrId);
+ SDPDBG("ATTRIBUTE VALUE PTR : %p", value);
+ if (d)
+ sdp_data_print(d);
+ else
+ SDPDBG("NULL value");
+ SDPDBG("=====================================");
+}
+
+void sdp_print_service_attr(sdp_list_t *svcAttrList)
+{
+ SDPDBG("Printing service attr list %p", svcAttrList);
+ sdp_list_foreach(svcAttrList, attr_print_func, NULL);
+ SDPDBG("Printed service attr list %p", svcAttrList);
+}
+#endif
+
+sdp_record_t *sdp_extract_pdu(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(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 + (int) sizeof(uint16_t)) {
+ SDPERR("Unexpected end of packet");
+ break;
+ }
+
+ dtd = *(uint8_t *) p;
+ attr = bt_get_be16(p + n);
+ n += sizeof(uint16_t);
+
+ SDPDBG("DTD of attrId : %d Attr id : 0x%x ", dtd, attr);
+
+ data = sdp_extract_attr(p + n, bufsize - n, &attrlen, rec);
+
+ SDPDBG("Attr id : 0x%x attrValueLength : %d", 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");
+ sdp_print_service_attr(rec->attrlist);
+#endif
+ *scanned += seqlen;
+ return rec;
+}
+
+static void sdp_copy_pattern(void *value, void *udata)
+{
+ uuid_t *uuid = value;
+ sdp_record_t *rec = udata;
+
+ sdp_pattern_add_uuid(rec, uuid);
+}
+
+static void *sdp_data_value(sdp_data_t *data, uint32_t *len)
+{
+ void *val = NULL;
+
+ switch (data->dtd) {
+ case SDP_DATA_NIL:
+ break;
+ case SDP_UINT8:
+ val = &data->val.uint8;
+ break;
+ case SDP_INT8:
+ case SDP_BOOL:
+ val = &data->val.int8;
+ break;
+ case SDP_UINT16:
+ val = &data->val.uint16;
+ break;
+ case SDP_INT16:
+ val = &data->val.int16;
+ break;
+ case SDP_UINT32:
+ val = &data->val.uint32;
+ break;
+ case SDP_INT32:
+ val = &data->val.int32;
+ break;
+ case SDP_INT64:
+ val = &data->val.int64;
+ break;
+ case SDP_UINT64:
+ val = &data->val.uint64;
+ break;
+ case SDP_UINT128:
+ val = &data->val.uint128;
+ break;
+ case SDP_INT128:
+ val = &data->val.int128;
+ break;
+ case SDP_UUID16:
+ val = &data->val.uuid.value.uuid16;
+ break;
+ case SDP_UUID32:
+ val = &data->val.uuid.value.uuid32;
+ break;
+ case SDP_UUID128:
+ val = &data->val.uuid.value.uuid128;
+ break;
+ case SDP_URL_STR8:
+ case SDP_URL_STR16:
+ case SDP_TEXT_STR8:
+ case SDP_TEXT_STR16:
+ case SDP_URL_STR32:
+ case SDP_TEXT_STR32:
+ val = data->val.str;
+ if (len)
+ *len = data->unitSize - 1;
+ break;
+ case SDP_ALT8:
+ case SDP_ALT16:
+ case SDP_ALT32:
+ case SDP_SEQ8:
+ case SDP_SEQ16:
+ case SDP_SEQ32:
+ val = sdp_copy_seq(data->val.dataseq);
+ break;
+ }
+
+ return val;
+}
+
+static sdp_data_t *sdp_copy_seq(sdp_data_t *data)
+{
+ sdp_data_t *tmp, *seq = NULL, *cur = NULL;
+
+ for (tmp = data; tmp; tmp = tmp->next) {
+ sdp_data_t *datatmp;
+ void *value;
+
+ value = sdp_data_value(tmp, NULL);
+ datatmp = sdp_data_alloc_with_length(tmp->dtd, value,
+ tmp->unitSize);
+
+ if (cur)
+ cur->next = datatmp;
+ else
+ seq = datatmp;
+
+ cur = datatmp;
+ }
+
+ return seq;
+}
+
+static void sdp_copy_attrlist(void *value, void *udata)
+{
+ sdp_data_t *data = value;
+ sdp_record_t *rec = udata;
+ void *val;
+ uint32_t len = 0;
+
+ val = sdp_data_value(data, &len);
+
+ if (!len)
+ sdp_attr_add_new(rec, data->attrId, data->dtd, val);
+ else
+ sdp_attr_add_new_with_length(rec, data->attrId,
+ data->dtd, val, len);
+}
+
+sdp_record_t *sdp_copy_record(sdp_record_t *rec)
+{
+ sdp_record_t *cpy;
+
+ cpy = sdp_record_alloc();
+
+ cpy->handle = rec->handle;
+
+ sdp_list_foreach(rec->pattern, sdp_copy_pattern, cpy);
+ sdp_list_foreach(rec->attrlist, sdp_copy_attrlist, cpy);
+
+ cpy->svclass = rec->svclass;
+
+ return cpy;
+}
+
+#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 && SDP_IS_TEXT_STR(d->dtd))
+ printf("Service Name: %.*s\n", d->unitSize, d->val.str);
+ d = sdp_data_get(rec, SDP_ATTR_SVCDESC_PRIMARY);
+ if (d && SDP_IS_TEXT_STR(d->dtd))
+ printf("Service Description: %.*s\n", d->unitSize, d->val.str);
+ d = sdp_data_get(rec, SDP_ATTR_PROVNAME_PRIMARY);
+ if (d && SDP_IS_TEXT_STR(d->dtd))
+ 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");
+ 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", d->val.uint32);
+ break;
+ case SDP_UUID16:
+ case SDP_UUID32:
+ case SDP_UUID128:
+ SDPDBG("UUID");
+ sdp_uuid_print(&d->val.uuid);
+ break;
+ case SDP_TEXT_STR8:
+ case SDP_TEXT_STR16:
+ case SDP_TEXT_STR32:
+ SDPDBG("Text : %s", d->val.str);
+ break;
+ case SDP_URL_STR8:
+ case SDP_URL_STR16:
+ case SDP_URL_STR32:
+ SDPDBG("URL : %s", 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");
+ 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 p->data;
+ }
+ return NULL;
+}
+
+static 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;
+}
+
+static 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");
+ if (select(session->sock + 1, &readFds, NULL, NULL, &timeout) == 0) {
+ SDPERR("Client timed out");
+ 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:%m");
+ return -1;
+ }
+ n = sdp_read_rsp(session, rspbuf, SDP_RSP_BUFFER_SIZE);
+ if (0 > n)
+ return -1;
+ SDPDBG("Read : %d", 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 NULL;
+
+ 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 NULL;
+ 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 = 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 = p->data;
+ if (SDP_IS_UUID(seq->dtd) &&
+ sdp_uuid_to_proto(&seq->val.uuid) == proto)
+ return seq->next;
+ }
+ }
+ return NULL;
+}
+
+static int sdp_get_proto_descs(uint16_t attr_id, const sdp_record_t *rec,
+ sdp_list_t **pap)
+{
+ sdp_data_t *pdlist, *curr;
+ sdp_list_t *ap = NULL;
+
+ pdlist = sdp_data_get(rec, attr_id);
+ if (pdlist == NULL) {
+ errno = ENODATA;
+ return -1;
+ }
+
+ SDPDBG("Attribute value type: 0x%02x", pdlist->dtd);
+
+ if (attr_id == SDP_ATTR_ADD_PROTO_DESC_LIST) {
+ if (!SDP_IS_SEQ(pdlist->dtd)) {
+ errno = EINVAL;
+ return -1;
+ }
+ pdlist = pdlist->val.dataseq;
+ }
+
+ for (; pdlist; pdlist = pdlist->next) {
+ sdp_list_t *pds = NULL;
+
+ if (!SDP_IS_SEQ(pdlist->dtd) && !SDP_IS_ALT(pdlist->dtd))
+ goto failed;
+
+ for (curr = pdlist->val.dataseq; curr; curr = curr->next) {
+ if (!SDP_IS_SEQ(curr->dtd)) {
+ sdp_list_free(pds, NULL);
+ goto failed;
+ }
+ pds = sdp_list_append(pds, curr->val.dataseq);
+ }
+
+ ap = sdp_list_append(ap, pds);
+ }
+
+ *pap = ap;
+
+ return 0;
+
+failed:
+ sdp_list_foreach(ap, (sdp_list_func_t) sdp_list_free, NULL);
+ sdp_list_free(ap, NULL);
+ errno = EINVAL;
+
+ return -1;
+}
+
+int sdp_get_access_protos(const sdp_record_t *rec, sdp_list_t **pap)
+{
+ return sdp_get_proto_descs(SDP_ATTR_PROTO_DESC_LIST, rec, pap);
+}
+
+int sdp_get_add_access_protos(const sdp_record_t *rec, sdp_list_t **pap)
+{
+ return sdp_get_proto_descs(SDP_ATTR_ADD_PROTO_DESC_LIST, rec, pap);
+}
+
+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 && SDP_IS_SEQ(sdpdata->dtd)) {
+ 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) {
+ errno = EINVAL;
+ goto fail;
+ }
+
+ u = malloc(sizeof(uuid_t));
+ if (!u)
+ goto fail;
+
+ *u = d->val.uuid;
+ *seqp = sdp_list_append(*seqp, u);
+ }
+ return 0;
+ }
+fail:
+ sdp_list_free(*seqp, free);
+ *seqp = NULL;
+ 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 = malloc(len * sizeof(void *));
+ if (!dtds)
+ return -1;
+
+ values = malloc(len * sizeof(void *));
+ if (!values) {
+ free(dtds);
+ return -1;
+ }
+
+ for (p = seq, i = 0; i < len; i++, p = p->next) {
+ uuid_t *uuid = 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;
+ }
+
+ if (!SDP_IS_SEQ(sdpdata->dtd))
+ goto invalid;
+ curr_data = sdpdata->val.dataseq;
+
+ while (curr_data) {
+ sdp_data_t *pCode, *pEncoding, *pOffset;
+
+ pCode = curr_data;
+ if (pCode->dtd != SDP_UINT16)
+ goto invalid;
+
+ /* LanguageBaseAttributeIDList entries are always grouped as
+ * triplets */
+ if (!pCode->next || !pCode->next->next)
+ goto invalid;
+
+ pEncoding = pCode->next;
+ if (pEncoding->dtd != SDP_UINT16)
+ goto invalid;
+
+ pOffset = pEncoding->next;
+ if (pOffset->dtd != SDP_UINT16)
+ goto invalid;
+
+ lang = malloc(sizeof(sdp_lang_attr_t));
+ if (!lang) {
+ sdp_list_free(*langSeq, free);
+ *langSeq = NULL;
+ return -1;
+ }
+ lang->code_ISO639 = pCode->val.uint16;
+ lang->encoding = pEncoding->val.uint16;
+ lang->base_offset = pOffset->val.uint16;
+ SDPDBG("code_ISO639 : 0x%02x", lang->code_ISO639);
+ SDPDBG("encoding : 0x%02x", lang->encoding);
+ SDPDBG("base_offfset : 0x%02x", lang->base_offset);
+ *langSeq = sdp_list_append(*langSeq, lang);
+
+ curr_data = pOffset->next;
+ }
+
+ return 0;
+
+invalid:
+ sdp_list_free(*langSeq, free);
+ *langSeq = NULL;
+ errno = EINVAL;
+
+ return -1;
+}
+
+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 == NULL) {
+ errno = ENODATA;
+ return -1;
+ }
+
+ if (!SDP_IS_SEQ(sdpdata->dtd) || sdpdata->val.dataseq == NULL)
+ goto invalid;
+
+ for (seq = sdpdata->val.dataseq; seq; seq = seq->next) {
+ uuid_t *uuid = NULL;
+ uint16_t version = 0x100;
+
+ if (SDP_IS_UUID(seq->dtd)) {
+ /* Mac OS X 10.7.3 and old Samsung phones do not comply
+ * to the SDP specification for
+ * BluetoothProfileDescriptorList. This workaround
+ * allows to properly parse UUID/version from SDP
+ * record published by these systems. */
+ sdp_data_t *next = seq->next;
+ uuid = &seq->val.uuid;
+ if (next && next->dtd == SDP_UINT16) {
+ version = next->val.uint16;
+ seq = next;
+ }
+ } else if (SDP_IS_SEQ(seq->dtd)) {
+ sdp_data_t *puuid, *pVnum;
+
+ puuid = seq->val.dataseq;
+ if (puuid == NULL || !SDP_IS_UUID(puuid->dtd))
+ goto invalid;
+
+ uuid = &puuid->val.uuid;
+
+ pVnum = puuid->next;
+ if (pVnum == NULL || pVnum->dtd != SDP_UINT16)
+ goto invalid;
+
+ version = pVnum->val.uint16;
+ } else
+ goto invalid;
+
+ if (uuid != NULL) {
+ profDesc = malloc(sizeof(sdp_profile_desc_t));
+ if (!profDesc) {
+ sdp_list_free(*profDescSeq, free);
+ *profDescSeq = NULL;
+ return -1;
+ }
+ profDesc->uuid = *uuid;
+ profDesc->version = version;
+#ifdef SDP_DEBUG
+ sdp_uuid_print(&profDesc->uuid);
+ SDPDBG("Vnum : 0x%04x", profDesc->version);
+#endif
+ *profDescSeq = sdp_list_append(*profDescSeq, profDesc);
+ }
+ }
+ return 0;
+
+invalid:
+ sdp_list_free(*profDescSeq, free);
+ *profDescSeq = NULL;
+ errno = EINVAL;
+
+ return -1;
+}
+
+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;
+ }
+
+ if (!SDP_IS_SEQ(d->dtd) || d->val.dataseq == NULL)
+ goto invalid;
+
+ for (curr = d->val.dataseq; curr; curr = curr->next) {
+ if (curr->dtd != SDP_UINT16)
+ goto invalid;
+ *u16 = sdp_list_append(*u16, &curr->val.uint16);
+ }
+
+ return 0;
+
+invalid:
+ sdp_list_free(*u16, NULL);
+ *u16 = NULL;
+ errno = EINVAL;
+
+ return -1;
+}
+
+/* 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 (SDP_IS_TEXT_STR(sdpdata->dtd))
+ if ((int) 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;
+}
+
+static int sdp_attr_add_new_with_length(sdp_record_t *rec,
+ uint16_t attr, uint8_t dtd, const void *value, uint32_t len)
+{
+ sdp_data_t *d;
+
+ d = sdp_data_alloc_with_length(dtd, value, len);
+ if (!d)
+ return -1;
+
+ sdp_attr_replace(rec, attr, d);
+
+ return 0;
+}
+
+/*
+ * 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, name);
+ if (prov)
+ sdp_attr_add_new(rec, SDP_ATTR_PROVNAME_PRIMARY,
+ SDP_TEXT_STR8, prov);
+ if (desc)
+ sdp_attr_add_new(rec, SDP_ATTR_SVCDESC_PRIMARY,
+ SDP_TEXT_STR8, 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 = malloc(seqlen * sizeof(void *));
+ if (!seqDTDs)
+ return NULL;
+
+ seqs = malloc(seqlen * sizeof(void *));
+ if (!seqs) {
+ free(seqDTDs);
+ return NULL;
+ }
+
+ for (i = 0, p = proto; p; p = p->next, i++) {
+ sdp_list_t *elt = p->data;
+ sdp_data_t *s;
+ uuid_t *uuid = NULL;
+ unsigned int pslen = 0;
+ for (; elt && pslen < ARRAY_SIZE(dtds); elt = elt->next, pslen++) {
+ sdp_data_t *d = elt->data;
+ dtds[pslen] = &d->dtd;
+ switch (d->dtd) {
+ case SDP_UUID16:
+ uuid = (uuid_t *) d;
+ values[pslen] = &uuid->value.uuid16;
+ break;
+ case SDP_UUID32:
+ uuid = (uuid_t *) d;
+ values[pslen] = &uuid->value.uuid32;
+ break;
+ case SDP_UUID128:
+ uuid = (uuid_t *) d;
+ values[pslen] = &uuid->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;
+ if (uuid)
+ sdp_pattern_add_uuid(rec, uuid);
+ }
+ }
+ 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, 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, 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, **values;
+ const sdp_list_t *p;
+
+ dtds = malloc(3 * seqlen * sizeof(void *));
+ if (!dtds)
+ return -1;
+
+ values = malloc(3 * seqlen * sizeof(void *));
+ if (!values) {
+ free(dtds);
+ return -1;
+ }
+
+ for (p = seq; p; p = p->next) {
+ sdp_lang_attr_t *lang = 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, **seqs;
+ const sdp_list_t *p;
+ sdp_data_t *pAPSeq;
+
+ seqDTDs = malloc(seqlen * sizeof(void *));
+ if (!seqDTDs)
+ return -1;
+
+ seqs = malloc(seqlen * sizeof(void *));
+ if (!seqs) {
+ free(seqDTDs);
+ return -1;
+ }
+
+ for (p = profiles; p; p = p->next) {
+ sdp_data_t *seq;
+ void *dtds[2], *values[2];
+ sdp_profile_desc_t *profile = p->data;
+ if (!profile) {
+ status = -1;
+ goto end;
+ }
+ 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;
+ goto end;
+ }
+ dtds[1] = &uint16;
+ values[1] = &profile->version;
+ seq = sdp_seq_alloc(dtds, values, 2);
+
+ if (seq == NULL) {
+ status = -1;
+ goto end;
+ }
+
+ seqDTDs[i] = &seq->dtd;
+ seqs[i] = seq;
+ sdp_pattern_add_uuid(rec, &profile->uuid);
+ i++;
+ }
+
+ pAPSeq = sdp_seq_alloc(seqDTDs, seqs, seqlen);
+ sdp_attr_add(rec, SDP_ATTR_PFILE_DESC_LIST, pAPSeq);
+end:
+ 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);
+}
+
+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_uuid_cmp(const void *p1, const void *p2)
+{
+ uuid_t *u1 = sdp_uuid_to_uuid128(p1);
+ uuid_t *u2 = sdp_uuid_to_uuid128(p2);
+ int ret;
+
+ ret = sdp_uuid128_cmp(u1, u2);
+
+ bt_free(u1);
+ bt_free(u2);
+
+ return ret;
+}
+
+/*
+ * UUID comparison function
+ * returns 0 if uuidValue1 == uuidValue2 else -1
+ */
+int sdp_uuid16_cmp(const void *p1, const void *p2)
+{
+ const uuid_t *u1 = p1;
+ const uuid_t *u2 = 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 = p1;
+ const uuid_t *u2 = 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, const 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, const 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(const uuid_t *uuid)
+{
+ uuid_t *uuid128 = bt_malloc(sizeof(uuid_t));
+
+ if (!uuid128)
+ return NULL;
+
+ 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;
+ unsigned 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 = *p;
+
+ SDPDBG("Append src size: %d", len);
+ SDPDBG("Append dst size: %d", dst->data_size);
+ SDPDBG("Dst buffer size: %d", dst->buf_size);
+ if (dst->data_size == 0 && dtd == 0) {
+ /* create initial sequence */
+ *p = SDP_SEQ8;
+ dst->data_size += sizeof(uint8_t);
+ /* reserve space for sequence size */
+ 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 = SDP_SEQ16;
+ dst->data_size += 1;
+ }
+ 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_be16(dst->data_size - sizeof(uint8_t) - sizeof(uint16_t), p);
+ break;
+ case SDP_SEQ32:
+ bt_put_be32(dst->data_size - sizeof(uint8_t) - sizeof(uint32_t), p);
+ break;
+ }
+}
+
+void sdp_append_to_pdu(sdp_buf_t *pdu, sdp_data_t *d)
+{
+ sdp_buf_t append;
+
+ memset(&append, 0, sizeof(sdp_buf_t));
+ sdp_gen_buffer(&append, d);
+ append.data = malloc(append.buf_size);
+ if (!append.data)
+ return;
+
+ sdp_set_attrid(&append, d->attrId);
+ sdp_gen_pdu(&append, d);
+ sdp_append_to_buf(pdu, append.data, append.data_size);
+ free(append.data);
+}
+
+/*
+ * 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 = bt_get_be32(p);
+ }
+
+end:
+ free(req);
+ 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_be32(handle, 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);
+
+ 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;
+ } else {
+ uint16_t tmp;
+
+ memcpy(&tmp, p, sizeof(tmp));
+
+ status = tmp;
+ }
+end:
+ free(reqbuf);
+ 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_be32(handle, 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", status);
+
+ rsphdr = (sdp_pdu_hdr_t *) rspbuf;
+ p = rspbuf + sizeof(sdp_pdu_hdr_t);
+
+ 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;
+ } else {
+ uint16_t tmp;
+
+ memcpy(&tmp, p, sizeof(tmp));
+
+ status = tmp;
+ }
+end:
+ free(reqbuf);
+ 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(void)
+{
+ sdp_record_t *rec = malloc(sizeof(sdp_record_t));
+
+ if (!rec)
+ return NULL;
+
+ memset(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("Elements in target pattern : %d", sdp_list_len(rec->pattern));
+ SDPDBG("Trying to add : 0x%lx", (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", 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, unsigned int *scanned)
+{
+ sdp_list_t *pSeq = *seq;
+ uint8_t *pdata = pdu;
+ int n;
+
+ for (n = 0; n < count; n++) {
+ uint32_t *pSvcRec;
+ if (bufsize < (int) sizeof(uint32_t)) {
+ SDPERR("Unexpected end of packet");
+ break;
+ }
+ pSvcRec = malloc(sizeof(uint32_t));
+ if (!pSvcRec)
+ break;
+ *pSvcRec = bt_get_be32(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("");
+
+ SDPDBG("Seq length : %d", seqlen);
+
+ types = malloc(seqlen * sizeof(void *));
+ if (!types)
+ return -ENOMEM;
+
+ values = malloc(seqlen * sizeof(void *));
+ if (!values) {
+ free(types);
+ return -ENOMEM;
+ }
+
+ 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);
+ if (!dataseq) {
+ free(types);
+ free(values);
+ return -ENOMEM;
+ }
+
+ memset(&buf, 0, sizeof(sdp_buf_t));
+ sdp_gen_buffer(&buf, dataseq);
+ buf.data = malloc(buf.buf_size);
+
+ if (!buf.data) {
+ sdp_data_free(dataseq);
+ free(types);
+ free(values);
+ return -ENOMEM;
+ }
+
+ SDPDBG("Data Seq : 0x%p", seq);
+ seqlen = sdp_gen_pdu(&buf, dataseq);
+ SDPDBG("Copying : %d", 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 = 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 rec_count;
+ unsigned scanned, 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", seqlen);
+
+ /* set the length and increment the pointer */
+ reqsize += seqlen;
+ pdata += seqlen;
+
+ /* specify the maximum svc rec count that client expects */
+ bt_put_be16(max_rec_num, 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", 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 */
+ pdata += sizeof(uint16_t);
+ scanned += sizeof(uint16_t);
+ pdata_len -= sizeof(uint16_t);
+ rec_count = bt_get_be16(pdata);
+ pdata += sizeof(uint16_t);
+ scanned += sizeof(uint16_t);
+ pdata_len -= sizeof(uint16_t);
+
+ SDPDBG("Current svc count: %d", rec_count);
+ SDPDBG("ResponseLength: %d", rsplen);
+
+ if (!rec_count) {
+ status = -1;
+ goto end;
+ }
+ extract_record_handle_seq(pdata, pdata_len, rsp, rec_count, &scanned);
+ SDPDBG("BytesScanned : %d", 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", cstate_len);
+ } else
+ cstate = NULL;
+ }
+ } while (cstate);
+
+end:
+ free(reqbuf);
+ 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)
+{
+ uint32_t reqsize = 0, _reqsize;
+ uint32_t rspsize = 0, rsp_count;
+ int attr_list_len = 0;
+ int seqlen = 0;
+ unsigned int 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 NULL;
+ }
+
+ memset(&rsp_concat_buf, 0, sizeof(sdp_buf_t));
+
+ reqbuf = malloc(SDP_REQ_BUFFER_SIZE);
+ rspbuf = malloc(SDP_RSP_BUFFER_SIZE);
+ if (!reqbuf || !rspbuf) {
+ errno = ENOMEM;
+ goto end;
+ }
+ 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_be32(handle, pdata);
+ reqsize += sizeof(uint32_t);
+ pdata += sizeof(uint32_t);
+
+ /* specify the response limit */
+ bt_put_be16(65535, 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;
+ goto end;
+ }
+ pdata += seqlen;
+ reqsize += seqlen;
+ SDPDBG("Attr list length : %d", seqlen);
+
+ /* save before Continuation State */
+ _pdata = pdata;
+ _reqsize = reqsize;
+
+ do {
+ int status;
+
+ /* 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");
+ goto end;
+ }
+
+ rsphdr = (sdp_pdu_hdr_t *) rspbuf;
+ if (rsphdr->pdu_id == SDP_ERROR_RSP) {
+ SDPDBG("PDU ID : 0x%x", rsphdr->pdu_id);
+ 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");
+ goto end;
+ }
+
+ rsp_count = bt_get_be16(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");
+ goto end;
+ }
+ cstate_len = *(uint8_t *) (pdata + rsp_count);
+
+ SDPDBG("Response id : %d", rsphdr->pdu_id);
+ SDPDBG("Attrlist byte count : %d", rsp_count);
+ SDPDBG("sdp_cstate_t length : %d", 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(pdata, pdata_len, &scanned);
+ }
+
+end:
+ free(reqbuf);
+ free(rsp_concat_buf.data);
+ 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 incoming and outgoing data are stored in the transaction structure
+ * buffers. When there is incoming 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;
+
+ /* clean possible allocated buffer */
+ 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", seqlen);
+
+ /* now set the length and increment the pointer */
+ t->reqsize += seqlen;
+ pdata += seqlen;
+
+ bt_put_be16(max_rec_num, 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:%m");
+ t->err = errno;
+ goto end;
+ }
+
+ return 0;
+end:
+
+ free(t->reqbuf);
+ t->reqbuf = NULL;
+
+ return -1;
+}
+
+/*
+ * This function starts an asynchronous service attribute request.
+ * The incoming and outgoing data are stored in the transaction structure
+ * buffers. When there is incoming 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;
+
+ /* clean possible allocated buffer */
+ 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_be32(handle, pdata);
+ t->reqsize += sizeof(uint32_t);
+ pdata += sizeof(uint32_t);
+
+ /* specify the response limit */
+ bt_put_be16(65535, 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", 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:%m");
+ t->err = errno;
+ goto end;
+ }
+
+ return 0;
+end:
+
+ 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 incoming
+ * and outgoing data are stored in the transaction structure buffers. When there
+ * is incoming 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;
+
+ /* clean possible allocated buffer */
+ 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", seqlen);
+
+ /* now set the length and increment the pointer */
+ t->reqsize += seqlen;
+ pdata += seqlen;
+
+ bt_put_be16(SDP_MAX_ATTR_LEN, pdata);
+ t->reqsize += sizeof(uint16_t);
+ pdata += sizeof(uint16_t);
+
+ SDPDBG("Max attr byte count : %d", 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", 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:%m");
+ t->err = errno;
+ goto end;
+ }
+
+ return 0;
+end:
+
+ 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 incoming 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:%m (%d)", 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:%m (%d)", errno);
+ t->err = errno;
+ goto end;
+ }
+
+ if (n == 0 || reqhdr->tid != rsphdr->tid ||
+ (n != (int) (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 = bt_get_be16(ssr_pdata);
+ ssr_pdata += sizeof(uint16_t);
+ csrc = bt_get_be16(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", tsrc);
+ SDPDBG("Current svc count: %d", 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 if (t->rsp_concat_buf.data_size >= sizeof(uint16_t) * 2) {
+ /* point to the first csrc */
+ uint8_t *pcsrc = t->rsp_concat_buf.data + 2;
+ uint16_t tcsrc, tcsrc2;
+
+ /* 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 */
+ memcpy(&tcsrc, pcsrc, sizeof(tcsrc));
+ memcpy(&tcsrc2, pdata, sizeof(tcsrc2));
+ tcsrc += tcsrc2;
+ memcpy(pcsrc, &tcsrc, sizeof(tcsrc));
+
+ pdata += sizeof(uint16_t); /* point to the first handle */
+ rsp_count = csrc * 4;
+ } else {
+ t->err = EPROTO;
+ SDPERR("Protocol error: invalid PDU size");
+ status = SDP_INVALID_PDU_SIZE;
+ goto end;
+ }
+ status = 0x0000;
+ break;
+ case SDP_SVC_ATTR_RSP:
+ case SDP_SVC_SEARCH_ATTR_RSP:
+ rsp_count = bt_get_be16(pdata);
+ SDPDBG("Attrlist byte count : %d", rsp_count);
+
+ /* Valid range for rsp_count is 0x0002-0xFFFF */
+ if (t->rsp_concat_buf.data_size == 0 && rsp_count < 0x0002) {
+ t->err = EPROTO;
+ SDPERR("Protocol error: invalid AttrList size");
+ status = SDP_INVALID_PDU_SIZE;
+ goto end;
+ }
+
+ /*
+ * 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 = bt_get_be16(pdata);
+ size = ntohs(rsphdr->plen);
+
+ goto end;
+ default:
+ t->err = EPROTO;
+ SDPERR("Illegal PDU ID: 0x%x", rsphdr->pdu_id);
+ goto end;
+ }
+
+ /* Out of bound check before using rsp_count as offset for
+ * continuation state, which has at least a one byte size
+ * field.
+ */
+ if ((n - (int) sizeof(sdp_pdu_hdr_t)) < plen + 1) {
+ t->err = EPROTO;
+ SDPERR("Protocol error: invalid PDU size");
+ status = SDP_INVALID_PDU_SIZE;
+ goto end;
+ }
+
+ pcstate = (sdp_cstate_t *) (pdata + rsp_count);
+
+ SDPDBG("Cstate length : %d", 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 - (int) 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:%m(%d)", 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);
+ }
+
+ 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;
+ unsigned int 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;
+ }
+
+ memset(&rsp_concat_buf, 0, sizeof(sdp_buf_t));
+
+ 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_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);
+ if (seqlen < 0) {
+ errno = EINVAL;
+ status = -1;
+ goto end;
+ }
+
+ SDPDBG("Data seq added : %d", seqlen);
+
+ /* now set the length and increment the pointer */
+ reqsize += seqlen;
+ pdata += seqlen;
+
+ bt_put_be16(SDP_MAX_ATTR_LEN, pdata);
+ reqsize += sizeof(uint16_t);
+ pdata += sizeof(uint16_t);
+
+ SDPDBG("Max attr byte count : %d", 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) {
+ errno = EINVAL;
+ status = -1;
+ goto end;
+ }
+ pdata += seqlen;
+ SDPDBG("Attr list length : %d", 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", 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 = bt_get_be16(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", attr_list_len);
+ SDPDBG("Response byte count : %d", rsp_count);
+ SDPDBG("Cstate length : %d", 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(pdata, pdata_len, &dataType, &seqlen);
+
+ SDPDBG("Bytes scanned : %d", scanned);
+ SDPDBG("Seq length : %d", seqlen);
+
+ if (scanned && seqlen) {
+ pdata += scanned;
+ pdata_len -= scanned;
+ do {
+ int recsize = 0;
+ sdp_record_t *rec = sdp_extract_pdu(pdata, pdata_len, &recsize);
+ if (rec == NULL) {
+ SDPERR("SVC REC is null");
+ status = -1;
+ goto end;
+ }
+ if (!recsize) {
+ sdp_record_free(rec);
+ break;
+ }
+ scanned += recsize;
+ pdata += recsize;
+ pdata_len -= recsize;
+
+ SDPDBG("Loc seq length : %d", recsize);
+ SDPDBG("Svc Rec Handle : 0x%x", rec->handle);
+ SDPDBG("Bytes scanned : %d", scanned);
+ SDPDBG("Attrlist byte count : %d", 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");
+ *rsp = rec_list;
+ }
+ }
+end:
+ free(rsp_concat_buf.data);
+ free(reqbuf);
+ 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:%m");
+ 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) {
+ free(t->reqbuf);
+
+ 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 | SOCK_CLOEXEC, 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 set_l2cap_mtu(int sk, uint16_t mtu)
+{
+ struct l2cap_options l2o;
+ socklen_t len;
+
+ memset(&l2o, 0, sizeof(l2o));
+ len = sizeof(l2o);
+
+ if (getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &len) < 0)
+ return -1;
+
+ l2o.imtu = mtu;
+ l2o.omtu = mtu;
+
+ if (setsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, sizeof(l2o)) < 0)
+ return -1;
+
+ return 0;
+}
+
+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;
+ int sockflags = SOCK_SEQPACKET | SOCK_CLOEXEC;
+
+ if (flags & SDP_NON_BLOCKING)
+ sockflags |= SOCK_NONBLOCK;
+
+ session->sock = socket(PF_BLUETOOTH, sockflags, BTPROTO_L2CAP);
+ if (session->sock < 0)
+ return -1;
+ session->local = 0;
+
+ sk = session->sock;
+
+ memset(&sa, 0, sizeof(sa));
+
+ 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));
+ }
+
+ if ((flags & SDP_LARGE_MTU) &&
+ set_l2cap_mtu(sk, SDP_LARGE_L2CAP_MTU) < 0)
+ return -1;
+
+ 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);
+ 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++;
+}
+
+/*
+ * Set the supported features
+ */
+int sdp_set_supp_feat(sdp_record_t *rec, const sdp_list_t *sf)
+{
+ const sdp_list_t *p, *r;
+ sdp_data_t *feat, *seq_feat;
+ int seqlen, i;
+ void **seqDTDs, **seqVals;
+
+ seqlen = sdp_list_len(sf);
+ seqDTDs = malloc(seqlen * sizeof(void *));
+ if (!seqDTDs)
+ return -1;
+ seqVals = malloc(seqlen * sizeof(void *));
+ if (!seqVals) {
+ free(seqDTDs);
+ return -1;
+ }
+
+ for (p = sf, i = 0; p; p = p->next, i++) {
+ int plen, j;
+ void **dtds, **vals;
+ int *lengths;
+
+ plen = sdp_list_len(p->data);
+ dtds = malloc(plen * sizeof(void *));
+ if (!dtds)
+ goto fail;
+ vals = malloc(plen * sizeof(void *));
+ if (!vals) {
+ free(dtds);
+ goto fail;
+ }
+ lengths = malloc(plen * sizeof(int));
+ if (!lengths) {
+ free(dtds);
+ free(vals);
+ goto fail;
+ }
+ for (r = p->data, j = 0; r; r = r->next, j++) {
+ sdp_data_t *data = (sdp_data_t *) r->data;
+ dtds[j] = &data->dtd;
+ switch (data->dtd) {
+ case SDP_URL_STR8:
+ case SDP_URL_STR16:
+ case SDP_TEXT_STR8:
+ case SDP_TEXT_STR16:
+ vals[j] = data->val.str;
+ lengths[j] = data->unitSize - sizeof(uint8_t);
+ break;
+ case SDP_ALT8:
+ case SDP_ALT16:
+ case SDP_ALT32:
+ case SDP_SEQ8:
+ case SDP_SEQ16:
+ case SDP_SEQ32:
+ vals[j] = data->val.dataseq;
+ lengths[j] = 0;
+ break;
+ default:
+ vals[j] = &data->val;
+ lengths[j] = 0;
+ break;
+ }
+ }
+ feat = sdp_seq_alloc_with_length(dtds, vals, lengths, plen);
+ free(dtds);
+ free(vals);
+ free(lengths);
+ if (!feat)
+ goto fail;
+ seqDTDs[i] = &feat->dtd;
+ seqVals[i] = feat;
+ }
+ seq_feat = sdp_seq_alloc(seqDTDs, seqVals, seqlen);
+ if (!seq_feat)
+ goto fail;
+ sdp_attr_replace(rec, SDP_ATTR_SUPPORTED_FEATURES_LIST, seq_feat);
+
+ free(seqVals);
+ free(seqDTDs);
+ return 0;
+
+fail:
+ free(seqVals);
+ free(seqDTDs);
+ return -1;
+}
+
+/*
+ * Get the supported features
+ * If an error occurred -1 is returned and errno is set
+ */
+int sdp_get_supp_feat(const sdp_record_t *rec, sdp_list_t **seqp)
+{
+ sdp_data_t *sdpdata, *d;
+ sdp_list_t *tseq;
+ tseq = NULL;
+
+ sdpdata = sdp_data_get(rec, SDP_ATTR_SUPPORTED_FEATURES_LIST);
+
+ if (!sdpdata || !SDP_IS_SEQ(sdpdata->dtd))
+ return sdp_get_uuidseq_attr(rec,
+ SDP_ATTR_SUPPORTED_FEATURES_LIST, seqp);
+
+ for (d = sdpdata->val.dataseq; d; d = d->next) {
+ sdp_data_t *dd;
+ sdp_list_t *subseq;
+
+ if (!SDP_IS_SEQ(d->dtd))
+ goto fail;
+
+ subseq = NULL;
+
+ for (dd = d->val.dataseq; dd; dd = dd->next) {
+ sdp_data_t *data;
+ void *val;
+ int length;
+
+ switch (dd->dtd) {
+ case SDP_URL_STR8:
+ case SDP_URL_STR16:
+ case SDP_TEXT_STR8:
+ case SDP_TEXT_STR16:
+ val = dd->val.str;
+ length = dd->unitSize - sizeof(uint8_t);
+ break;
+ case SDP_UINT8:
+ case SDP_UINT16:
+ val = &dd->val;
+ length = 0;
+ break;
+ default:
+ goto fail;
+ }
+
+ data = sdp_data_alloc_with_length(dd->dtd, val, length);
+ if (data)
+ subseq = sdp_list_append(subseq, data);
+ }
+ tseq = sdp_list_append(tseq, subseq);
+ }
+ *seqp = tseq;
+ return 0;
+
+fail:
+ while (tseq) {
+ sdp_list_t * next;
+
+ next = tseq->next;
+ sdp_list_free(tseq, free);
+ tseq = next;
+ }
+ errno = EINVAL;
+ return -1;
+}
+
+void sdp_add_lang_attr(sdp_record_t *rec)
+{
+ sdp_lang_attr_t base_lang;
+ sdp_list_t *langs;
+
+ base_lang.code_ISO639 = (0x65 << 8) | 0x6e;
+ base_lang.encoding = 106;
+ base_lang.base_offset = SDP_PRIMARY_LANG_BASE;
+
+ langs = sdp_list_append(0, &base_lang);
+ sdp_set_lang_attr(rec, langs);
+ sdp_list_free(langs, NULL);
+}
diff --git a/drive-sdk/deps/bzle/src/utils.c b/drive-sdk/deps/bzle/src/utils.c
new file mode 100644
index 0000000..db1f5a0
--- /dev/null
+++ b/drive-sdk/deps/bzle/src/utils.c
@@ -0,0 +1,120 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2011 Nokia Corporation
+ *
+ *
+ * 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 <stdlib.h>
+#include <glib.h>
+
+#include <bzle/bluetooth/bluetooth.h>
+#include <bzle/bluetooth/hci.h>
+#include <bzle/bluetooth/hci_lib.h>
+#include <bzle/bluetooth/sdp.h>
+
+#include <bzle/bluetooth/uuid.h>
+#include <bzle/bluetooth/btio.h>
+#include <bzle/gatt/att.h>
+#include <bzle/gatt/gattrib.h>
+#include <bzle/gatt/gatt.h>
+
+GIOChannel *gatt_connect(const char *src, const char *dst,
+ const char *dst_type, const char *sec_level,
+ int psm, int mtu, BtIOConnect connect_cb,
+ GError **gerr)
+{
+ GIOChannel *chan;
+ bdaddr_t sba, dba;
+ uint8_t dest_type;
+ GError *tmp_err = NULL;
+ BtIOSecLevel sec;
+
+ str2ba(dst, &dba);
+
+ /* Local adapter */
+ if (src != NULL) {
+ if (!strncmp(src, "hci", 3))
+ hci_devba(atoi(src + 3), &sba);
+ else
+ str2ba(src, &sba);
+ } else
+ bacpy(&sba, BDADDR_ANY);
+
+ /* Not used for BR/EDR */
+ if (strcmp(dst_type, "random") == 0)
+ dest_type = BDADDR_LE_RANDOM;
+ else
+ dest_type = BDADDR_LE_PUBLIC;
+
+ if (strcmp(sec_level, "medium") == 0)
+ sec = BT_IO_SEC_MEDIUM;
+ else if (strcmp(sec_level, "high") == 0)
+ sec = BT_IO_SEC_HIGH;
+ else
+ sec = BT_IO_SEC_LOW;
+
+ if (psm == 0)
+ chan = bt_io_connect(connect_cb, NULL, NULL, &tmp_err,
+ BT_IO_OPT_SOURCE_BDADDR, &sba,
+ BT_IO_OPT_SOURCE_TYPE, BDADDR_LE_PUBLIC,
+ BT_IO_OPT_DEST_BDADDR, &dba,
+ BT_IO_OPT_DEST_TYPE, dest_type,
+ BT_IO_OPT_CID, ATT_CID,
+ BT_IO_OPT_SEC_LEVEL, sec,
+ BT_IO_OPT_INVALID);
+ else
+ chan = bt_io_connect(connect_cb, NULL, NULL, &tmp_err,
+ BT_IO_OPT_SOURCE_BDADDR, &sba,
+ BT_IO_OPT_DEST_BDADDR, &dba,
+ BT_IO_OPT_PSM, psm,
+ BT_IO_OPT_IMTU, mtu,
+ BT_IO_OPT_SEC_LEVEL, sec,
+ BT_IO_OPT_INVALID);
+
+ if (tmp_err) {
+ g_propagate_error(gerr, tmp_err);
+ return NULL;
+ }
+
+ return chan;
+}
+
+size_t gatt_attr_data_from_string(const char *str, uint8_t **data)
+{
+ char tmp[3];
+ size_t size, i;
+
+ size = strlen(str) / 2;
+ *data = g_try_malloc0(size);
+ if (*data == NULL)
+ return 0;
+
+ tmp[2] = '\0';
+ for (i = 0; i < size; i++) {
+ memcpy(tmp, str + (i * 2), 2);
+ (*data)[i] = (uint8_t) strtol(tmp, NULL, 16);
+ }
+
+ return size;
+}
diff --git a/drive-sdk/deps/bzle/src/uuid.c b/drive-sdk/deps/bzle/src/uuid.c
new file mode 100644
index 0000000..08278be
--- /dev/null
+++ b/drive-sdk/deps/bzle/src/uuid.c
@@ -0,0 +1,278 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2011 Nokia Corporation
+ * Copyright (C) 2011 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 <string.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include <bzle/bluetooth/uuid.h>
+
+#if __BYTE_ORDER == __BIG_ENDIAN
+static uint128_t bluetooth_base_uuid = {
+ .data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
+ 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }
+};
+
+#define BASE_UUID16_OFFSET 2
+#define BASE_UUID32_OFFSET 0
+
+#else
+static uint128_t bluetooth_base_uuid = {
+ .data = { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
+ 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
+};
+
+#define BASE_UUID16_OFFSET 12
+#define BASE_UUID32_OFFSET BASE_UUID16_OFFSET
+
+#endif
+
+static void bt_uuid16_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst)
+{
+ dst->value.u128 = bluetooth_base_uuid;
+ dst->type = BT_UUID128;
+
+ memcpy(&dst->value.u128.data[BASE_UUID16_OFFSET],
+ &src->value.u16, sizeof(src->value.u16));
+}
+
+static void bt_uuid32_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst)
+{
+ dst->value.u128 = bluetooth_base_uuid;
+ dst->type = BT_UUID128;
+
+ memcpy(&dst->value.u128.data[BASE_UUID32_OFFSET],
+ &src->value.u32, sizeof(src->value.u32));
+}
+
+void bt_uuid_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst)
+{
+ switch (src->type) {
+ case BT_UUID128:
+ *dst = *src;
+ break;
+ case BT_UUID32:
+ bt_uuid32_to_uuid128(src, dst);
+ break;
+ case BT_UUID16:
+ bt_uuid16_to_uuid128(src, dst);
+ break;
+ default:
+ break;
+ }
+}
+
+static int bt_uuid128_cmp(const bt_uuid_t *u1, const bt_uuid_t *u2)
+{
+ return memcmp(&u1->value.u128, &u2->value.u128, sizeof(uint128_t));
+}
+
+int bt_uuid16_create(bt_uuid_t *btuuid, uint16_t value)
+{
+ memset(btuuid, 0, sizeof(bt_uuid_t));
+ btuuid->type = BT_UUID16;
+ btuuid->value.u16 = value;
+
+ return 0;
+}
+
+int bt_uuid32_create(bt_uuid_t *btuuid, uint32_t value)
+{
+ memset(btuuid, 0, sizeof(bt_uuid_t));
+ btuuid->type = BT_UUID32;
+ btuuid->value.u32 = value;
+
+ return 0;
+}
+
+int bt_uuid128_create(bt_uuid_t *btuuid, uint128_t value)
+{
+ memset(btuuid, 0, sizeof(bt_uuid_t));
+ btuuid->type = BT_UUID128;
+ btuuid->value.u128 = value;
+
+ return 0;
+}
+
+int bt_uuid_cmp(const bt_uuid_t *uuid1, const bt_uuid_t *uuid2)
+{
+ bt_uuid_t u1, u2;
+
+ bt_uuid_to_uuid128(uuid1, &u1);
+ bt_uuid_to_uuid128(uuid2, &u2);
+
+ return bt_uuid128_cmp(&u1, &u2);
+}
+
+/*
+ * convert the UUID to string, copying a maximum of n characters.
+ */
+int bt_uuid_to_string(const bt_uuid_t *uuid, char *str, size_t n)
+{
+ if (!uuid) {
+ snprintf(str, n, "NULL");
+ return -EINVAL;
+ }
+
+ switch (uuid->type) {
+ case BT_UUID16:
+ snprintf(str, n, "%.4x", uuid->value.u16);
+ break;
+ case BT_UUID32:
+ snprintf(str, n, "%.8x", uuid->value.u32);
+ break;
+ case BT_UUID128: {
+ unsigned int data0;
+ unsigned short data1;
+ unsigned short data2;
+ unsigned short data3;
+ unsigned int data4;
+ unsigned short data5;
+
+ uint128_t nvalue;
+ const uint8_t *data = (uint8_t *) &nvalue;
+
+ hton128(&uuid->value.u128, &nvalue);
+
+ memcpy(&data0, &data[0], 4);
+ memcpy(&data1, &data[4], 2);
+ memcpy(&data2, &data[6], 2);
+ memcpy(&data3, &data[8], 2);
+ memcpy(&data4, &data[10], 4);
+ memcpy(&data5, &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 -EINVAL; /* Enum type of UUID not set */
+ }
+
+ return 0;
+}
+
+static inline int is_uuid128(const char *string)
+{
+ return (strlen(string) == 36 &&
+ string[8] == '-' &&
+ string[13] == '-' &&
+ string[18] == '-' &&
+ string[23] == '-');
+}
+
+static inline int is_uuid32(const char *string)
+{
+ return (strlen(string) == 8 || strlen(string) == 10);
+}
+
+static inline int is_uuid16(const char *string)
+{
+ return (strlen(string) == 4 || strlen(string) == 6);
+}
+
+static int bt_string_to_uuid16(bt_uuid_t *uuid, const char *string)
+{
+ uint16_t u16;
+ char *endptr = NULL;
+
+ u16 = strtol(string, &endptr, 16);
+ if (endptr && *endptr == '\0') {
+ bt_uuid16_create(uuid, u16);
+ return 0;
+ }
+
+ return -EINVAL;
+}
+
+static int bt_string_to_uuid32(bt_uuid_t *uuid, const char *string)
+{
+ uint32_t u32;
+ char *endptr = NULL;
+
+ u32 = strtol(string, &endptr, 16);
+ if (endptr && *endptr == '\0') {
+ bt_uuid32_create(uuid, u32);
+ return 0;
+ }
+
+ return -EINVAL;
+}
+
+static int bt_string_to_uuid128(bt_uuid_t *uuid, const char *string)
+{
+ uint32_t data0, data4;
+ uint16_t data1, data2, data3, data5;
+ uint128_t n128, u128;
+ uint8_t *val = (uint8_t *) &n128;
+
+ if (sscanf(string, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
+ &data0, &data1, &data2,
+ &data3, &data4, &data5) != 6)
+ return -EINVAL;
+
+ data0 = htonl(data0);
+ data1 = htons(data1);
+ data2 = htons(data2);
+ data3 = htons(data3);
+ data4 = htonl(data4);
+ data5 = htons(data5);
+
+ memcpy(&val[0], &data0, 4);
+ memcpy(&val[4], &data1, 2);
+ memcpy(&val[6], &data2, 2);
+ memcpy(&val[8], &data3, 2);
+ memcpy(&val[10], &data4, 4);
+ memcpy(&val[14], &data5, 2);
+
+ ntoh128(&n128, &u128);
+
+ bt_uuid128_create(uuid, u128);
+
+ return 0;
+}
+
+int bt_string_to_uuid(bt_uuid_t *uuid, const char *string)
+{
+ if (is_uuid128(string))
+ return bt_string_to_uuid128(uuid, string);
+ else if (is_uuid32(string))
+ return bt_string_to_uuid32(uuid, string);
+ else if (is_uuid16(string))
+ return bt_string_to_uuid16(uuid, string);
+
+ return -EINVAL;
+}
+
+int bt_uuid_strcmp(const void *a, const void *b)
+{
+ return strcasecmp(a, b);
+}