From 4aad107137423f5019ca2b6d3a700298fbcfa2eb Mon Sep 17 00:00:00 2001 From: Abhishek Vijay Date: Tue, 14 Jul 2020 14:06:37 +0530 Subject: [PATCH] [Non-ACR] refactoring - dbus init/deinit in listeners Change-Id: Ie431b2684223ece34ea307e1ec2d7273986d3a27 --- include/bm_util.h | 4 ++ src/battery_dump/bm_listeners.c | 85 ++++++++++++++--------------------------- src/bm_server.c | 18 ++++++--- src/bm_util.c | 20 ++++++++++ 4 files changed, 65 insertions(+), 62 deletions(-) diff --git a/include/bm_util.h b/include/bm_util.h index c8686bd..bf2c4b4 100644 --- a/include/bm_util.h +++ b/include/bm_util.h @@ -51,6 +51,10 @@ GVariant* bm_marshal_serialized_data(const bm_total_consumption_h data); const char *bm_get_resource_id_string(gint resource_id); +int bm_set_dbus_connection_obj(GDBusConnection* conn); + +GDBusConnection* bm_get_dbus_connection_obj(void); + void bm_data_free(gpointer data); void bm_atm_free(gpointer data); diff --git a/src/battery_dump/bm_listeners.c b/src/battery_dump/bm_listeners.c index 2f246ba..a714ee1 100644 --- a/src/battery_dump/bm_listeners.c +++ b/src/battery_dump/bm_listeners.c @@ -18,6 +18,7 @@ #include "bm_listeners.h" #include "bm_dbg.h" #include +#include #include #include #include @@ -78,7 +79,7 @@ GHashTable *app_list = NULL; #define DBUS_DEVICED_SLEEP_MEMBER "sleep" #define DBUS_DEVICED_WAKEUP_MEMBER "wakeup" -static GDBusConnection *dbus_connection; +/*static GDBusConnection *dbus_connection; static int bm_listener_dbus_init(void) { @@ -116,7 +117,7 @@ int bm_listener_dbus_deinit() EXIT; return BATTERY_MONITOR_ERROR_NONE; -} +}*/ static int fill_AppId_info(char *app_id) { @@ -1902,38 +1903,27 @@ static int bm_powerlock_unlock_subscribe_listener(void) ENTER; guint ret = 0; - if (!dbus_connection) { + GDBusConnection* dbus_connection = bm_get_dbus_connection_obj(); + if (dbus_connection == NULL) { _ERR("dbus connection is null"); EXIT; return 1; } - ret = g_dbus_connection_signal_subscribe(dbus_connection, - NULL, - DBUS_DEVICED_IFACE, - DBUS_DEVICED_PL_MEMBER, - DBUS_DEVICED_PATH, - NULL, - G_DBUS_SIGNAL_FLAGS_NONE, - __powerlock_signal_callback, - NULL, - NULL); + ret = g_dbus_connection_signal_subscribe(dbus_connection, NULL, DBUS_DEVICED_IFACE, + DBUS_DEVICED_PL_MEMBER, DBUS_DEVICED_PATH, NULL, + G_DBUS_SIGNAL_FLAGS_NONE, __powerlock_signal_callback, + NULL, NULL); if (ret == 0) { _ERR("Failed to subscrive bus signal"); //LCOV_EXCL_LINE System Error return 1; } - ret = g_dbus_connection_signal_subscribe(dbus_connection, - NULL, - DBUS_DEVICED_IFACE, - DBUS_DEVICED_PU_MEMBER, - DBUS_DEVICED_PATH, - NULL, - G_DBUS_SIGNAL_FLAGS_NONE, - __powerunlock_signal_callback, - NULL, - NULL); + ret = g_dbus_connection_signal_subscribe(dbus_connection, NULL, DBUS_DEVICED_IFACE, + DBUS_DEVICED_PU_MEMBER, DBUS_DEVICED_PATH, + NULL, G_DBUS_SIGNAL_FLAGS_NONE, __powerunlock_signal_callback, + NULL, NULL); if (ret == 0) { _ERR("Failed to subscrive bus signal"); //LCOV_EXCL_LINE System Error @@ -1949,38 +1939,25 @@ static int bm_sleep_wakeup_subscribe_listener(void) ENTER; guint ret = 0; + GDBusConnection* dbus_connection = bm_get_dbus_connection_obj(); if (!dbus_connection) { _ERR("dbus connection is null"); EXIT; return 1; } - ret = g_dbus_connection_signal_subscribe(dbus_connection, - NULL, - DBUS_DEVICED_IFACE, - DBUS_DEVICED_SLEEP_MEMBER, - DBUS_DEVICED_PATH, - NULL, - G_DBUS_SIGNAL_FLAGS_NONE, - __sleep_signal_callback, - NULL, - NULL); + ret = g_dbus_connection_signal_subscribe(dbus_connection, NULL, DBUS_DEVICED_IFACE, + DBUS_DEVICED_SLEEP_MEMBER, DBUS_DEVICED_PATH, NULL, + G_DBUS_SIGNAL_FLAGS_NONE, __sleep_signal_callback, NULL, NULL); if (ret == 0) { _ERR("Failed to subscrive bus signal"); //LCOV_EXCL_LINE System Error return 1; } - ret = g_dbus_connection_signal_subscribe(dbus_connection, - NULL, - DBUS_DEVICED_IFACE, - DBUS_DEVICED_WAKEUP_MEMBER, - DBUS_DEVICED_PATH, - NULL, - G_DBUS_SIGNAL_FLAGS_NONE, - __wakeup_signal_callback, - NULL, - NULL); + ret = g_dbus_connection_signal_subscribe(dbus_connection, NULL, DBUS_DEVICED_IFACE, + DBUS_DEVICED_WAKEUP_MEMBER, DBUS_DEVICED_PATH, NULL, + G_DBUS_SIGNAL_FLAGS_NONE, __wakeup_signal_callback, NULL, NULL); if (ret == 0) { _ERR("Failed to subscrive bus signal"); //LCOV_EXCL_LINE System Error @@ -1996,22 +1973,16 @@ static int bm_app_status_listener(void) ENTER; guint ret = 0; + GDBusConnection* dbus_connection = bm_get_dbus_connection_obj(); if (!dbus_connection) { _ERR("dbus connection is null"); EXIT; return 1; } - ret = g_dbus_connection_signal_subscribe(dbus_connection, - NULL, - "org.tizen.aul.AppStatus", - "AppStatusChange", - "/Org/Tizen/Aul/AppStatus", - NULL, - G_DBUS_SIGNAL_FLAGS_NONE, - __app_status_signal_callback, - NULL, - NULL); + ret = g_dbus_connection_signal_subscribe(dbus_connection, NULL, "org.tizen.aul.AppStatus", + "AppStatusChange", "/Org/Tizen/Aul/AppStatus", NULL, + G_DBUS_SIGNAL_FLAGS_NONE, __app_status_signal_callback, NULL, NULL); if (ret == 0) { _ERR("Failed to subscrive bus signal"); //LCOV_EXCL_LINE System Error @@ -2112,9 +2083,9 @@ int bd_initialize_listeners(void) if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) _ERR("pthread_attr_setdetachstate failed"); - /*Initialize DBus*/ + /*Initialize DBus* if (bm_listener_dbus_init()) - _ERR("bm_listener_dbus_init failed \n"); + _ERR("bm_listener_dbus_init failed \n"); */ /* Wifi callbacks */ if (!bm_wifi_subscribe_listeners()) @@ -2250,9 +2221,9 @@ int bd_deinitialize_listeners() tel_deinit(handle); } - /*Dbus Deinitialize*/ + /*Dbus Deinitialize* if (bm_listener_dbus_deinit()) - _ERR("failed to de-init dbus "); + _ERR("failed to de-init dbus "); */ /* de-initialize pthread attributes */ if (pthread_attr_destroy(&attr) != 0) diff --git a/src/bm_server.c b/src/bm_server.c index 177d9c6..3142f2f 100644 --- a/src/bm_server.c +++ b/src/bm_server.c @@ -258,15 +258,20 @@ static int bm_set_dbus_interface_handles(BatteryMonitorIface *iface) { ENTER; - BM_RETURN_VAL((iface != NULL), {}, BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "interface handle is null"); + BM_RETURN_VAL((iface != NULL), {}, \ + BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "interface handle is null"); - iface->handle_get_mah_usage_by_app_id_for_resource_id_custom_interval = &bm_get_mah_usage_by_app_id_for_resource_id_custom_interval; + iface->handle_get_mah_usage_by_app_id_for_resource_id_custom_interval = \ + &bm_get_mah_usage_by_app_id_for_resource_id_custom_interval; - iface->handle_get_total_mah_usage_by_app_id_custom_interval = &bm_get_total_mah_usage_by_app_id_custom_interval; + iface->handle_get_total_mah_usage_by_app_id_custom_interval = \ + &bm_get_total_mah_usage_by_app_id_custom_interval; - iface->handle_get_total_mah_usage_by_resource_id_custom_interval = &bm_get_total_mah_usage_by_resource_id_custom_interval; + iface->handle_get_total_mah_usage_by_resource_id_custom_interval = \ + &bm_get_total_mah_usage_by_resource_id_custom_interval; - iface->handle_get_usage_handle_by_app_id_for_all_resource_id_custom_interval = &bm_get_usage_handle_by_app_id_for_all_resource_id_custom_interval; + iface->handle_get_usage_handle_by_app_id_for_all_resource_id_custom_interval = \ + &bm_get_usage_handle_by_app_id_for_all_resource_id_custom_interval; EXIT; return BATTERY_MONITOR_ERROR_NONE; @@ -298,6 +303,9 @@ static void on_bus_acquired(GDBusConnection *connection, const gchar *name, gpoi return; } + /* set dbus connection object */ + bm_set_dbus_connection_obj(g_dbus_interface_skeleton_get_connection(interface)); + _INFO("connecting battery-monitor signals start"); if (BATTERY_MONITOR_ERROR_NONE != bm_set_dbus_interface_handles(iface)) { diff --git a/src/bm_util.c b/src/bm_util.c index 7961a56..af27291 100644 --- a/src/bm_util.c +++ b/src/bm_util.c @@ -23,6 +23,7 @@ #include "bm_common.h" static cynara *p_cynara; +GDBusConnection* connection_obj = NULL; GDBusErrorEntry bm_svc_errors[] = { {BATTERY_MONITOR_ERROR_NONE, BATTERY_MONITOR_SVC_ERROR_PREFIX".NoError"}, @@ -311,6 +312,25 @@ const char *bm_get_resource_id_string(gint resource_id) } } +int bm_set_dbus_connection_obj(GDBusConnection* conn) +{ + BM_CHECK_INPUT_PARAM(conn); + + connection_obj = conn; + + return BATTERY_MONITOR_ERROR_NONE; +} + +GDBusConnection* bm_get_dbus_connection_obj(void) +{ + if (connection_obj == NULL) { + _ERR("connection object not available"); + return NULL; + } + + return connection_obj; +} + void bm_data_free(gpointer data) { ENTER; -- 2.7.4