Implement WakeLock and DBusCaller helpper classes 98/141198/6
authorMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 28 Jul 2017 12:35:15 +0000 (21:35 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Sun, 30 Jul 2017 02:39:53 +0000 (11:39 +0900)
Change-Id: Ica7faa0c497f09946d1965f30a04d238d2db1d9b
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/DBusCaller.h [new file with mode: 0644]
include/DBusMonitor.h
include/ServerUtil.h
src/server/DBusCaller.cpp [new file with mode: 0644]
src/server/DBusMonitor.cpp
src/server/ServerUtil.cpp
src/server/WakeLock.cpp

diff --git a/include/DBusCaller.h b/include/DBusCaller.h
new file mode 100644 (file)
index 0000000..8d2f155
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CONTEXT_DBUS_CALLER_H__
+#define __CONTEXT_DBUS_CALLER_H__
+
+#include <string>
+#include <ContextTypes.h>
+
+namespace ctx {
+
+       class EXPORT_API DBusCaller {
+       public:
+               GVariant* call(const std::string& busName, const std::string& objectPath,
+                               const std::string& interface, const std::string& methodName,
+                               GVariant* param);
+       };
+
+}
+
+#endif
index 9d1452e..3edae44 100644 (file)
@@ -27,9 +27,12 @@ namespace ctx {
 
        class EXPORT_API DBusMonitor {
        public:
-               DBusMonitor(GDBusConnection* conn);
+               DBusMonitor();
                ~DBusMonitor();
 
+               /* Backward compatibility */
+               DBusMonitor(GDBusConnection* conn);
+
                unsigned int subscribe(
                                const char* sender, const char* objPath,
                                const std::string& interface, const std::string& signalName,
@@ -40,7 +43,6 @@ namespace ctx {
                static void __signalCb(GDBusConnection* conn, const gchar* sender, const gchar* path,
                                const gchar* iface, const gchar* name, GVariant* param, gpointer userData);
 
-               GDBusConnection* __connection;
                std::set<unsigned int> __subscriptionIds;
        };
 
index 9739b3f..a6d490a 100644 (file)
@@ -36,6 +36,10 @@ namespace ctx { namespace util {
 
        std::vector<std::string> get_files(const std::string& dir, bool symlink = false);
 
+       void set_dbus_connection(GDBusConnection* conn);
+
+       GDBusConnection* get_dbus_connection();
+
 } }
 
 #endif /* __CONTEXT_SERVER_UTIL_H__ */
diff --git a/src/server/DBusCaller.cpp b/src/server/DBusCaller.cpp
new file mode 100644 (file)
index 0000000..977ab2d
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <ServerUtil.h>
+#include <DBusCaller.h>
+
+using namespace ctx;
+
+GVariant* DBusCaller::call(const std::string& busName, const std::string& objectPath,
+               const std::string& interface, const std::string& methodName, GVariant* param)
+{
+       GError* gerr = NULL;
+
+       GVariant* result = g_dbus_connection_call_sync(util::get_dbus_connection(),
+                       busName.c_str(), objectPath.c_str(), interface.c_str(), methodName.c_str(), param,
+                       NULL, G_DBUS_CALL_FLAGS_NONE, CTX_DBUS_TIMEOUT, NULL, &gerr);
+
+       HANDLE_GERROR(gerr);
+
+       return result;
+}
index dcfc2d7..0477fd1 100644 (file)
  * limitations under the License.
  */
 
+#include <ServerUtil.h>
 #include <IDBusSignalListener.h>
 #include <DBusMonitor.h>
 
 using namespace ctx;
 
-DBusMonitor::DBusMonitor(GDBusConnection* conn) :
-       __connection(conn)
+DBusMonitor::DBusMonitor(GDBusConnection* conn)
+{
+}
+
+DBusMonitor::DBusMonitor()
 {
 }
 
 DBusMonitor::~DBusMonitor()
 {
        for (auto& sid : __subscriptionIds) {
-               g_dbus_connection_signal_unsubscribe(__connection, sid);
+               g_dbus_connection_signal_unsubscribe(util::get_dbus_connection(), sid);
        }
 }
 
 unsigned int DBusMonitor::subscribe(const char* sender, const char* objPath,
                const std::string& interface, const std::string& signalName, IDBusSignalListener* listener)
 {
-       unsigned int sid = g_dbus_connection_signal_subscribe(__connection, sender,
+       unsigned int sid = g_dbus_connection_signal_subscribe(util::get_dbus_connection(), sender,
                        interface.c_str(), signalName.c_str(), objPath, NULL,
                        G_DBUS_SIGNAL_FLAGS_NONE, __signalCb, listener, NULL);
        __subscriptionIds.insert(sid);
@@ -45,7 +49,7 @@ void DBusMonitor::unsubscribe(unsigned int subscriptionId)
 {
        unsigned int erased = __subscriptionIds.erase(subscriptionId);
        if (erased > 0)
-               g_dbus_connection_signal_unsubscribe(__connection, subscriptionId);
+               g_dbus_connection_signal_unsubscribe(util::get_dbus_connection(), subscriptionId);
 }
 
 void DBusMonitor::__signalCb(GDBusConnection* conn, const gchar* sender, const gchar* path,
index 736938f..b1cc832 100644 (file)
@@ -29,6 +29,7 @@ using namespace ctx;
 static GMutex __pathMutex;
 static uid_t __normalUid       = ROOT_UID;     // Last known normal user UID
 static uid_t __containerUid    = ROOT_UID;     // Last known container user UID
+static GDBusConnection* __dbusConnection = NULL;
 
 EXPORT_API bool util::is_normal_user(uid_t uid)
 {
@@ -162,3 +163,13 @@ EXPORT_API std::vector<std::string> util::get_files(const std::string& dirPath,
        closedir(dir);
        return fileNames;
 }
+
+EXPORT_API void set_dbus_connection(GDBusConnection* conn)
+{
+       __dbusConnection = conn;
+}
+
+EXPORT_API GDBusConnection* get_dbus_connection()
+{
+       return __dbusConnection;
+}
index 155f2fc..6da5074 100644 (file)
  */
 
 #include <atomic>
+#include <ServerUtil.h>
 #include <WakeLock.h>
 
+#define DEVICED_DBUS_DEST      "org.tizen.system.deviced"
+#define DISPLAY_OBJ_PATH       "/Org/Tizen/System/DeviceD/Display"
+#define DISPLAY_INTERFACE      "org.tizen.system.deviced.display"
+
 using namespace ctx;
 
 static std::atomic_uint __refCount(0);
@@ -24,13 +29,37 @@ static std::atomic_uint __refCount(0);
 static void __acquire()
 {
        _I("Acquiring a WakeLock");
-       // TODO
+
+       GError* gerr = NULL;
+       GVariant* result = g_dbus_connection_call_sync(util::get_dbus_connection(),
+                       DEVICED_DBUS_DEST, DISPLAY_OBJ_PATH, DISPLAY_INTERFACE, "lockstate",
+                       g_variant_new("(sssi)", "lcdoff", "staycurstate", "NULL", 0),
+                       NULL, G_DBUS_CALL_FLAGS_NONE, CTX_DBUS_TIMEOUT, NULL, &gerr);
+
+       if (result) {
+               //TODO: Print the result?
+               g_variant_unref(result);
+       }
+
+       HANDLE_GERROR(gerr);
 }
 
 static void __release()
 {
        _I("Releasing the WakeLock");
-       // TODO
+
+       GError* gerr = NULL;
+       GVariant* result = g_dbus_connection_call_sync(util::get_dbus_connection(),
+                       DEVICED_DBUS_DEST, DISPLAY_OBJ_PATH, DISPLAY_INTERFACE, "unlockstate",
+                       g_variant_new("(ss)", "lcdoff", "sleepmargin"),
+                       NULL, G_DBUS_CALL_FLAGS_NONE, CTX_DBUS_TIMEOUT, NULL, &gerr);
+
+       if (result) {
+               //TODO: Print the result?
+               g_variant_unref(result);
+       }
+
+       HANDLE_GERROR(gerr);
 }
 
 WakeLock::WakeLock()