* [PATCH BlueZ 1/2] android/hid: Fetch record before attempting to connect
@ 2013-10-31 15:57 Luiz Augusto von Dentz
2013-10-31 15:57 ` [PATCH BlueZ 2/2] android/hid: Initial implementation for uHID Luiz Augusto von Dentz
0 siblings, 1 reply; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2013-10-31 15:57 UTC (permalink / raw)
To: linux-bluetooth
From: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
This patch fetch HID record and store the information needed by uHID.
---
android/Android.mk | 4 +-
android/Makefile.am | 4 +-
android/hid.c | 134 +++++++++++++++++++++++++++++++++++++++++++++-------
3 files changed, 122 insertions(+), 20 deletions(-)
diff --git a/android/Android.mk b/android/Android.mk
index 72ea8b8..d2afa09 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -35,7 +35,9 @@ LOCAL_SRC_FILES := \
../lib/sdp.c \
../lib/bluetooth.c \
../lib/hci.c \
- ../btio/btio.c
+ ../btio/btio.c \
+ ../src/sdp-client.c \
+ ../src/glib-helper.c
LOCAL_C_INCLUDES := \
$(call include-path-for, glib) \
diff --git a/android/Makefile.am b/android/Makefile.am
index c5b70ab..ac92510 100644
--- a/android/Makefile.am
+++ b/android/Makefile.am
@@ -13,7 +13,9 @@ android_bluetoothd_SOURCES = android/main.c \
android/hid.h android/hid.c \
android/ipc.h android/ipc.c \
android/socket.h android/socket.c \
- btio/btio.h btio/btio.c
+ btio/btio.h btio/btio.c \
+ src/sdp-client.h src/sdp-client.c \
+ src/glib-helper.h src/glib-helper.c
android_bluetoothd_LDADD = lib/libbluetooth-internal.la @GLIB_LIBS@
diff --git a/android/hid.c b/android/hid.c
index 5caa25b..6b3b1f7 100644
--- a/android/hid.c
+++ b/android/hid.c
@@ -31,7 +31,12 @@
#include "btio/btio.h"
#include "lib/bluetooth.h"
+#include "lib/sdp.h"
+#include "lib/sdp_lib.h"
+#include "lib/uuid.h"
#include "src/shared/mgmt.h"
+#include "src/sdp-client.h"
+#include "src/glib-helper.h"
#include "log.h"
#include "hal-msg.h"
@@ -52,6 +57,12 @@ static GSList *devices = NULL;
struct hid_device {
bdaddr_t dst;
uint8_t state;
+ uint16_t vendor;
+ uint16_t product;
+ uint16_t version;
+ uint8_t country;
+ int rd_size;
+ void *rd_data;
GIOChannel *ctrl_io;
GIOChannel *intr_io;
guint ctrl_watch;
@@ -80,6 +91,8 @@ static void hid_device_free(struct hid_device *hdev)
if (hdev->ctrl_io)
g_io_channel_unref(hdev->ctrl_io);
+ g_free(hdev->rd_data);
+
devices = g_slist_remove(devices, hdev);
g_free(hdev);
}
@@ -98,10 +111,6 @@ static gboolean intr_io_watch_cb(GIOChannel *chan, gpointer data)
DBG("bytes read %d", bread);
- /* TODO: At this moment only baseband is connected, i.e. mouse
- * movements keyboard events doesn't effect on UI. Have to send
- * this data to uhid fd for profile connection. */
-
return TRUE;
}
@@ -186,8 +195,6 @@ static void interrupt_connect_cb(GIOChannel *chan, GError *conn_err,
if (conn_err)
goto failed;
- /*TODO: Get device details through SDP and create UHID fd and start
- * listening on uhid events */
hdev->intr_watch = g_io_add_watch(hdev->intr_io,
G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
intr_watch_cb, hdev);
@@ -250,14 +257,100 @@ failed:
hid_device_free(hdev);
}
+static void hid_sdp_search_cb(sdp_list_t *recs, int err, gpointer data)
+{
+ struct hid_device *hdev = data;
+ sdp_list_t *list;
+ GError *gerr = NULL;
+ const bdaddr_t *src = bt_adapter_get_address();
+
+ DBG("");
+
+ if (err < 0) {
+ error("Unable to get SDP record: %s", strerror(-err));
+ goto fail;
+ }
+
+ if (!recs || !recs->data) {
+ error("No SDP records found");
+ goto fail;
+ }
+
+ for (list = recs; list != NULL; list = list->next) {
+ sdp_record_t *rec = list->data;
+ sdp_data_t *data;
+
+ data = sdp_data_get(rec, SDP_ATTR_VENDOR_ID);
+ if (data)
+ hdev->vendor = data->val.uint16;
+
+ data = sdp_data_get(rec, SDP_ATTR_PRODUCT_ID);
+ if (data)
+ hdev->product = data->val.uint16;
+
+ data = sdp_data_get(rec, SDP_ATTR_VERSION);
+ if (data)
+ hdev->version = data->val.uint16;
+
+ data = sdp_data_get(rec, SDP_ATTR_HID_COUNTRY_CODE);
+ if (data)
+ hdev->country = data->val.uint8;
+
+ data = sdp_data_get(rec, SDP_ATTR_HID_DESCRIPTOR_LIST);
+ if (data) {
+ if (!SDP_IS_SEQ(data->dtd))
+ goto fail;
+
+ /* First HIDDescriptor */
+ data = data->val.dataseq;
+ if (!SDP_IS_SEQ(data->dtd))
+ goto fail;
+
+ /* ClassDescriptorType */
+ data = data->val.dataseq;
+ if (data->dtd != SDP_UINT8)
+ goto fail;
+
+ /* ClassDescriptorData */
+ data = data->next;
+ if (!data || !SDP_IS_TEXT_STR(data->dtd))
+ goto fail;
+
+ hdev->rd_size = data->unitSize;
+ hdev->rd_data = g_memdup(data->val.str, data->unitSize);
+ }
+ }
+
+ if (hdev->ctrl_io)
+ return;
+
+ hdev->ctrl_io = bt_io_connect(control_connect_cb, hdev, NULL, &gerr,
+ BT_IO_OPT_SOURCE_BDADDR, src,
+ BT_IO_OPT_DEST_BDADDR, &hdev->dst,
+ BT_IO_OPT_PSM, L2CAP_PSM_HIDP_CTRL,
+ BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
+ BT_IO_OPT_INVALID);
+ if (gerr) {
+ error("%s", gerr->message);
+ g_error_free(gerr);
+ goto fail;
+ }
+
+ return;
+
+fail:
+ bt_hid_set_state(hdev, HAL_HID_STATE_DISCONNECTED);
+ hid_device_free(hdev);
+}
+
static uint8_t bt_hid_connect(struct hal_cmd_hid_connect *cmd, uint16_t len)
{
struct hid_device *hdev;
char addr[18];
bdaddr_t dst;
GSList *l;
- GError *err = NULL;
const bdaddr_t *src = bt_adapter_get_address();
+ uuid_t uuid;
DBG("");
@@ -271,20 +364,15 @@ static uint8_t bt_hid_connect(struct hal_cmd_hid_connect *cmd, uint16_t len)
return HAL_STATUS_FAILED;
hdev = g_new0(struct hid_device, 1);
- android2bdaddr(&cmd->bdaddr, &hdev->dst);
- ba2str(&hdev->dst, addr);
+ bacpy(&hdev->dst, &dst);
+ ba2str(&hdev->dst, addr);
DBG("connecting to %s", addr);
- hdev->ctrl_io = bt_io_connect(control_connect_cb, hdev, NULL, &err,
- BT_IO_OPT_SOURCE_BDADDR, src,
- BT_IO_OPT_DEST_BDADDR, &hdev->dst,
- BT_IO_OPT_PSM, L2CAP_PSM_HIDP_CTRL,
- BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
- BT_IO_OPT_INVALID);
- if (err) {
- error("%s", err->message);
- g_error_free(err);
+ bt_string2uuid(&uuid, HID_UUID);
+ if (bt_search_service(src, &hdev->dst, &uuid, hid_sdp_search_cb, hdev,
+ NULL) < 0) {
+ error("Failed to search sdp details");
hid_device_free(hdev);
return HAL_STATUS_FAILED;
}
@@ -354,6 +442,8 @@ static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
uint16_t psm;
GError *gerr = NULL;
GSList *l;
+ const bdaddr_t *src = bt_adapter_get_address();
+ uuid_t uuid;
if (err) {
error("%s", err->message);
@@ -383,6 +473,14 @@ static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
bacpy(&hdev->dst, &dst);
hdev->ctrl_io = g_io_channel_ref(chan);
+ bt_string2uuid(&uuid, HID_UUID);
+ if (bt_search_service(src, &hdev->dst, &uuid,
+ hid_sdp_search_cb, hdev, NULL) < 0) {
+ error("failed to search sdp details");
+ hid_device_free(hdev);
+ return;
+ }
+
devices = g_slist_append(devices, hdev);
hdev->ctrl_watch = g_io_add_watch(hdev->ctrl_io,
--
1.8.3.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH BlueZ 2/2] android/hid: Initial implementation for uHID
2013-10-31 15:57 [PATCH BlueZ 1/2] android/hid: Fetch record before attempting to connect Luiz Augusto von Dentz
@ 2013-10-31 15:57 ` Luiz Augusto von Dentz
2013-11-01 7:44 ` Johan Hedberg
0 siblings, 1 reply; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2013-10-31 15:57 UTC (permalink / raw)
To: linux-bluetooth
From: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
This patch reads data on interrupt channel and feeds into uHID.
Kernel will take care rest of the things and connects bt mouse
and keyboard:
[168988.557647] input: bluez-input-device as /devices/virtual/misc/uhid/input54
---
android/hid.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 125 insertions(+), 4 deletions(-)
diff --git a/android/hid.c b/android/hid.c
index 6b3b1f7..68dcec7 100644
--- a/android/hid.c
+++ b/android/hid.c
@@ -37,6 +37,7 @@
#include "src/shared/mgmt.h"
#include "src/sdp-client.h"
#include "src/glib-helper.h"
+#include "profiles/input/uhid_copy.h"
#include "log.h"
#include "hal-msg.h"
@@ -47,7 +48,7 @@
#define L2CAP_PSM_HIDP_CTRL 0x11
#define L2CAP_PSM_HIDP_INTR 0x13
-#define MAX_READ_BUFFER 4096
+#define UHID_DEVICE_FILE "/dev/uhid"
static GIOChannel *notification_io = NULL;
static GIOChannel *ctrl_io = NULL;
@@ -67,6 +68,8 @@ struct hid_device {
GIOChannel *intr_io;
guint ctrl_watch;
guint intr_watch;
+ int uhid_fd;
+ guint uhid_watch_id;
};
static int device_cmp(gconstpointer s, gconstpointer user_data)
@@ -77,6 +80,21 @@ static int device_cmp(gconstpointer s, gconstpointer user_data)
return bacmp(&hdev->dst, dst);
}
+static void uhid_destroy(int fd)
+{
+ struct uhid_event ev;
+
+ /* destroy uHID device */
+ memset(&ev, 0, sizeof(ev));
+ ev.type = UHID_DESTROY;
+
+ if (write(fd, &ev, sizeof(ev)) < 0)
+ error("Failed to destroy uHID device: %s (%d)",
+ strerror(errno), errno);
+
+ close(fd);
+}
+
static void hid_device_free(struct hid_device *hdev)
{
if (hdev->ctrl_watch > 0)
@@ -91,17 +109,63 @@ static void hid_device_free(struct hid_device *hdev)
if (hdev->ctrl_io)
g_io_channel_unref(hdev->ctrl_io);
+ if (hdev->uhid_watch_id) {
+ g_source_remove(hdev->uhid_watch_id);
+ hdev->uhid_watch_id = 0;
+ }
+
+ if (hdev->uhid_fd > 0)
+ uhid_destroy(hdev->uhid_fd);
+
g_free(hdev->rd_data);
devices = g_slist_remove(devices, hdev);
g_free(hdev);
}
+static gboolean uhid_event_cb(GIOChannel *io, GIOCondition cond,
+ gpointer user_data)
+{
+ struct hid_device *hdev = user_data;
+ struct uhid_event ev;
+ ssize_t bread;
+ int fd;
+
+ DBG("");
+
+ if (cond & (G_IO_ERR | G_IO_NVAL))
+ goto failed;
+
+ fd = g_io_channel_unix_get_fd(io);
+ memset(&ev, 0, sizeof(ev));
+
+ bread = read(fd, &ev, sizeof(ev));
+ if (bread < 0) {
+ DBG("read: %s (%d)", strerror(errno), errno);
+ goto failed;
+ }
+
+ DBG("uHID event type %d received", ev.type);
+ /* TODO Handle events */
+
+ return TRUE;
+
+failed:
+ hdev->uhid_watch_id = 0;
+ return FALSE;
+}
+
static gboolean intr_io_watch_cb(GIOChannel *chan, gpointer data)
{
- char buf[MAX_READ_BUFFER];
+ struct hid_device *hdev = data;
+ uint8_t buf[UHID_DATA_MAX];
+ struct uhid_event ev;
int fd, bread;
+ /* Wait uHID if not ready */
+ if (hdev->uhid_fd < 0)
+ return TRUE;
+
fd = g_io_channel_unix_get_fd(chan);
bread = read(fd, buf, sizeof(buf));
if (bread < 0) {
@@ -109,7 +173,18 @@ static gboolean intr_io_watch_cb(GIOChannel *chan, gpointer data)
return TRUE;
}
- DBG("bytes read %d", bread);
+ /* Discard non-data packets */
+ if (bread == 0 || buf[0] != 0xA1)
+ return TRUE;
+
+ /* send data to uHID device skipping HIDP header byte */
+ memset(&ev, 0, sizeof(ev));
+ ev.type = UHID_INPUT;
+ ev.u.input.size = bread - 1;
+ memcpy(ev.u.input.data, &buf[1], ev.u.input.size);
+
+ if (write(hdev->uhid_fd, &ev, sizeof(ev)) < 0)
+ DBG("write: %s (%d)", strerror(errno), errno);
return TRUE;
}
@@ -185,6 +260,44 @@ static gboolean ctrl_watch_cb(GIOChannel *chan, GIOCondition cond,
return FALSE;
}
+static int uhid_create(struct hid_device *hdev)
+{
+ GIOCondition cond = G_IO_IN | G_IO_ERR | G_IO_NVAL;
+ GIOChannel *io;
+ struct uhid_event ev;
+
+ hdev->uhid_fd = open(UHID_DEVICE_FILE, O_RDWR | O_CLOEXEC);
+ if (hdev->uhid_fd < 0) {
+ error("Failed to open uHID device: %s", strerror(errno));
+ return -errno;
+ }
+
+ memset(&ev, 0, sizeof(ev));
+ ev.type = UHID_CREATE;
+ strcpy((char *) ev.u.create.name, "bluez-input-device");
+ ev.u.create.bus = BUS_BLUETOOTH;
+ ev.u.create.vendor = hdev->vendor;
+ ev.u.create.product = hdev->product;
+ ev.u.create.version = hdev->vendor;
+ ev.u.create.country = hdev->country;
+ ev.u.create.rd_size = hdev->rd_size;
+ ev.u.create.rd_data = hdev->rd_data;
+
+ if (write(hdev->uhid_fd, &ev, sizeof(ev)) < 0) {
+ error("Failed to create uHID device: %s", strerror(errno));
+ close(hdev->uhid_fd);
+ hdev->uhid_fd = -1;
+ return -errno;
+ }
+
+ io = g_io_channel_unix_new(hdev->uhid_fd);
+ g_io_channel_set_encoding(io, NULL, NULL);
+ hdev->uhid_watch_id = g_io_add_watch(io, cond, uhid_event_cb, hdev);
+ g_io_channel_unref(io);
+
+ return 0;
+}
+
static void interrupt_connect_cb(GIOChannel *chan, GError *conn_err,
gpointer user_data)
{
@@ -195,6 +308,9 @@ static void interrupt_connect_cb(GIOChannel *chan, GError *conn_err,
if (conn_err)
goto failed;
+ if (uhid_create(hdev) < 0)
+ goto failed;
+
hdev->intr_watch = g_io_add_watch(hdev->intr_io,
G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
intr_watch_cb, hdev);
@@ -321,8 +437,11 @@ static void hid_sdp_search_cb(sdp_list_t *recs, int err, gpointer data)
}
}
- if (hdev->ctrl_io)
+ if (hdev->ctrl_io) {
+ if (uhid_create(hdev) < 0)
+ goto fail;
return;
+ }
hdev->ctrl_io = bt_io_connect(control_connect_cb, hdev, NULL, &gerr,
BT_IO_OPT_SOURCE_BDADDR, src,
@@ -365,6 +484,7 @@ static uint8_t bt_hid_connect(struct hal_cmd_hid_connect *cmd, uint16_t len)
hdev = g_new0(struct hid_device, 1);
bacpy(&hdev->dst, &dst);
+ hdev->uhid_fd = -1;
ba2str(&hdev->dst, addr);
DBG("connecting to %s", addr);
@@ -472,6 +592,7 @@ static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
hdev = g_new0(struct hid_device, 1);
bacpy(&hdev->dst, &dst);
hdev->ctrl_io = g_io_channel_ref(chan);
+ hdev->uhid_fd = -1;
bt_string2uuid(&uuid, HID_UUID);
if (bt_search_service(src, &hdev->dst, &uuid,
--
1.8.3.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH BlueZ 2/2] android/hid: Initial implementation for uHID
2013-10-31 15:57 ` [PATCH BlueZ 2/2] android/hid: Initial implementation for uHID Luiz Augusto von Dentz
@ 2013-11-01 7:44 ` Johan Hedberg
0 siblings, 0 replies; 3+ messages in thread
From: Johan Hedberg @ 2013-11-01 7:44 UTC (permalink / raw)
To: Luiz Augusto von Dentz; +Cc: linux-bluetooth
Hi Luiz/Ravi,
On Thu, Oct 31, 2013, Luiz Augusto von Dentz wrote:
> From: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
>
> This patch reads data on interrupt channel and feeds into uHID.
> Kernel will take care rest of the things and connects bt mouse
> and keyboard:
>
> [168988.557647] input: bluez-input-device as /devices/virtual/misc/uhid/input54
> ---
> android/hid.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
> 1 file changed, 125 insertions(+), 4 deletions(-)
After applying these two patches the tree stops compiling for me (even
after rerunning ./bootstrap-configure):
src/glib-helper.o: In function `bt_modalias':
/home/jh/src/bluez/src/glib-helper.c:42: multiple definition of `bt_modalias'
src/glib-helper.o:/home/jh/src/bluez/src/glib-helper.c:42: first defined here
src/glib-helper.o: In function `bt_uuid2string':
/home/jh/src/bluez/src/glib-helper.c:55: multiple definition of `bt_uuid2string'
src/glib-helper.o:/home/jh/src/bluez/src/glib-helper.c:55: first defined here
src/glib-helper.o: In function `bt_name2string':
/home/jh/src/bluez/src/glib-helper.c:172: multiple definition of `bt_name2string'
src/glib-helper.o:/home/jh/src/bluez/src/glib-helper.c:172: first defined here
src/glib-helper.o: In function `bt_string2uuid':
/home/jh/src/bluez/src/glib-helper.c:202: multiple definition of `bt_string2uuid'
src/glib-helper.o:/home/jh/src/bluez/src/glib-helper.c:202: first defined here
collect2: error: ld returned 1 exit status
make[1]: *** [android/bluetoothd] Error 1
Johan
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2013-11-01 7:44 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-10-31 15:57 [PATCH BlueZ 1/2] android/hid: Fetch record before attempting to connect Luiz Augusto von Dentz
2013-10-31 15:57 ` [PATCH BlueZ 2/2] android/hid: Initial implementation for uHID Luiz Augusto von Dentz
2013-11-01 7:44 ` Johan Hedberg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox