* [PATCH BlueZ 1/8] shared/uhid: Add initial API
@ 2014-05-21 15:01 Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 2/8] unit/test-uhid: Add /uhid/command/create test Luiz Augusto von Dentz
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2014-05-21 15:01 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
Makefile.am | 8 +++
src/shared/uhid.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/shared/uhid.h | 43 ++++++++++++++
unit/test-uhid.c | 37 ++++++++++++
4 files changed, 263 insertions(+)
create mode 100644 src/shared/uhid.c
create mode 100644 src/shared/uhid.h
create mode 100644 unit/test-uhid.c
diff --git a/Makefile.am b/Makefile.am
index 3ca98d0..6b989b4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -263,6 +263,14 @@ unit_test_mgmt_SOURCES = unit/test-mgmt.c \
src/shared/mgmt.h src/shared/mgmt.c
unit_test_mgmt_LDADD = @GLIB_LIBS@
+unit_tests += unit/test-uhid
+
+unit_test_uhid_SOURCES = unit/test-uhid.c \
+ src/shared/uhid.h src/shared/uhid.c \
+ src/shared/io.h src/shared/io-glib.c \
+ src/shared/util.h src/shared/util.c
+unit_test_uhid_LDADD = @GLIB_LIBS@
+
unit_tests += unit/test-sdp
unit_test_sdp_SOURCES = unit/test-sdp.c \
diff --git a/src/shared/uhid.c b/src/shared/uhid.c
new file mode 100644
index 0000000..13d9b5e
--- /dev/null
+++ b/src/shared/uhid.c
@@ -0,0 +1,175 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2014 Intel Corporation. All rights reserved.
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; 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 <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#include "src/shared/io.h"
+#include "src/shared/util.h"
+#include "src/shared/uhid.h"
+
+#define UHID_DEVICE_FILE "/dev/uhid"
+
+struct bt_uhid {
+ int ref_count;
+ struct io *io;
+};
+
+static void uhid_free(struct bt_uhid *uhid)
+{
+ if (uhid->io)
+ io_destroy(uhid->io);
+
+ free(uhid);
+}
+
+static bool uhid_read_handler(struct io *io, void *user_data)
+{
+ int fd;
+ ssize_t len;
+ struct uhid_event ev;
+
+ fd = io_get_fd(io);
+ if (fd < 0)
+ return false;
+
+ memset(&ev, 0, sizeof(ev));
+
+ len = read(fd, &ev, sizeof(ev));
+ if (len < 0)
+ return false;
+
+ if ((size_t) len < sizeof(ev.type))
+ return false;
+
+ return true;
+}
+
+struct bt_uhid *bt_uhid_new_default(void)
+{
+ struct bt_uhid *uhid;
+ int fd;
+
+ fd = open(UHID_DEVICE_FILE, O_RDWR | O_CLOEXEC);
+ if (fd < 0)
+ return NULL;
+
+ uhid = bt_uhid_new(fd);
+ if (!uhid) {
+ close(fd);
+ return NULL;
+ }
+
+ io_set_close_on_destroy(uhid->io, true);
+
+ return uhid;
+}
+
+struct bt_uhid *bt_uhid_new(int fd)
+{
+ struct bt_uhid *uhid;
+
+ uhid = new0(struct bt_uhid, 1);
+ if (!uhid)
+ return NULL;
+
+ uhid->io = io_new(fd);
+ if (!uhid->io)
+ goto failed;
+
+ if (!io_set_read_handler(uhid->io, uhid_read_handler, uhid, NULL))
+ goto failed;
+
+ return bt_uhid_ref(uhid);
+
+failed:
+ uhid_free(uhid);
+ return NULL;
+}
+
+struct bt_uhid *bt_uhid_ref(struct bt_uhid *uhid)
+{
+ if (!uhid)
+ return NULL;
+
+ __sync_fetch_and_add(&uhid->ref_count, 1);
+
+ return uhid;
+}
+
+void bt_uhid_unref(struct bt_uhid *uhid)
+{
+ if (!uhid)
+ return;
+
+ if (__sync_sub_and_fetch(&uhid->ref_count, 1))
+ return;
+
+ uhid_free(uhid);
+}
+
+bool bt_uhid_set_close_on_unref(struct bt_uhid *uhid, bool do_close)
+{
+ if (!uhid || !uhid->io)
+ return false;
+
+ io_set_close_on_destroy(uhid->io, do_close);
+
+ return true;
+}
+
+unsigned int bt_uhid_register(struct bt_uhid *uhid, uint32_t type,
+ bt_uhid_notify_func_t func, void *user_data)
+{
+ return 0;
+}
+
+bool bt_uhid_unregister(struct bt_uhid *uhid, unsigned int id)
+{
+ return false;
+}
+
+int bt_uhid_send(struct bt_uhid *uhid, const struct uhid_event *ev)
+{
+ int fd;
+ ssize_t len;
+
+ if (!uhid->io)
+ return -ENOTCONN;
+
+ fd = io_get_fd(uhid->io);
+
+ len = write(fd, ev, sizeof(*ev));
+ if (len < 0)
+ return -errno;
+
+ /* uHID kernel driver does not handle partial writes */
+ return len != sizeof(*ev) ? -EIO : 0;
+}
diff --git a/src/shared/uhid.h b/src/shared/uhid.h
new file mode 100644
index 0000000..fcf5379
--- /dev/null
+++ b/src/shared/uhid.h
@@ -0,0 +1,43 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2014 Intel Corporation. All rights reserved.
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <linux/uhid.h>
+
+struct bt_uhid;
+
+struct bt_uhid *bt_uhid_new_default(void);
+struct bt_uhid *bt_uhid_new(int fd);
+
+struct bt_uhid *bt_uhid_ref(struct bt_uhid *uhid);
+void bt_uhid_unref(struct bt_uhid *uhid);
+
+bool bt_uhid_set_close_on_unref(struct bt_uhid *uhid, bool do_close);
+
+typedef void (*bt_uhid_callback_t)(struct uhid_event *ev, void *user_data);
+unsigned int bt_uhid_register(struct bt_uhid *uhid, uint32_t event,
+ bt_uhid_callback_t func, void *user_data);
+bool bt_uhid_unregister(struct bt_uhid *uhid, unsigned int id);
+
+int bt_uhid_send(struct bt_uhid *uhid, const struct uhid_event *ev);
diff --git a/unit/test-uhid.c b/unit/test-uhid.c
new file mode 100644
index 0000000..e0fb411
--- /dev/null
+++ b/unit/test-uhid.c
@@ -0,0 +1,37 @@
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2014 Intel Corporation. All rights reserved.
+ *
+ *
+ * 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 <glib.h>
+
+#include "src/shared/uhid.h"
+
+int main(int argc, char *argv[])
+{
+ g_test_init(&argc, &argv, NULL);
+
+ return g_test_run();
+}
--
1.9.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH BlueZ 2/8] unit/test-uhid: Add /uhid/command/create test
2014-05-21 15:01 [PATCH BlueZ 1/8] shared/uhid: Add initial API Luiz Augusto von Dentz
@ 2014-05-21 15:01 ` Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 3/8] unit/test-uhid: Add /uhid/command/destroy test Luiz Augusto von Dentz
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2014-05-21 15:01 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
unit/test-uhid.c | 210 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 210 insertions(+)
diff --git a/unit/test-uhid.c b/unit/test-uhid.c
index e0fb411..e4b53e6 100644
--- a/unit/test-uhid.c
+++ b/unit/test-uhid.c
@@ -25,13 +25,223 @@
#include <config.h>
#endif
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <inttypes.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/socket.h>
+
#include <glib.h>
#include "src/shared/uhid.h"
+#include "src/shared/util.h"
+
+struct test_pdu {
+ bool valid;
+ const uint8_t *data;
+ size_t size;
+};
+
+struct test_data {
+ char *test_name;
+ struct test_pdu *pdu_list;
+};
+
+struct context {
+ GMainLoop *main_loop;
+ struct bt_uhid *uhid;
+ guint source;
+ guint process;
+ int fd;
+ unsigned int pdu_offset;
+ const struct test_data *data;
+};
+
+#define event(args...) \
+ { \
+ .valid = true, \
+ .data = (void *) args, \
+ .size = sizeof(*args), \
+ }
+
+#define define_test(name, function, args...) \
+ do { \
+ const struct test_pdu pdus[] = { \
+ args, { } \
+ }; \
+ static struct test_data data; \
+ data.test_name = g_strdup(name); \
+ data.pdu_list = g_malloc(sizeof(pdus)); \
+ memcpy(data.pdu_list, pdus, sizeof(pdus)); \
+ g_test_add_data_func(name, &data, function); \
+ } while (0)
+
+static void test_debug(const char *str, void *user_data)
+{
+ const char *prefix = user_data;
+
+ g_print("%s%s\n", prefix, str);
+}
+
+static void test_free(gconstpointer user_data)
+{
+ const struct test_data *data = user_data;
+
+ g_free(data->test_name);
+ g_free(data->pdu_list);
+}
+
+static gboolean context_quit(gpointer user_data)
+{
+ struct context *context = user_data;
+
+ if (context->process > 0)
+ g_source_remove(context->process);
+
+ g_main_loop_quit(context->main_loop);
+
+ return FALSE;
+}
+
+static gboolean send_pdu(gpointer user_data)
+{
+ struct context *context = user_data;
+ const struct test_pdu *pdu;
+ ssize_t len;
+
+ pdu = &context->data->pdu_list[context->pdu_offset++];
+
+ len = write(context->fd, pdu->data, pdu->size);
+
+ if (g_test_verbose())
+ util_hexdump('<', pdu->data, len, test_debug, "uHID: ");
+
+ g_assert_cmpint(len, ==, pdu->size);
+
+ context->process = 0;
+ return FALSE;
+}
+
+static void context_process(struct context *context)
+{
+ if (!context->data->pdu_list[context->pdu_offset].valid) {
+ context_quit(context);
+ return;
+ }
+
+ context->process = g_idle_add(send_pdu, context);
+}
+
+static gboolean test_handler(GIOChannel *channel, GIOCondition cond,
+ gpointer user_data)
+{
+ struct context *context = user_data;
+ const struct test_pdu *pdu;
+ unsigned char buf[sizeof(struct uhid_event)];
+ ssize_t len;
+ int fd;
+
+ pdu = &context->data->pdu_list[context->pdu_offset++];
+
+ if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
+ context->source = 0;
+ g_print("%s: cond %x\n", __func__, cond);
+ return FALSE;
+ }
+
+ fd = g_io_channel_unix_get_fd(channel);
+
+ len = read(fd, buf, sizeof(buf));
+
+ g_assert(len > 0);
+
+ if (g_test_verbose())
+ util_hexdump('>', buf, len, test_debug, "uHID: ");
+
+ g_assert_cmpint(len, ==, pdu->size);
+
+ g_assert(memcmp(buf, pdu->data, pdu->size) == 0);
+
+ context_process(context);
+
+ return TRUE;
+}
+
+static struct context *create_context(gconstpointer data)
+{
+ struct context *context = g_new0(struct context, 1);
+ GIOChannel *channel;
+ int err, sv[2];
+
+ context->main_loop = g_main_loop_new(NULL, FALSE);
+ g_assert(context->main_loop);
+
+ err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
+ g_assert(err == 0);
+
+ context->uhid = bt_uhid_new(sv[0]);
+ g_assert(context->uhid != NULL);
+
+ channel = g_io_channel_unix_new(sv[1]);
+
+ g_io_channel_set_close_on_unref(channel, TRUE);
+ g_io_channel_set_encoding(channel, NULL, NULL);
+ g_io_channel_set_buffered(channel, FALSE);
+
+ context->source = g_io_add_watch(channel,
+ G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+ test_handler, context);
+ g_assert(context->source > 0);
+
+ g_io_channel_unref(channel);
+
+ context->fd = sv[1];
+ context->data = data;
+
+ return context;
+}
+
+static void destroy_context(struct context *context)
+{
+ if (context->source > 0)
+ g_source_remove(context->source);
+
+ bt_uhid_unref(context->uhid);
+
+ g_main_loop_unref(context->main_loop);
+
+ test_free(context->data);
+ g_free(context);
+}
+
+static void execute_context(struct context *context)
+{
+ g_main_loop_run(context->main_loop);
+
+ destroy_context(context);
+}
+
+static const struct uhid_event ev_create = {
+ .type = UHID_CREATE,
+};
+
+static void test_client(gconstpointer data)
+{
+ struct context *context = create_context(data);
+
+ if (g_str_equal(context->data->test_name, "/uhid/command/create"))
+ bt_uhid_send(context->uhid, &ev_create);
+
+ execute_context(context);
+}
int main(int argc, char *argv[])
{
g_test_init(&argc, &argv, NULL);
+ define_test("/uhid/command/create", test_client, event(&ev_create));
+
return g_test_run();
}
--
1.9.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH BlueZ 3/8] unit/test-uhid: Add /uhid/command/destroy test
2014-05-21 15:01 [PATCH BlueZ 1/8] shared/uhid: Add initial API Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 2/8] unit/test-uhid: Add /uhid/command/create test Luiz Augusto von Dentz
@ 2014-05-21 15:01 ` Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 4/8] unit/test-uhid: Add /uhid/command/feature_answer test Luiz Augusto von Dentz
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2014-05-21 15:01 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
unit/test-uhid.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/unit/test-uhid.c b/unit/test-uhid.c
index e4b53e6..25216b8 100644
--- a/unit/test-uhid.c
+++ b/unit/test-uhid.c
@@ -227,6 +227,10 @@ static const struct uhid_event ev_create = {
.type = UHID_CREATE,
};
+static const struct uhid_event ev_destroy = {
+ .type = UHID_DESTROY,
+};
+
static void test_client(gconstpointer data)
{
struct context *context = create_context(data);
@@ -234,6 +238,9 @@ static void test_client(gconstpointer data)
if (g_str_equal(context->data->test_name, "/uhid/command/create"))
bt_uhid_send(context->uhid, &ev_create);
+ if (g_str_equal(context->data->test_name, "/uhid/command/destroy"))
+ bt_uhid_send(context->uhid, &ev_destroy);
+
execute_context(context);
}
@@ -242,6 +249,7 @@ int main(int argc, char *argv[])
g_test_init(&argc, &argv, NULL);
define_test("/uhid/command/create", test_client, event(&ev_create));
+ define_test("/uhid/command/destroy", test_client, event(&ev_destroy));
return g_test_run();
}
--
1.9.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH BlueZ 4/8] unit/test-uhid: Add /uhid/command/feature_answer test
2014-05-21 15:01 [PATCH BlueZ 1/8] shared/uhid: Add initial API Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 2/8] unit/test-uhid: Add /uhid/command/create test Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 3/8] unit/test-uhid: Add /uhid/command/destroy test Luiz Augusto von Dentz
@ 2014-05-21 15:01 ` Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 5/8] unit/test-uhid: Add /uhid/command/input test Luiz Augusto von Dentz
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2014-05-21 15:01 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
unit/test-uhid.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/unit/test-uhid.c b/unit/test-uhid.c
index 25216b8..b8bb3a4 100644
--- a/unit/test-uhid.c
+++ b/unit/test-uhid.c
@@ -231,6 +231,10 @@ static const struct uhid_event ev_destroy = {
.type = UHID_DESTROY,
};
+static const struct uhid_event ev_feature_answer = {
+ .type = UHID_FEATURE_ANSWER,
+};
+
static void test_client(gconstpointer data)
{
struct context *context = create_context(data);
@@ -241,6 +245,10 @@ static void test_client(gconstpointer data)
if (g_str_equal(context->data->test_name, "/uhid/command/destroy"))
bt_uhid_send(context->uhid, &ev_destroy);
+ if (g_str_equal(context->data->test_name,
+ "/uhid/command/feature_answer"))
+ bt_uhid_send(context->uhid, &ev_feature_answer);
+
execute_context(context);
}
@@ -250,6 +258,8 @@ int main(int argc, char *argv[])
define_test("/uhid/command/create", test_client, event(&ev_create));
define_test("/uhid/command/destroy", test_client, event(&ev_destroy));
+ define_test("/uhid/command/feature_answer", test_client,
+ event(&ev_feature_answer));
return g_test_run();
}
--
1.9.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH BlueZ 5/8] unit/test-uhid: Add /uhid/command/input test
2014-05-21 15:01 [PATCH BlueZ 1/8] shared/uhid: Add initial API Luiz Augusto von Dentz
` (2 preceding siblings ...)
2014-05-21 15:01 ` [PATCH BlueZ 4/8] unit/test-uhid: Add /uhid/command/feature_answer test Luiz Augusto von Dentz
@ 2014-05-21 15:01 ` Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 6/8] shared/uhid: Add implemetation of bt_uhid_register/bt_uhid_unregister Luiz Augusto von Dentz
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2014-05-21 15:01 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
unit/test-uhid.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/unit/test-uhid.c b/unit/test-uhid.c
index b8bb3a4..cb9b05a 100644
--- a/unit/test-uhid.c
+++ b/unit/test-uhid.c
@@ -235,6 +235,10 @@ static const struct uhid_event ev_feature_answer = {
.type = UHID_FEATURE_ANSWER,
};
+static const struct uhid_event ev_input = {
+ .type = UHID_INPUT,
+};
+
static void test_client(gconstpointer data)
{
struct context *context = create_context(data);
@@ -249,6 +253,9 @@ static void test_client(gconstpointer data)
"/uhid/command/feature_answer"))
bt_uhid_send(context->uhid, &ev_feature_answer);
+ if (g_str_equal(context->data->test_name, "/uhid/command/input"))
+ bt_uhid_send(context->uhid, &ev_input);
+
execute_context(context);
}
@@ -260,6 +267,7 @@ int main(int argc, char *argv[])
define_test("/uhid/command/destroy", test_client, event(&ev_destroy));
define_test("/uhid/command/feature_answer", test_client,
event(&ev_feature_answer));
+ define_test("/uhid/command/input", test_client, event(&ev_input));
return g_test_run();
}
--
1.9.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH BlueZ 6/8] shared/uhid: Add implemetation of bt_uhid_register/bt_uhid_unregister
2014-05-21 15:01 [PATCH BlueZ 1/8] shared/uhid: Add initial API Luiz Augusto von Dentz
` (3 preceding siblings ...)
2014-05-21 15:01 ` [PATCH BlueZ 5/8] unit/test-uhid: Add /uhid/command/input test Luiz Augusto von Dentz
@ 2014-05-21 15:01 ` Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 7/8] unit/test-uhid: Add /uhid/event/output test Luiz Augusto von Dentz
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2014-05-21 15:01 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
Makefile.am | 3 ++-
src/shared/uhid.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
2 files changed, 76 insertions(+), 5 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index 6b989b4..c45cf2a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -268,7 +268,8 @@ unit_tests += unit/test-uhid
unit_test_uhid_SOURCES = unit/test-uhid.c \
src/shared/uhid.h src/shared/uhid.c \
src/shared/io.h src/shared/io-glib.c \
- src/shared/util.h src/shared/util.c
+ src/shared/util.h src/shared/util.c \
+ src/shared/queue.h src/shared/queue.c
unit_test_uhid_LDADD = @GLIB_LIBS@
unit_tests += unit/test-sdp
diff --git a/src/shared/uhid.c b/src/shared/uhid.c
index 13d9b5e..28c1792 100644
--- a/src/shared/uhid.c
+++ b/src/shared/uhid.c
@@ -33,6 +33,7 @@
#include "src/shared/io.h"
#include "src/shared/util.h"
+#include "src/shared/queue.h"
#include "src/shared/uhid.h"
#define UHID_DEVICE_FILE "/dev/uhid"
@@ -40,6 +41,15 @@
struct bt_uhid {
int ref_count;
struct io *io;
+ unsigned int notify_id;
+ struct queue *notify_list;
+};
+
+struct uhid_notify {
+ unsigned int id;
+ uint32_t event;
+ bt_uhid_callback_t func;
+ void *user_data;
};
static void uhid_free(struct bt_uhid *uhid)
@@ -47,11 +57,27 @@ static void uhid_free(struct bt_uhid *uhid)
if (uhid->io)
io_destroy(uhid->io);
+ if (uhid->notify_list)
+ queue_destroy(uhid->notify_list, free);
+
free(uhid);
}
+static void notify_handler(void *data, void *user_data)
+{
+ struct uhid_notify *notify = data;
+ struct uhid_event *ev = user_data;
+
+ if (notify->event != ev->type)
+ return;
+
+ if (notify->func)
+ notify->func(ev, notify->user_data);
+}
+
static bool uhid_read_handler(struct io *io, void *user_data)
{
+ struct bt_uhid *uhid = user_data;
int fd;
ssize_t len;
struct uhid_event ev;
@@ -69,6 +95,8 @@ static bool uhid_read_handler(struct io *io, void *user_data)
if ((size_t) len < sizeof(ev.type))
return false;
+ queue_foreach(uhid->notify_list, notify_handler, &ev);
+
return true;
}
@@ -104,6 +132,10 @@ struct bt_uhid *bt_uhid_new(int fd)
if (!uhid->io)
goto failed;
+ uhid->notify_list = queue_new();
+ if (!uhid->notify_list)
+ goto failed;
+
if (!io_set_read_handler(uhid->io, uhid_read_handler, uhid, NULL))
goto failed;
@@ -145,15 +177,53 @@ bool bt_uhid_set_close_on_unref(struct bt_uhid *uhid, bool do_close)
return true;
}
-unsigned int bt_uhid_register(struct bt_uhid *uhid, uint32_t type,
- bt_uhid_notify_func_t func, void *user_data)
+unsigned int bt_uhid_register(struct bt_uhid *uhid, uint32_t event,
+ bt_uhid_callback_t func, void *user_data)
{
- return 0;
+ struct uhid_notify *notify;
+
+ if (!uhid)
+ return 0;
+
+ notify = new0(struct uhid_notify, 1);
+ if (!notify)
+ return 0;
+
+ notify->id = uhid->notify_id++;
+ notify->event = event;
+ notify->func = func;
+ notify->user_data = user_data;
+
+ if (!queue_push_tail(uhid->notify_list, notify)) {
+ free(notify);
+ return 0;
+ }
+
+ return notify->id;
+}
+
+static bool match_notify_id(const void *a, const void *b)
+{
+ const struct uhid_notify *notify = a;
+ unsigned int id = PTR_TO_UINT(b);
+
+ return notify->id == id;
}
bool bt_uhid_unregister(struct bt_uhid *uhid, unsigned int id)
{
- return false;
+ struct uhid_notify *notify;
+
+ if (!uhid || !id)
+ return false;
+
+ notify = queue_remove_if(uhid->notify_list, match_notify_id,
+ UINT_TO_PTR(id));
+ if (!notify)
+ return false;
+
+ free(notify);
+ return true;
}
int bt_uhid_send(struct bt_uhid *uhid, const struct uhid_event *ev)
--
1.9.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH BlueZ 7/8] unit/test-uhid: Add /uhid/event/output test
2014-05-21 15:01 [PATCH BlueZ 1/8] shared/uhid: Add initial API Luiz Augusto von Dentz
` (4 preceding siblings ...)
2014-05-21 15:01 ` [PATCH BlueZ 6/8] shared/uhid: Add implemetation of bt_uhid_register/bt_uhid_unregister Luiz Augusto von Dentz
@ 2014-05-21 15:01 ` Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 8/8] unit/test-uhid: Add /uhid/event/feature test Luiz Augusto von Dentz
2014-05-23 13:51 ` [PATCH BlueZ 1/8] shared/uhid: Add initial API Luiz Augusto von Dentz
7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2014-05-21 15:01 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
unit/test-uhid.c | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/unit/test-uhid.c b/unit/test-uhid.c
index cb9b05a..a8aba9b 100644
--- a/unit/test-uhid.c
+++ b/unit/test-uhid.c
@@ -239,6 +239,10 @@ static const struct uhid_event ev_input = {
.type = UHID_INPUT,
};
+static const struct uhid_event ev_output = {
+ .type = UHID_OUTPUT,
+};
+
static void test_client(gconstpointer data)
{
struct context *context = create_context(data);
@@ -259,6 +263,24 @@ static void test_client(gconstpointer data)
execute_context(context);
}
+static void handle_output(struct uhid_event *ev, void *user_data)
+{
+ g_assert_cmpint(ev->type, ==, UHID_OUTPUT);
+
+ context_quit(user_data);
+}
+
+static void test_server(gconstpointer data)
+{
+ struct context *context = create_context(data);
+
+ bt_uhid_register(context->uhid, UHID_OUTPUT, handle_output, context);
+
+ g_idle_add(send_pdu, context);
+
+ execute_context(context);
+}
+
int main(int argc, char *argv[])
{
g_test_init(&argc, &argv, NULL);
@@ -269,5 +291,7 @@ int main(int argc, char *argv[])
event(&ev_feature_answer));
define_test("/uhid/command/input", test_client, event(&ev_input));
+ define_test("/uhid/event/output", test_server, event(&ev_output));
+
return g_test_run();
}
--
1.9.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH BlueZ 8/8] unit/test-uhid: Add /uhid/event/feature test
2014-05-21 15:01 [PATCH BlueZ 1/8] shared/uhid: Add initial API Luiz Augusto von Dentz
` (5 preceding siblings ...)
2014-05-21 15:01 ` [PATCH BlueZ 7/8] unit/test-uhid: Add /uhid/event/output test Luiz Augusto von Dentz
@ 2014-05-21 15:01 ` Luiz Augusto von Dentz
2014-05-23 13:51 ` [PATCH BlueZ 1/8] shared/uhid: Add initial API Luiz Augusto von Dentz
7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2014-05-21 15:01 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
unit/test-uhid.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/unit/test-uhid.c b/unit/test-uhid.c
index a8aba9b..85e1356 100644
--- a/unit/test-uhid.c
+++ b/unit/test-uhid.c
@@ -243,6 +243,10 @@ static const struct uhid_event ev_output = {
.type = UHID_OUTPUT,
};
+static const struct uhid_event ev_feature = {
+ .type = UHID_FEATURE,
+};
+
static void test_client(gconstpointer data)
{
struct context *context = create_context(data);
@@ -270,11 +274,19 @@ static void handle_output(struct uhid_event *ev, void *user_data)
context_quit(user_data);
}
+static void handle_feature(struct uhid_event *ev, void *user_data)
+{
+ g_assert_cmpint(ev->type, ==, UHID_FEATURE);
+
+ context_quit(user_data);
+}
+
static void test_server(gconstpointer data)
{
struct context *context = create_context(data);
bt_uhid_register(context->uhid, UHID_OUTPUT, handle_output, context);
+ bt_uhid_register(context->uhid, UHID_FEATURE, handle_feature, context);
g_idle_add(send_pdu, context);
@@ -292,6 +304,7 @@ int main(int argc, char *argv[])
define_test("/uhid/command/input", test_client, event(&ev_input));
define_test("/uhid/event/output", test_server, event(&ev_output));
+ define_test("/uhid/event/feature", test_server, event(&ev_feature));
return g_test_run();
}
--
1.9.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH BlueZ 1/8] shared/uhid: Add initial API
2014-05-21 15:01 [PATCH BlueZ 1/8] shared/uhid: Add initial API Luiz Augusto von Dentz
` (6 preceding siblings ...)
2014-05-21 15:01 ` [PATCH BlueZ 8/8] unit/test-uhid: Add /uhid/event/feature test Luiz Augusto von Dentz
@ 2014-05-23 13:51 ` Luiz Augusto von Dentz
7 siblings, 0 replies; 9+ messages in thread
From: Luiz Augusto von Dentz @ 2014-05-23 13:51 UTC (permalink / raw)
To: linux-bluetooth@vger.kernel.org
Hi,
On Wed, May 21, 2014 at 6:01 PM, Luiz Augusto von Dentz
<luiz.dentz@gmail.com> wrote:
> From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
>
> ---
> Makefile.am | 8 +++
> src/shared/uhid.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
> src/shared/uhid.h | 43 ++++++++++++++
> unit/test-uhid.c | 37 ++++++++++++
> 4 files changed, 263 insertions(+)
> create mode 100644 src/shared/uhid.c
> create mode 100644 src/shared/uhid.h
> create mode 100644 unit/test-uhid.c
>
> diff --git a/Makefile.am b/Makefile.am
> index 3ca98d0..6b989b4 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -263,6 +263,14 @@ unit_test_mgmt_SOURCES = unit/test-mgmt.c \
> src/shared/mgmt.h src/shared/mgmt.c
> unit_test_mgmt_LDADD = @GLIB_LIBS@
>
> +unit_tests += unit/test-uhid
> +
> +unit_test_uhid_SOURCES = unit/test-uhid.c \
> + src/shared/uhid.h src/shared/uhid.c \
> + src/shared/io.h src/shared/io-glib.c \
> + src/shared/util.h src/shared/util.c
> +unit_test_uhid_LDADD = @GLIB_LIBS@
> +
> unit_tests += unit/test-sdp
>
> unit_test_sdp_SOURCES = unit/test-sdp.c \
> diff --git a/src/shared/uhid.c b/src/shared/uhid.c
> new file mode 100644
> index 0000000..13d9b5e
> --- /dev/null
> +++ b/src/shared/uhid.c
> @@ -0,0 +1,175 @@
> +/*
> + *
> + * BlueZ - Bluetooth protocol stack for Linux
> + *
> + * Copyright (C) 2014 Intel Corporation. All rights reserved.
> + *
> + *
> + * This library is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * This library 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
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with this library; 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 <unistd.h>
> +#include <string.h>
> +#include <errno.h>
> +#include <fcntl.h>
> +
> +#include "src/shared/io.h"
> +#include "src/shared/util.h"
> +#include "src/shared/uhid.h"
> +
> +#define UHID_DEVICE_FILE "/dev/uhid"
> +
> +struct bt_uhid {
> + int ref_count;
> + struct io *io;
> +};
> +
> +static void uhid_free(struct bt_uhid *uhid)
> +{
> + if (uhid->io)
> + io_destroy(uhid->io);
> +
> + free(uhid);
> +}
> +
> +static bool uhid_read_handler(struct io *io, void *user_data)
> +{
> + int fd;
> + ssize_t len;
> + struct uhid_event ev;
> +
> + fd = io_get_fd(io);
> + if (fd < 0)
> + return false;
> +
> + memset(&ev, 0, sizeof(ev));
> +
> + len = read(fd, &ev, sizeof(ev));
> + if (len < 0)
> + return false;
> +
> + if ((size_t) len < sizeof(ev.type))
> + return false;
> +
> + return true;
> +}
> +
> +struct bt_uhid *bt_uhid_new_default(void)
> +{
> + struct bt_uhid *uhid;
> + int fd;
> +
> + fd = open(UHID_DEVICE_FILE, O_RDWR | O_CLOEXEC);
> + if (fd < 0)
> + return NULL;
> +
> + uhid = bt_uhid_new(fd);
> + if (!uhid) {
> + close(fd);
> + return NULL;
> + }
> +
> + io_set_close_on_destroy(uhid->io, true);
> +
> + return uhid;
> +}
> +
> +struct bt_uhid *bt_uhid_new(int fd)
> +{
> + struct bt_uhid *uhid;
> +
> + uhid = new0(struct bt_uhid, 1);
> + if (!uhid)
> + return NULL;
> +
> + uhid->io = io_new(fd);
> + if (!uhid->io)
> + goto failed;
> +
> + if (!io_set_read_handler(uhid->io, uhid_read_handler, uhid, NULL))
> + goto failed;
> +
> + return bt_uhid_ref(uhid);
> +
> +failed:
> + uhid_free(uhid);
> + return NULL;
> +}
> +
> +struct bt_uhid *bt_uhid_ref(struct bt_uhid *uhid)
> +{
> + if (!uhid)
> + return NULL;
> +
> + __sync_fetch_and_add(&uhid->ref_count, 1);
> +
> + return uhid;
> +}
> +
> +void bt_uhid_unref(struct bt_uhid *uhid)
> +{
> + if (!uhid)
> + return;
> +
> + if (__sync_sub_and_fetch(&uhid->ref_count, 1))
> + return;
> +
> + uhid_free(uhid);
> +}
> +
> +bool bt_uhid_set_close_on_unref(struct bt_uhid *uhid, bool do_close)
> +{
> + if (!uhid || !uhid->io)
> + return false;
> +
> + io_set_close_on_destroy(uhid->io, do_close);
> +
> + return true;
> +}
> +
> +unsigned int bt_uhid_register(struct bt_uhid *uhid, uint32_t type,
> + bt_uhid_notify_func_t func, void *user_data)
> +{
> + return 0;
> +}
> +
> +bool bt_uhid_unregister(struct bt_uhid *uhid, unsigned int id)
> +{
> + return false;
> +}
> +
> +int bt_uhid_send(struct bt_uhid *uhid, const struct uhid_event *ev)
> +{
> + int fd;
> + ssize_t len;
> +
> + if (!uhid->io)
> + return -ENOTCONN;
> +
> + fd = io_get_fd(uhid->io);
> +
> + len = write(fd, ev, sizeof(*ev));
> + if (len < 0)
> + return -errno;
> +
> + /* uHID kernel driver does not handle partial writes */
> + return len != sizeof(*ev) ? -EIO : 0;
> +}
> diff --git a/src/shared/uhid.h b/src/shared/uhid.h
> new file mode 100644
> index 0000000..fcf5379
> --- /dev/null
> +++ b/src/shared/uhid.h
> @@ -0,0 +1,43 @@
> +/*
> + *
> + * BlueZ - Bluetooth protocol stack for Linux
> + *
> + * Copyright (C) 2014 Intel Corporation. All rights reserved.
> + *
> + *
> + * This library is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * This library 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
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with this library; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
> + *
> + */
> +
> +#include <stdbool.h>
> +#include <stdint.h>
> +#include <linux/uhid.h>
> +
> +struct bt_uhid;
> +
> +struct bt_uhid *bt_uhid_new_default(void);
> +struct bt_uhid *bt_uhid_new(int fd);
> +
> +struct bt_uhid *bt_uhid_ref(struct bt_uhid *uhid);
> +void bt_uhid_unref(struct bt_uhid *uhid);
> +
> +bool bt_uhid_set_close_on_unref(struct bt_uhid *uhid, bool do_close);
> +
> +typedef void (*bt_uhid_callback_t)(struct uhid_event *ev, void *user_data);
> +unsigned int bt_uhid_register(struct bt_uhid *uhid, uint32_t event,
> + bt_uhid_callback_t func, void *user_data);
> +bool bt_uhid_unregister(struct bt_uhid *uhid, unsigned int id);
> +
> +int bt_uhid_send(struct bt_uhid *uhid, const struct uhid_event *ev);
> diff --git a/unit/test-uhid.c b/unit/test-uhid.c
> new file mode 100644
> index 0000000..e0fb411
> --- /dev/null
> +++ b/unit/test-uhid.c
> @@ -0,0 +1,37 @@
> +/*
> + *
> + * BlueZ - Bluetooth protocol stack for Linux
> + *
> + * Copyright (C) 2014 Intel Corporation. All rights reserved.
> + *
> + *
> + * 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 <glib.h>
> +
> +#include "src/shared/uhid.h"
> +
> +int main(int argc, char *argv[])
> +{
> + g_test_init(&argc, &argv, NULL);
> +
> + return g_test_run();
> +}
> --
> 1.9.0
Pushed.
--
Luiz Augusto von Dentz
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2014-05-23 13:51 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-21 15:01 [PATCH BlueZ 1/8] shared/uhid: Add initial API Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 2/8] unit/test-uhid: Add /uhid/command/create test Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 3/8] unit/test-uhid: Add /uhid/command/destroy test Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 4/8] unit/test-uhid: Add /uhid/command/feature_answer test Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 5/8] unit/test-uhid: Add /uhid/command/input test Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 6/8] shared/uhid: Add implemetation of bt_uhid_register/bt_uhid_unregister Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 7/8] unit/test-uhid: Add /uhid/event/output test Luiz Augusto von Dentz
2014-05-21 15:01 ` [PATCH BlueZ 8/8] unit/test-uhid: Add /uhid/event/feature test Luiz Augusto von Dentz
2014-05-23 13:51 ` [PATCH BlueZ 1/8] shared/uhid: Add initial API Luiz Augusto von Dentz
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).