usbhost api: replace libdeviced to dbus method
authortaeyoung <ty317.kim@samsung.com>
Wed, 21 Sep 2016 07:54:54 +0000 (16:54 +0900)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Thu, 13 Apr 2017 11:20:32 +0000 (13:20 +0200)
libusb cannot use libdeviced since circular dependency problem exists.

  libusb -> bluez -> pulseaudio (libpulse) -> efl (ecore) -> deviced -> libusb

Thus libdeviced is replaced to the dbus method which is provided by deviced.

Change-Id: Ia1ee8b72c17f30c8b4c49526d0101ed9294f230f
Signed-off-by: taeyoung <ty317.kim@samsung.com>
[Rebase onto new library version]
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
configure.ac
libusb/Makefile.am
libusb/os/linux_usbfs.c
packaging/libusb.spec

index c822e5b..72a9d70 100644 (file)
@@ -107,15 +107,15 @@ case $backend in
 linux)
        AC_DEFINE(OS_LINUX, 1, [Linux backend])
        AC_SUBST(OS_LINUX)
-       AC_ARG_ENABLE([deviced],
-               [AC_HELP_STRING([--enable-deviced], [Use deviced to obtain dev node fds [default=no]])],
-               [], [enable_deviced="no"])
-       if test "x$enable_deviced" = "xyes"; then
-          PKG_CHECK_MODULES([DEVICED], [deviced])
-          AC_DEFINE(USE_DEVICED, 1, [Use deviced to obtain dev node fds])
+       AC_ARG_ENABLE([usbhost_api],
+               [AC_HELP_STRING([--enable-usbhost-api], [Request to deviced to obtain dev node fds [default=no]])],
+               [], [enable_usbhost_api="no"])
+       if test "x$enable_usbhost_api" = "xyes"; then
+          PKG_CHECK_MODULES([USBHOST_API], [dbus-1])
+          AC_DEFINE(USE_USBHOST_API, 1, [Request to deviced to obtain dev node fds])
        fi
 
-       AC_SUBST(USE_DEVICED)
+       AC_SUBST(USE_USBHOST_API)
 
        AC_SEARCH_LIBS(clock_gettime, rt, [], [], -pthread)
        AC_ARG_ENABLE([udev],
@@ -223,7 +223,7 @@ AM_CONDITIONAL(THREADS_POSIX, test "x$threads" = xposix)
 AM_CONDITIONAL(CREATE_IMPORT_LIB, test "x$create_import_lib" = "xyes")
 AM_CONDITIONAL(USE_UDEV, test "x$enable_udev" = xyes)
 AM_CONDITIONAL(USE_USBDK, test "x$enable_usbdk" = xyes)
-AM_CONDITIONAL(USE_DEVICED, test "x$enable_deviced" = xyes)
+AM_CONDITIONAL(USE_USBHOST_API, test "x$enable_usbhost_api" = xyes)
 if test "$threads" = posix; then
        AC_DEFINE(THREADS_POSIX, 1, [Use POSIX Threads])
 fi
index e6fc154..d2bb864 100644 (file)
@@ -104,8 +104,8 @@ hdrdir = $(includedir)/libusb-1.0
 hdr_HEADERS = libusb.h
 
 if OS_LINUX
-if USE_DEVICED
-libusb_1_0_la_CFLAGS += $(DEVICED_CFLAGS)
-libusb_1_0_la_LIBADD = $(DEVICED_LIBS)
+if USE_USBHOST_API
+libusb_1_0_la_CFLAGS += $(USBHOST_API_CFLAGS)
+libusb_1_0_la_LIBADD = $(USBHOST_API_LIBS)
 endif
 endif
index 90b80c4..588435b 100644 (file)
@@ -40,8 +40,8 @@
 #include <time.h>
 
 /* Tizen specific */
-#ifdef USE_DEVICED
-#include <dd-usbhost.h>
+#ifdef USE_USBHOST_API
+#include <dbus/dbus.h>
 #endif
 
 #include "libusbi.h"
@@ -211,7 +211,97 @@ static int _direct_open_device(struct libusb_context *ctx, const char *path,
        return fd;
 }
 
-#ifdef USE_DEVICED
+#ifdef USE_USBHOST_API
+
+#define DEVICED_BUS_NAME           "org.tizen.system.deviced"
+#define DEVICED_OBJECT_PATH        "/Org/Tizen/System/DeviceD"
+#define DEVICED_INTERFACE_NAME     DEVICED_BUS_NAME
+#define DEVICED_PATH_USBHOST       DEVICED_OBJECT_PATH"/Usbhost"
+#define DEVICED_INTERFACE_USBHOST  DEVICED_INTERFACE_NAME".Usbhost"
+#define METHOD_OPEN_DEVICE         "OpenDevice"
+
+DBusMessage *dbus_method_sync_with_reply(const char *dest, const char *path,
+               const char *interface, const char *method, const char *param)
+{
+       DBusConnection *conn;
+       DBusMessage *msg;
+       DBusMessageIter iter;
+       DBusMessage *reply;
+       DBusError err;
+
+       conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
+       if (!conn) {
+               usbi_err(NULL, "dbus_bus_get error");
+               return NULL;
+       }
+
+       msg = dbus_message_new_method_call(dest, path, interface, method);
+       if (!msg) {
+               usbi_err(NULL, "dbus_message_new_method_call(%s:%s-%s)",
+                               path, interface, method);
+               return NULL;
+       }
+
+       dbus_message_iter_init_append(msg, &iter);
+       dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &path);
+
+       dbus_error_init(&err);
+
+       reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);
+       if (!reply) {
+               usbi_err(NULL, "dbus_connection_send error(No reply) %s %s:%s-%s",
+                               dest, path, interface, method);
+       }
+
+       if (dbus_error_is_set(&err)) {
+               usbi_err(NULL, "dbus_connection_send error(%s:%s) %s %s:%s-%s",
+                               err.name, err.message, dest, path, interface, method);
+               dbus_error_free(&err);
+               reply = NULL;
+       }
+
+       dbus_message_unref(msg);
+       return reply;
+}
+
+static int open_usb_device(const char *path, int *fd)
+{
+       DBusMessage *reply;
+       DBusError err;
+       int ret, rfd;
+       dbus_bool_t result;
+
+       if (!fd || !path)
+               return -EINVAL;
+
+       dbus_error_init(&err);
+
+       reply = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
+                       DEVICED_PATH_USBHOST,
+                       DEVICED_INTERFACE_USBHOST,
+                       METHOD_OPEN_DEVICE,
+                       path);
+
+       if (!reply) {
+               usbi_err(NULL, "Unable to open USB device");
+               return -1;
+       }
+
+       result = dbus_message_get_args(reply, &err,
+                       DBUS_TYPE_INT32, &ret,
+                       DBUS_TYPE_UNIX_FD, &rfd,
+                       DBUS_TYPE_INVALID);
+       if (!result) {
+               usbi_err(NULL, "Failed to get arguments: %s", err.message);
+               return -1;
+       }
+
+       if (ret >= 0)
+               *fd = rfd;
+
+       return ret;
+}
+
 static int _ask_for_open(const char *path, mode_t mode, int silent)
 {
        int ret;
@@ -231,7 +321,7 @@ static int _ask_for_open(const char *path, mode_t mode, int silent)
 
        return ret;
 }
-#endif /* USE_DEVICED */
+#endif /* USE_USBHOST_API */
 
 static int _get_usbfs_fd(struct libusb_device *dev, mode_t mode, int silent)
 {
@@ -250,7 +340,7 @@ static int _get_usbfs_fd(struct libusb_device *dev, mode_t mode, int silent)
        if (fd != -1)
                return fd; /* Success */
 
-#ifdef USE_DEVICED
+#ifdef USE_USBHOST_API
        /*
         * If we are here, we were unable to go simple
         * path and open the device directly. In Tizen
@@ -272,7 +362,7 @@ static int _get_usbfs_fd(struct libusb_device *dev, mode_t mode, int silent)
                if (fd != -1)
                        return fd;
        }
-#endif /* USE_DEVICED */
+#endif /* USE_USBHOST_API */
 
        if (!silent) {
                usbi_err(ctx, "libusb couldn't open USB device %s: %s",
index e43ef05..611b690 100644 (file)
@@ -11,7 +11,7 @@ Source1001:   libusb.manifest
 BuildRequires:  pkg-config
 BuildRequires:  systemd-devel
 %if "%{?profile}" == "mobile"
-BuildRequires:  pkgconfig(deviced)
+BuildRequires:  pkgconfig(dbus-1)
 %endif
 
 %description
@@ -34,7 +34,7 @@ cp %{SOURCE1001} .
 %reconfigure\
        --with-pic\
 %if "%{?profile}" == "mobile"
-       --enable-deviced\
+       --enable-usbhost-api \
 %endif
        --disable-static
 make %{?_smp_mflags}