Add DBus Signal to Deviced during booting and setting 19/156819/3
authorLee Sung Jun <sjun221.lee@samsung.com>
Fri, 20 Oct 2017 05:58:44 +0000 (14:58 +0900)
committerLee Sung Jun <sjun221.lee@samsung.com>
Mon, 23 Oct 2017 07:40:09 +0000 (16:40 +0900)
Change-Id: Icab9f7b5929c2c807a9d311118318a828a7599e3
Signed-off-by: Lee Sung Jun <sjun221.lee@samsung.com>
plugin/usb.cpp

index d2a4ab35d41af3fcbf2c08c65a0b57bd0b2b65f2..30e9fbc783b3226381a13967ace0d53436a8aab3 100644 (file)
 
 #include <tzplatform_config.h>
 
-#define DEVICED_INTERFACE_USB "org.tizen.system.deviced.Usb"
-#define DEVICED_PATH_USB "/Org/Tizen/System/DeviceD/Usb"
-#define SIGNAL_STATE_CHANGED "StateChanged"
+#define DEVICED_SYSNOTI_INTERFACE              \
+       "org.tizen.system.deviced",                             \
+       "/Org/Tizen/System/DeviceD/SysNoti",    \
+       "org.tizen.system.deviced.SysNoti",                     \
+       "control"
 
 class DebuggingMode : public GlobalPolicy<DataSetInt> {
 public:
@@ -68,20 +70,54 @@ public:
        Client() : GlobalPolicy("usb-client")
        {
                PolicyEventNotifier::create("usb-client");
+               sendDbusSignal();
        }
 
        bool apply(const DataType& value)
        {
+               int ret;
                int enable = value;
-               PolicyEventNotifier::emit("usb-client", enable ? "allowed" : "disallowed");
-               return true;
+
+               try {
+                       std::string pid(std::to_string(::getpid()));
+                       std::string state(std::to_string(enable));
+                       dbus::Connection &systemDBus = dbus::Connection::getSystem();
+                       systemDBus.methodcall(DEVICED_SYSNOTI_INTERFACE,
+                                                                               -1, "(i)", "(sisss)",
+                                                                               "control", 3, pid.c_str(), "1", state.c_str()).get("(i)", &ret);
+               } catch(runtime::Exception& e) {
+                       ERROR("Failed to enforce usb client");
+                       return false;
+               }
+
+               if (ret == 0) {
+                       PolicyEventNotifier::emit("usb-client", enable ? "allowed" : "disallowed");
+                       return true;
+               }
+
+               return false;
+       }
+
+       void sendDbusSignal(void)
+       {
+               int ret;
+               int enable = get().value;
+
+               try {
+                       std::string pid(std::to_string(::getpid()));
+                       std::string state(std::to_string(enable));
+                       dbus::Connection &systemDBus = dbus::Connection::getSystem();
+                       systemDBus.methodcall(DEVICED_SYSNOTI_INTERFACE,
+                                                                               -1, "(i)", "(sisss)",
+                                                                               "control", 3, pid.c_str(), "1", state.c_str()).get("(i)", &ret);
+               } catch(runtime::Exception& e) {
+                       ERROR("Failed to enforce usb client");
+               }
        }
 };
 
 class Usb : public AbstractPolicyProvider {
 public:
-       Usb();
-
        int setDebuggingState(bool enable);
        bool getDebuggingState();
 
@@ -91,19 +127,12 @@ public:
        int setClientState(bool enable);
        bool getClientState();
 
-       void addDbusSignalHandler(void);
-
 private:
        DebuggingMode debugging;
        Tethering tethering;
        Client client;
 };
 
-Usb::Usb()
-{
-       addDbusSignalHandler();
-}
-
 int Usb::setDebuggingState(bool enable)
 {
        try {
@@ -155,32 +184,6 @@ bool Usb::getClientState()
        return client.get();
 }
 
-void Usb::addDbusSignalHandler(void)
-{
-       dbus::Connection &systemDBus = dbus::Connection::getSystem();
-
-       auto onUSBConnectionStateChanged = [this](dbus::Variant parameters) {
-               unsigned int intparam;
-
-               parameters.get("(u)", &intparam);
-
-               if (intparam == 1 && client.get().value == 0) {
-                       try {
-                               Bundle bundle;
-                               bundle.add("id", "usb-client");
-
-                               runtime::User defaultUser(::tzplatform_getenv(TZ_SYS_DEFAULT_USER));
-                               Launchpad launchpad(defaultUser.getUid());
-                               launchpad.launch("org.tizen.dpm-syspopup", bundle);
-                       } catch(runtime::Exception& e) {
-                               ERROR(e.what());
-                       }
-               }
-       };
-
-       systemDBus.subscribeSignal("", DEVICED_PATH_USB, DEVICED_INTERFACE_USB, SIGNAL_STATE_CHANGED, onUSBConnectionStateChanged);
-}
-
 extern "C" {
 
 #define PRIVILEGE_USB       "http://tizen.org/privilege/dpm.usb"