DPM: apply dpm policy for usb 19/157219/4
authorlokilee73 <changjoo.lee@samsung.com>
Wed, 18 Oct 2017 07:11:09 +0000 (16:11 +0900)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Tue, 24 Oct 2017 00:27:07 +0000 (00:27 +0000)
When dpm policy about usb is received, the target has to take actions according to the policy.
1. Allow
   The host can access to the target via usb and the user can see the storage of it
2. Disallow
   The host cannot access to the target via usb, so the user cannot see the contents of storage

Change-Id: I32918269d3ddb41cac948c36d73cb4ad741d32dc
Signed-off-by: lokilee73 <changjoo.lee@samsung.com>
Signed-off-by: Hyotaek Shim <hyotaek.shim@samsung.com>
src/control/control.c [changed mode: 0644->0755]
src/extcon/extcon.c
src/extcon/extcon.h
src/shared/dbus.c [changed mode: 0644->0755]
src/shared/dbus.h
src/usb/usb.c [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index ffc09d2..85c1e4e
@@ -27,6 +27,7 @@
 #include "core/common.h"
 #include "core/devices.h"
 #include "core/edbus-handler.h"
+#include "extcon/extcon.h"
 
 #define CONTROL_HANDLER_NAME           "control"
 #define CONTROL_GETTER_NAME                    "getcontrol"
@@ -36,9 +37,9 @@ static const struct control_device {
        const char *name;
 } devices[] = {
        /* Add id & ops to provide start/stop control */
-       { DEVICE_CONTROL_MMC,               "mmc" },
-       { DEVICE_CONTROL_USBCLIENT,   "usbclient" },
-       { DEVICE_CONTROL_BLOCK,           "block" },
+       { DEVICE_CONTROL_MMC,                   "mmc" },
+       { DEVICE_CONTROL_USBCLIENT,             "USB" },
+       { DEVICE_CONTROL_BLOCK,                 "block" },
 };
 
 static int control_handler(int argc, char **argv)
@@ -71,11 +72,20 @@ static int control_handler(int argc, char **argv)
 
        if (i >= ARRAY_SIZE(devices))
                return -EINVAL;
-       FIND_DEVICE_INT(dev_ops, devices[i].name);
+
+       FIND_DEVICE(dev_ops, devices[i].name);
+       if (!check_default(dev_ops)) {
+               _I("Device %s found", devices[i].name);
        if (enable)
                ret = device_start(dev_ops);
        else
                ret = device_stop(dev_ops);
+       } else {
+               if (enable)
+                       ret = extcon_enable_device(devices[i].name);
+               else
+                       ret = extcon_disable_device(devices[i].name);
+       }
 
        return ret;
 }
index 34a0d81..ac326c1 100755 (executable)
@@ -33,6 +33,8 @@
 #define EXTCON_PATH "/sys/class/extcon"
 #define STATE_NAME  "STATE"
 
+#define METHOD_SYSPOPUP_SHOW           "show"
+
 #define BUF_MAX 256
 
 static dd_list *extcon_list;
@@ -76,13 +78,19 @@ int extcon_get_status(const char *name)
        if (!dev)
                return -ENOENT;
 
+       if (!dev->enabled)
+               return -ENODEV;
+
        return dev->status;
 }
 
 static int extcon_update(const char *name, const char *value)
 {
        struct extcon_ops *dev;
+       char *param[1];
+       char buf[BUF_MAX];
        int status;
+       int ret = 0;
 
        if (!name || !value)
                return -EINVAL;
@@ -100,12 +108,88 @@ static int extcon_update(const char *name, const char *value)
        _I("Changed %s device : %d -> %d", name, dev->status, status);
 
        dev->status = status;
+
+       if (dev->enabled == false && strncmp(name, "USB", strlen("USB")) == 0) {
+               if (status > 0) {
+                       snprintf(buf, BUF_MAX, "usb-client");
+                       param[0] = buf;
+                       ret = dbus_method_sync(DEVICEMANAGER_BUS_NAME, DEVICEMANAGER_PATH_POPUP,
+                                       DEVICEMANAGER_INTERFACE_POPUP, METHOD_SYSPOPUP_SHOW, "s", param);
+                       if (ret < 0)
+                               _E("Failed to launch USB restricted popup(%d)", ret);
+                       return ret;
+               } else
+                       return 0;
+       }
+
        if (dev->update)
                dev->update(status);
 
        return 0;
 }
 
+int extcon_enable_device(const char *name)
+{
+       struct extcon_ops *dev;
+       int ret;
+
+       dev = find_extcon(name);
+       if (!dev)
+               return -ENODEV;
+
+       if (dev->enabled) {
+               _I("extcon(%s) already enabled", name);
+               return 0;
+       }
+
+       dev->init(NULL);
+       dev->enabled = true;
+
+       if (strncmp(name, "USB", strlen("USB")) == 0) {
+               ret = extcon_update("USB", "0");
+               if (ret != 0)
+                       _E("fail to disconnect USB");
+
+               ret = extcon_update("USB", "1");
+               if (ret != 0)
+                       _E("fail to connect USB");
+       }
+       _I("extcon(%s) enabled", name);
+
+       return 0;
+}
+
+int extcon_disable_device(const char *name)
+{
+       struct extcon_ops *dev;
+       char *param[1];
+       char buf[BUF_MAX];
+       int ret;
+
+       dev = find_extcon(name);
+       if (!dev)
+               return -ENODEV;
+
+       snprintf(buf, BUF_MAX, "usb-client");
+       param[0] = buf;
+       ret = dbus_method_sync(DEVICEMANAGER_BUS_NAME, DEVICEMANAGER_PATH_POPUP,
+                       DEVICEMANAGER_INTERFACE_POPUP, METHOD_SYSPOPUP_SHOW, "s", param);
+       if (ret < 0)
+               _E("Failed to launch USB restricted popup(%d)", ret);
+
+       if (!dev->enabled) {
+               _I("extcon(%s) already disabled", name);
+               return 0;
+       }
+
+       dev->exit(NULL);
+       dev->enabled = false;
+
+       _I("extcon(%s) disabled", name);
+
+       return 0;
+}
+
 static int extcon_parsing_value(const char *value)
 {
        char *s, *p;
@@ -398,6 +482,7 @@ static void extcon_init(void *data)
                _I("[extcon] init (%s)", dev->name);
                if (dev->init)
                        dev->init(data);
+               dev->enabled = true;
        }
 
        event_handler_state_changed((void *)&state);
@@ -427,6 +512,7 @@ static void extcon_exit(void *data)
                _I("[extcon] deinit (%s)", dev->name);
                if (dev->exit)
                        dev->exit(data);
+               dev->enabled = false;
        }
 }
 
index 5a53454..fd9316d 100755 (executable)
@@ -38,6 +38,7 @@
 struct extcon_ops {
        const char *name;
        int status;
+       int enabled;
        void (*init)(void *data);
        void (*exit)(void *data);
        int (*update)(int status);
@@ -66,5 +67,6 @@ void add_extcon(struct extcon_ops *dev);
 void remove_extcon(struct extcon_ops *dev);
 
 int extcon_get_status(const char *name);
-
+int extcon_enable_device(const char *name);
+int extcon_disable_device(const char *name);
 #endif /* __EXTCON_H__ */
old mode 100644 (file)
new mode 100755 (executable)
index 3a07418..321b8f5
@@ -153,6 +153,7 @@ int dbus_method_sync(const char *dest, const char *path,
        DBusMessage *reply;
        DBusError err;
        int ret, result;
+       dbus_bool_t result_bool;
 
        conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
        if (!conn) {
@@ -183,19 +184,34 @@ int dbus_method_sync(const char *dest, const char *path,
        if (!reply) {
                _E("dbus_connection_send error(%s:%s) %s %s:%s-%s",
                        err.name, err.message, dest, path, interface, method);
-               dbus_error_free(&err);
+
+               if (dbus_error_is_set(&err))
+                       dbus_error_free(&err);
+
                return -ECOMM;
        }
 
        ret = dbus_message_get_args(reply, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
-       dbus_message_unref(reply);
        if (!ret) {
-               _E("no message : [%s:%s] %s %s:%s-%s",
-                       err.name, err.message, dest, path, interface, method);
-               dbus_error_free(&err);
-               return -ENOMSG;
+               if (dbus_error_is_set(&err))
+                       dbus_error_free(&err);
+
+               dbus_error_init(&err);
+               ret = dbus_message_get_args(reply, &err, DBUS_TYPE_BOOLEAN, &result_bool, DBUS_TYPE_INVALID);
+               if (!ret) {
+                       _E("no message : [%s:%s] %s %s:%s-%s",
+                               err.name, err.message, dest, path, interface, method);
+
+                       if (dbus_error_is_set(&err))
+                               dbus_error_free(&err);
+
+                       result = -ENOMSG;
+               } else {
+                       result = (int)result_bool;
+               }
        }
 
+       dbus_message_unref(reply);
        return result;
 }
 
index 430f4d4..54ea682 100755 (executable)
 #define CRASHD_PATH_CRASH                   CRASHD_OBJECT_PATH"/Crash"
 #define CRASHD_INTERFACE_CRASH              CRASHD_INTERFACE_NAME".Crash"
 
+/*
+ * Device Manager Policy Popup
+ */
+#define DEVICEMANAGER_BUS_NAME                     "org.tizen.DevicePolicyManager"
+#define DEVICEMANAGER_OBJECT_PATH                  "/org/tizen/DevicePolicyManager"
+#define DEVICEMANAGER_INTERFACE_NAME               DEVICEMANAGER_BUS_NAME
+
+#define DEVICEMANAGER_PATH_POPUP                   DEVICEMANAGER_OBJECT_PATH"/Syspopup"
+#define DEVICEMANAGER_INTERFACE_POPUP              DEVICEMANAGER_INTERFACE_NAME".Syspopup"
 
 
 /***************************************************************/
old mode 100644 (file)
new mode 100755 (executable)
index 2cdd5ee..be8a02a
@@ -117,14 +117,18 @@ no_gadget_hal:
 
 static void usb_release()
 {
-       if (usb_client)
+       if (usb_client) {
                if (usb_client->common.info->close)
                        usb_client->common.info->close(&usb_client->common);
+               usb_client = NULL;
+       }
 
-       if (gadget_translator)
+       if (gadget_translator) {
                if (gadget_translator->common.info->close)
                        gadget_translator->common.info->close(
                                &gadget_translator->common);
+               gadget_translator = NULL;
+       }
 }
 
 static int usb_config_init(void)