Use libsyscommon 97/245997/2
authorYunmi Ha <yunmi.ha@samsung.com>
Wed, 21 Oct 2020 11:25:30 +0000 (20:25 +0900)
committerYunmi Ha <yunmi.ha@samsung.com>
Thu, 22 Oct 2020 03:14:13 +0000 (12:14 +0900)
To eliminate dbus proxy in feedback api, use libsyscommon instead.

Change-Id: I767494466cda183a33f91a5d7ad9a302ec2ebc49
Signed-off-by: Yunmi Ha <yunmi.ha@samsung.com>
CMakeLists.txt
packaging/libfeedback.spec
src/dbus.c [deleted file]
src/dbus.h [deleted file]
src/feedback.c
src/vibrator.c

index f9852ae..c284c7c 100644 (file)
@@ -16,7 +16,6 @@ SET(SRCS
        src/devices.c
        src/parser.c
        src/feedback.c
-       src/dbus.c
        src/check.c)
 
 SET(HEADERS
@@ -33,7 +32,17 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src)
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(pkgs REQUIRED vconf mm-keysound dlog glib-2.0 dbus-1 gio-2.0 capi-system-info capi-media-sound-manager capi-media-wav-player)
+pkg_check_modules(pkgs REQUIRED
+               vconf
+               mm-keysound
+               dlog
+               glib-2.0
+               gio-2.0
+               capi-system-info
+               capi-media-sound-manager
+               capi-media-wav-player
+               libsyscommon
+)
 
 FOREACH(flag ${pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
index 59ce44b..d1132f0 100644 (file)
@@ -18,9 +18,9 @@ BuildRequires:  pkgconfig(gio-2.0)
 BuildRequires:  pkgconfig(capi-base-common)
 BuildRequires:  pkgconfig(capi-media-sound-manager)
 BuildRequires:  pkgconfig(capi-media-wav-player)
-BuildRequires:  pkgconfig(dbus-1)
 BuildRequires:  pkgconfig(libtzplatform-config)
 BuildRequires:  pkgconfig(capi-system-info)
+BuildRequires:  pkgconfig(libsyscommon)
 %if 0%{?gcov:1}
 BuildRequires: lcov
 %endif
diff --git a/src/dbus.c b/src/dbus.c
deleted file mode 100644 (file)
index 7899c71..0000000
+++ /dev/null
@@ -1,398 +0,0 @@
-/*
- * feedback
- *
- * Copyright (c) 2012 - 2013 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 <stdio.h>
-#include <stdint.h>
-#include <stdbool.h>
-#include <errno.h>
-
-#include "common.h"
-#include "log.h"
-#include "dbus.h"
-
-/* -1 is a default timeout value, it's converted to 25*1000 internally. */
-#define DBUS_REPLY_TIMEOUT     (-1)
-
-/** extract from dbus/dbus-protocol.h
- * (GDbus use the same maximum value.)
- * Max length in bytes of a bus name, interface, or member (not object
- * path, paths are unlimited). This is limited because lots of stuff
- * is O(n) in this number, plus it would be obnoxious to type in a
- * paragraph-long method name so most likely something like that would
- * be an exploit.
- */
-#define DBUS_MAXIMUM_NAME_LENGTH 255
-
-#define SIGNAL_VIBRATOR_INITIATED "InitiateVibrator"
-
-struct feedback_restart_callback {
-       feedback_restart_cb func;
-       guint feedback_id;
-};
-
-struct proxy_node {
-       GDBusProxy *proxy;
-       char *dest;
-       char *path;
-       char *interface;
-};
-
-static GList *proxy_pool;
-static pthread_mutex_t dmutex = PTHREAD_MUTEX_INITIALIZER;
-static int bus_init;
-
-static dd_list *callback_list;
-
-static int g_dbus_error_to_errno(int code)
-{
-       /**
-        * if device is not supported,
-        * deviced does not register the method call of the device.
-        * in this case, dbus will return UNKNOWN_METHOD error.
-        */
-       /* refer to gio/gioenums.h */
-       if (code == G_DBUS_ERROR_ACCESS_DENIED)
-               return -EACCES;
-       else if (code == G_DBUS_ERROR_UNKNOWN_METHOD)
-               return -ENOTSUP;
-       return -ECOMM;
-}
-
-static struct proxy_node *find_matched_proxy_node(const char *dest,
-               const char *path,
-               const char *interface)
-{
-       GList *elem;
-       struct proxy_node *node;
-       int plen;
-
-       if (!dest || !path || !interface)
-               return NULL;
-
-       plen = strlen(path) + 1;
-
-       /* find matched proxy object */
-       for (elem = proxy_pool; elem; elem = elem->next) {
-               node = elem->data;
-               if (!node)
-                       continue;
-               if (!strncmp(node->dest, dest, DBUS_MAXIMUM_NAME_LENGTH) &&
-                   !strncmp(node->path, path, plen) &&
-                   !strncmp(node->interface, interface,
-                           DBUS_MAXIMUM_NAME_LENGTH))
-                       return node;
-       }
-
-       return NULL;
-}
-
-//LCOV_EXCL_START Not called Callback
-static void on_name_vanished(GDBusConnection *connection,
-               const gchar *name,
-               gpointer user_data)
-{
-       GList *elem;
-       GList *next;
-       struct proxy_node *node;
-
-       pthread_mutex_lock(&dmutex);
-       for (elem = proxy_pool, next = g_list_next(elem); elem;
-               elem = next, next = g_list_next(elem)) {
-               node = elem->data;
-               if (!node)
-                       continue;
-               proxy_pool = g_list_delete_link(proxy_pool, elem);
-               g_object_unref(node->proxy);
-               free(node->dest);
-               free(node->path);
-               free(node->interface);
-               free(node);
-       }
-       pthread_mutex_unlock(&dmutex);
-}
-//LCOV_EXCL_STOP
-
-static GDBusConnection *get_dbus_connection(void)
-{
-       GError *err = NULL;
-       static GDBusConnection *conn;
-
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-       if (!conn) {
-               //LCOV_EXCL_START System Error
-               if (err)
-                       _D("Fail to get dbus connection: %s", err->message);
-               else
-                       _D("Fail to get dbus connection");
-               return NULL;
-               //LCOV_EXCL_STOP
-       }
-
-       return conn;
-}
-
-static GDBusProxy *get_proxy_from_proxy_pool(const char *dest,
-               const char *path,
-               const char *interface,
-               GError **err)
-{
-       GDBusConnection *conn;
-       GDBusProxy *proxy;
-       struct proxy_node *node;
-
-       if (!dest || !path || !interface) {
-               if (err)
-                       g_set_error(err, G_IO_ERROR,
-                                       G_IO_ERROR_INVALID_ARGUMENT,
-                                       "Cannot determine destination address");
-               return NULL;
-       }
-
-       /* find matched proxy node in proxy pool */
-       node = find_matched_proxy_node(dest, path, interface);
-       if (node)
-               return node->proxy;
-
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, err);
-       if (!conn)
-               return NULL;
-
-       if (!bus_init) {
-               bus_init++;
-               g_bus_watch_name_on_connection(conn,
-                       DEVICED_BUS_NAME,
-                       G_BUS_NAME_WATCHER_FLAGS_NONE,
-                       NULL,
-                       on_name_vanished,
-                       NULL,
-                       NULL);
-       }
-
-       proxy = g_dbus_proxy_new_sync(conn,
-                       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
-                       NULL,      /* GDBusinterfaceinfo */
-                       dest,      /* bus name */
-                       path,      /* object path */
-                       interface, /* interface name */
-                       NULL,      /* GCancellable */
-                       err);
-       if (!proxy)
-               return NULL;
-
-       node = malloc(sizeof(struct proxy_node));
-       if (!node) {
-               //LCOV_EXCL_START System Error
-               g_object_unref(proxy);
-               if (err)
-                       g_set_error(err, G_IO_ERROR,
-                                       G_IO_ERROR_FAILED,
-                                       "Cannot allocate proxy_node memory");
-               return NULL;
-               //LCOV_EXCL_STOP
-       }
-
-       node->proxy = proxy;
-       node->dest = strdup(dest);
-       node->path = strdup(path);
-       node->interface = strdup(interface);
-
-       proxy_pool = g_list_append(proxy_pool, node);
-
-       return proxy;
-}
-
-int dbus_method_sync_var(const char *dest, const char *path,
-               const char *interface, const char *method, GVariant *param)
-{
-       GDBusProxy *proxy;
-       GError *err = NULL;
-       GVariant *output;
-       int result;
-
-#if !GLIB_CHECK_VERSION(2, 35, 0)
-       g_type_init();
-#endif
-
-       pthread_mutex_lock(&dmutex);
-       proxy = get_proxy_from_proxy_pool(dest, path, interface, &err);
-//LCOV_EXCL_START System Error
-       if (!proxy) {
-               pthread_mutex_unlock(&dmutex);
-               _E("fail to get proxy from proxy pool : %s-%s (%d-%s)",
-                               interface, method, err->code, err->message);
-               result = g_dbus_error_to_errno(err->code);
-               g_clear_error(&err);
-               return result;
-       }
-//LCOV_EXCL_STOP
-       output = g_dbus_proxy_call_sync(proxy,
-                       method,                       /* method name */
-                       param, /* parameters */
-                       G_DBUS_CALL_FLAGS_NONE,
-                       DBUS_REPLY_TIMEOUT,           /* timeout */
-                       NULL,                         /* GCancellable */
-                       &err);
-       pthread_mutex_unlock(&dmutex);
-
-//LCOV_EXCL_START System Error
-       if (!output) {
-               if (!err) {
-                       _E("g_dbus_proxy_call_sync error : %s-%s",
-                                       interface, method);
-                       return -EPERM;
-               }
-               _E("g_dbus_proxy_call_sync error : %s-%s (%d-%s)",
-                               interface, method, err->code, err->message);
-               result = g_dbus_error_to_errno(err->code);
-               g_clear_error(&err);
-               return result;
-//LCOV_EXCL_STOP
-       }
-
-       /* get output value */
-       g_variant_get(output, "(i)", &result);
-
-       g_variant_unref(output);
-
-       return result;
-}
-
-//LCOV_EXCL_START Not called Callback
-static void feedback_signal_callback(GDBusConnection *conn,
-               const gchar *sender,
-               const gchar *path,
-               const gchar *iface,
-               const gchar *signal,
-               GVariant *params,
-               gpointer user_data)
-{
-       size_t iface_len, signal_len;
-       struct feedback_restart_callback *callback;
-       dd_list *elem;
-       int ret;
-
-       if (!params || !sender || !path || !iface || !signal)
-               return;
-
-       iface_len = strlen(iface) + 1;
-       signal_len = strlen(signal) + 1;
-
-       if (strncmp(iface, VIBRATOR_INTERFACE_HAPTIC, iface_len))
-               return;
-
-       if (strncmp(signal, SIGNAL_VIBRATOR_INITIATED, signal_len))
-               return;
-
-       DD_LIST_FOREACH(callback_list, elem, callback) {
-               if (!callback->func)
-                       continue;
-               ret = callback->func();
-               if (ret < 0)
-                       _E("Failed to call restart callback");
-       }
-}
-//LCOV_EXCL_STOP
-
-int register_signal_handler(feedback_restart_cb func)
-{
-       GDBusConnection *conn;
-       guint feedback_id = 0;
-       struct feedback_restart_callback *callback;
-       dd_list *elem;
-
-       DD_LIST_FOREACH(callback_list, elem, callback) {
-               if (callback->func != func)
-                       continue;
-               if (callback->feedback_id == 0)
-                       continue;
-
-               return -EEXIST;
-       }
-
-       callback = (struct feedback_restart_callback *)malloc(sizeof(struct feedback_restart_callback));
-       if (!callback) {
-//LCOV_EXCL_START System Error
-               _E("malloc() failed");
-               return -ENOMEM;
-//LCOV_EXCL_STOP
-       }
-
-       conn = get_dbus_connection();
-       if (!conn) {
-//LCOV_EXCL_START System Error
-               free(callback);
-               _E("Failed to get dbus connection");
-               return -EPERM;
-//LCOV_EXCL_STOP
-       }
-
-       feedback_id = g_dbus_connection_signal_subscribe(conn,
-                       NULL,
-                       VIBRATOR_INTERFACE_HAPTIC,
-                       NULL,
-                       NULL,
-                       NULL,
-                       G_DBUS_SIGNAL_FLAGS_NONE,
-                       feedback_signal_callback,
-                       NULL,
-                       NULL);
-       if (feedback_id == 0) {
-//LCOV_EXCL_START System Error
-               free(callback);
-               _E("Failed to subscrive bus signal");
-               return -EPERM;
-//LCOV_EXCL_STOP
-       }
-
-       callback->func = func;
-       callback->feedback_id = feedback_id;
-
-       DD_LIST_APPEND(callback_list, callback);
-
-       return 0;
-}
-
-int unregister_signal_handler(feedback_restart_cb func)
-{
-       GDBusConnection *conn;
-       struct feedback_restart_callback *callback;
-       dd_list *elem, *next;
-
-       if (!func)
-               return -EINVAL;
-
-       conn = get_dbus_connection();
-       if (!conn) {
-//LCOV_EXCL_START System Error
-               _E("Failed to get dbus connection");
-               return -EPERM;
-//LCOV_EXCL_STOP
-       }
-
-       DD_LIST_FOREACH_SAFE(callback_list, elem, next, callback) {
-               if (callback->func != func)
-                       continue;
-               if (callback->feedback_id > 0)
-                       g_dbus_connection_signal_unsubscribe(conn, callback->feedback_id);
-               DD_LIST_REMOVE(callback_list, callback);
-               free(callback);
-       }
-
-       return 0;
-}
diff --git a/src/dbus.h b/src/dbus.h
deleted file mode 100644 (file)
index f573c43..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * feedback
- *
- * Copyright (c) 2012 - 2013 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 __DBUS_H__
-#define __DBUS_H__
-
-#include <gio/gio.h>
-#include <glib.h>
-
-/*
- * Device daemon
- */
-#define DEVICED_BUS_NAME                    "org.tizen.system.deviced"
-#define DEVICED_OBJECT_PATH                 "/Org/Tizen/System/DeviceD"
-#define DEVICED_INTERFACE_NAME              DEVICED_BUS_NAME
-/* Led service: play/stop led operations about led */
-#define DEVICED_PATH_LED                    DEVICED_OBJECT_PATH"/Led"
-#define DEVICED_INTERFACE_LED               DEVICED_INTERFACE_NAME".Led"
-/* Haptic service: operatioins about haptic */
-#define VIBRATOR_BUS_NAME                    "org.tizen.system.vibrator"
-#define VIBRATOR_OBJECT_PATH                 "/Org/Tizen/System/Vibrator"
-#define VIBRATOR_INTERFACE_NAME              VIBRATOR_BUS_NAME
-#define VIBRATOR_PATH_HAPTIC                 VIBRATOR_OBJECT_PATH"/Haptic"
-#define VIBRATOR_INTERFACE_HAPTIC            VIBRATOR_INTERFACE_NAME".haptic"
-
-struct dbus_byte {
-       const unsigned char *data;
-       int size;
-};
-
-int dbus_method_sync_var(const char *dest, const char *path,
-               const char *interface, const char *method, GVariant *param);
-
-/**
- * If result is NULL, err is set.
- * Do not invoke g_variant_unref() with result.
- */
-typedef void (*dbus_pending_cb)(void *data, GVariant *result, GError *err);
-
-typedef int (*feedback_restart_cb)();
-int register_signal_handler(feedback_restart_cb func);
-int unregister_signal_handler(feedback_restart_cb func);
-#endif
index ef978f4..83febea 100644 (file)
 #include <stdbool.h>
 #include <string.h>
 #include <limits.h>
+#include <libsyscommon/dbus-system.h>
 
 #include "feedback.h"
 #include "feedback-internal.h"
 #include "profiles.h"
 #include "devices.h"
 #include "log.h"
-#include "dbus.h"
 
 #ifndef API
 #define API __attribute__ ((visibility("default")))
 
 static unsigned int init_cnt;
 static pthread_mutex_t fmutex = PTHREAD_MUTEX_INITIALIZER;
+static guint signal_id = 0;
+
+#define SIGNAL_VIBRATOR_INITIATED "InitiateVibrator"
 
 //LCOV_EXCL_START Not called Callback
-static int restart_callback()
+static void restart_callback(GDBusConnection *conn,
+       const gchar *sender,
+       const gchar *path,
+       const gchar *iface,
+       const gchar *name,
+       GVariant *params,
+       gpointer user_data)
 {
        const struct device_ops *dev;
 
        dev = find_device(FEEDBACK_TYPE_VIBRATION);
        if (!dev) {
                _E("Not supported device : type(FEEDBACK_TYPE_VIBRATION)"); //LCOV_EXCL_LINE
-               return -1;
+               return;
        }
        if (dev->init)
                dev->init();
-       return 0;
 }
 //LCOV_EXCL_STOP
 
@@ -67,9 +75,14 @@ API int feedback_initialize(void)
                return FEEDBACK_ERROR_NONE;
        }
 
-       ret = register_signal_handler(restart_callback);
-       if (ret < 0)
-               _E("Fail to register signal handler: %d", ret); //LCOV_EXCL_LINE System Error
+       ret = subscribe_dbus_signal(NULL, NULL,
+                               VIBRATOR_INTERFACE_HAPTIC,
+                               SIGNAL_VIBRATOR_INITIATED,
+                               restart_callback, NULL, NULL);
+       if (ret <= 0)
+               _E("Failed to register signal handler. ret=%d", ret); //LCOV_EXCL_LINE System Error
+       else
+               signal_id = ret;
 
        /* initialize device */
        devices_init();
@@ -84,8 +97,6 @@ API int feedback_initialize(void)
 
 API int feedback_deinitialize(void)
 {
-       int ret;
-
        pthread_mutex_lock(&fmutex);
        if (!init_cnt) {
                pthread_mutex_unlock(&fmutex);
@@ -97,9 +108,8 @@ API int feedback_deinitialize(void)
                return FEEDBACK_ERROR_NONE;
        }
 
-       ret = unregister_signal_handler(restart_callback);
-       if (ret < 0)
-               _E("Fail to unregister signal handler: %d", ret); //LCOV_EXCL_LINE System Error
+       if (signal_id > 0)
+               unsubscribe_dbus_signal(NULL, signal_id);
 
        /* deinitialize device */
        devices_exit();
index 06ee0a7..ea5de75 100644 (file)
 #include <vconf.h>
 #include <sys/stat.h>
 #include <system_info.h>
+#include <libsyscommon/dbus-system.h>
 
 #include "feedback.h"
 #include "profiles.h"
 #include "parser.h"
 #include "devices.h"
 #include "log.h"
-#include "dbus.h"
 
 #define HAPTIC_DEVICE                          0
 
@@ -77,21 +77,21 @@ inline int is_vibration_mode(void)
 
 static int haptic_open(void)
 {
-       return dbus_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
+       return dbus_handle_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
                        VIBRATOR_INTERFACE_HAPTIC, METHOD_OPEN,
                        g_variant_new("(i)", HAPTIC_DEVICE));
 }
 
 static int haptic_close(unsigned int handle)
 {
-       return dbus_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
+       return dbus_handle_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
                        VIBRATOR_INTERFACE_HAPTIC, METHOD_CLOSE,
                        g_variant_new("(u)", handle));
 }
 
 static int haptic_is_supported(const char *pattern)
 {
-       return dbus_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
+       return dbus_handle_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
                        VIBRATOR_INTERFACE_HAPTIC, METHOD_IS_SUPPORTED,
                        g_variant_new("(s)", pattern));
 }
@@ -101,14 +101,14 @@ static int haptic_vibrate_effect(unsigned int handle,
                                                                int feedback,
                                                                int priority)
 {
-       return dbus_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
+       return dbus_handle_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
                        VIBRATOR_INTERFACE_HAPTIC, METHOD_VIBRATE_PATTERN,
                        g_variant_new("(usii)", handle, pattern, feedback, priority));
 }
 
 static int haptic_vibrate_stop(unsigned int handle)
 {
-       return dbus_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
+       return dbus_handle_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
                        VIBRATOR_INTERFACE_HAPTIC, METHOD_STOP,
                        g_variant_new("(u)", handle));
 }