+#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;
+}
+