usbhost api: replace libdeviced to dbus method 60/88960/2 accepted/tizen/common/20160921.162727 submit/tizen/20160921.084123
authortaeyoung <ty317.kim@samsung.com>
Wed, 21 Sep 2016 07:54:54 +0000 (16:54 +0900)
committertaeyoung <ty317.kim@samsung.com>
Wed, 21 Sep 2016 08:18:17 +0000 (17:18 +0900)
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>
configure.ac
libusb/Makefile.am
libusb/os/linux_usbfs.c
packaging/libusb.spec

index c64afbe..b639514 100644 (file)
@@ -97,15 +97,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],
@@ -191,7 +191,7 @@ AM_CONDITIONAL(OS_WINDOWS, test "x$backend" = xwindows)
 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_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 c9c1d8c..358c7ce 100644 (file)
@@ -75,8 +75,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 e6d1e11..948d065 100644 (file)
@@ -39,8 +39,8 @@
 #include <unistd.h>
 
 /* Tizen specific */
-#ifdef USE_DEVICED
-#include <dd-usbhost.h>
+#ifdef USE_USBHOST_API
+#include <dbus/dbus.h>
 #endif
 
 #include "libusb.h"
@@ -210,7 +210,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;
@@ -230,7 +320,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)
 {
@@ -249,7 +339,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
@@ -271,7 +361,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",
@@ -433,7 +523,7 @@ static int op_init(struct libusb_context *ctx)
        struct stat statbuf;
        int r;
 
-#ifdef USE_DEVICED
+#ifdef USE_USBHOST_API
        /* find_usbfs_path() may fail if there are no devices connected.
         * We assume that /dev/bus/usb is usbfs mount point. */
        usbfs_path = "/dev/bus/usb";
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}