From fe7ec23a11a58dcaf5c1b03b837b662837020876 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pawe=C5=82=20Szewczyk?= Date: Thu, 28 Jan 2016 17:22:01 +0100 Subject: [PATCH] usbhost: Add API for opening device MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: If2cf9e117edb6ff35c5a8541c656999e1de03187 Signed-off-by: Paweł Szewczyk --- src/deviced/dd-usbhost.h | 17 +++++++++++++ src/libdeviced/usbhost.c | 36 +++++++++++++++++++++++++++ src/usbhost/usb-host.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) diff --git a/src/deviced/dd-usbhost.h b/src/deviced/dd-usbhost.h index 424469b..eff38b0 100644 --- a/src/deviced/dd-usbhost.h +++ b/src/deviced/dd-usbhost.h @@ -248,6 +248,23 @@ int unmount_usb_storage(char *path); */ int format_usb_storage(char *path); +/** + * @par Description: + * This API is used to open usb device \n + * @param[in] path path to device + * @return 0 on success, negative error code if failed + * @par Example + * @code + * ... + * + * r = open_usb_device(path, &fd); + * if (r < 0) + * printf("failed to open device"); + * + * ... + * @endcode + */ +int open_usb_device(char *path, int *fd); /** * @} // end of CAPI_SYSTEM_DEVICED_USBHOST_MODULE diff --git a/src/libdeviced/usbhost.c b/src/libdeviced/usbhost.c index ad5104a..747be17 100644 --- a/src/libdeviced/usbhost.c +++ b/src/libdeviced/usbhost.c @@ -27,6 +27,7 @@ #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) +#define METHOD_OPEN_DEVICE "OpenDevice" #define METHOD_REQUEST_STORAGE_INFO_ALL "StorageInfoAll" #define METHOD_REQUEST_STORAGE_MOUNT "StorageMount" #define METHOD_REQUEST_STORAGE_UNMOUNT "StorageUnmount" @@ -252,3 +253,38 @@ API int format_usb_storage(char *path) return dbus_method_sync(DEVICED_BUS_NAME, DEVICED_PATH_USBHOST, DEVICED_INTERFACE_USBHOST, METHOD_REQUEST_STORAGE_FORMAT, "s", param); } + +API int open_usb_device(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, + "s", &path); + + if (!reply) { + _E("Unable to send USB device request"); + return -1; + } + + result = dbus_message_get_args(reply, &err, DBUS_TYPE_INT32, &ret, DBUS_TYPE_UNIX_FD, &rfd, DBUS_TYPE_INVALID); + if (!result) { + _E("Failed to get arguments: %s", err.message); + return -1; + } + + if (ret >= 0) + *fd = rfd; + + return ret; +} diff --git a/src/usbhost/usb-host.c b/src/usbhost/usb-host.c index 9fead6c..248f249 100644 --- a/src/usbhost/usb-host.c +++ b/src/usbhost/usb-host.c @@ -448,10 +448,75 @@ static struct uevent_handler uh = { .uevent_func = uevent_usbhost_handler, }; +enum policy_value { + POLICY_ALLOW, + POLICY_DENY, +}; + +static int get_policy_value(const char *path) +{ + /* TODO */ + + return POLICY_ALLOW; +} + +static DBusMessage *open_device(E_DBus_Object *obj, DBusMessage *msg) +{ + DBusMessageIter iter; + DBusMessage *reply; + DBusError err; + dbus_bool_t dbus_ret; + int ret = 0, fd = -1; + const char *path; + struct device d; + + dbus_error_init(&err); + + dbus_ret = dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &path); + if (!dbus_ret) { + _E("Unable to get arguments from message: %s", err.message); + ret = -EINVAL; + goto out; + } + + reply = dbus_message_new_method_return(msg); + if (!reply) { + _E("Cannot allocate memory for message"); + ret = -ENOMEM; + goto out; + } + + ret = get_policy_value(path); + switch (ret) { + case POLICY_ALLOW: + fd = open(path, O_RDWR); + if (fd < 0) { + ret = -errno; + _E("Unable to open file (%s): %m", path); + } else + ret = 0; + break; + case POLICY_DENY: + ret = -EACCES; + break; + default: + /* ret has error code */ + break; + } + +out: + dbus_message_iter_init_append(reply, &iter); + dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret); + dbus_message_iter_append_basic(&iter, DBUS_TYPE_UNIX_FD, &fd); + + return reply; +} + static const struct edbus_method edbus_methods[] = { { "PrintDeviceList", NULL, NULL, print_device_list }, /* for debugging */ { "GetDeviceList", "i", "a(siiiiisss)", get_device_list }, { "GetDeviceListCount", "i", "i", get_device_list_count }, + { "OpenDevice", "s", "ih", open_device }, }; static int booting_done(void *data) -- 2.7.4