From 7b51159b3287c009e906111b58ef4816a79cdecf Mon Sep 17 00:00:00 2001 From: Atul Rai Date: Fri, 21 Sep 2018 10:58:00 +0530 Subject: [PATCH] Added dbus request handler and event sender This patch adds dbus request handler and event sender functionalities in uafw-manager daemon. Signed-off-by: Atul Rai --- include/uafw-internal.h | 6 +- uafw-daemon/CMakeLists.txt | 3 + uafw-daemon/include/uafw-manager-common.h | 110 +++++++ uafw-daemon/src/uafw-manager-common.c | 131 ++++++++ uafw-daemon/src/uafw-manager-event-sender.c | 64 ++++ uafw-daemon/src/uafw-manager-main.c | 70 ++++ uafw-daemon/src/uafw-manager-request-handler.c | 439 +++++++++++++++++++++++++ 7 files changed, 822 insertions(+), 1 deletion(-) create mode 100644 uafw-daemon/include/uafw-manager-common.h create mode 100644 uafw-daemon/src/uafw-manager-common.c create mode 100644 uafw-daemon/src/uafw-manager-event-sender.c create mode 100644 uafw-daemon/src/uafw-manager-request-handler.c diff --git a/include/uafw-internal.h b/include/uafw-internal.h index 82e5e11..e5475fb 100644 --- a/include/uafw-internal.h +++ b/include/uafw-internal.h @@ -32,6 +32,10 @@ extern "C" { #define UAFW_EVENT_INTERFACE "org.projectx.uafw_event" #define UAFW_EVENT_PATH "org/projectx/uafw/event" +#define NAME_OWNER_CHANGED "NameOwnerChanged" +#define UAFW_SERVICE_DBUS "org.freedesktop.DBus" +#define UAFW_INTERFACE_DBUS "org.freedesktop.DBus" + #define FOREACH_REQUEST(REQUEST) \ REQUEST(UAFW_REQUEST_START_PRESENCE_DETECTION) \ REQUEST(UAFW_REQUEST_STOP_PRESENCE_DETECTION) \ @@ -54,6 +58,7 @@ typedef enum { ERROR(UAFW_ERROR_TIMEOUT, 0x02) \ ERROR(UAFW_ERROR_ALREADY_REGISTERED, 0x03) \ ERROR(UAFW_ERROR_NOT_REGISTERED, 0x04) \ + ERROR(UAFW_ERROR_NOT_SUPPORTED, 0x05) \ #define GENERATE_ERROR_ENUM(ENUM, offset) ENUM=-offset, #define GENERATE_ERROR_STRING(STRING, offset) #STRING, @@ -64,7 +69,6 @@ typedef enum { #define UAFW_SIGNAL_USER_PRESENCE_DETECTED "UserPresenceDetected" - #ifdef __cplusplus } #endif diff --git a/uafw-daemon/CMakeLists.txt b/uafw-daemon/CMakeLists.txt index b9a39cb..46ff730 100644 --- a/uafw-daemon/CMakeLists.txt +++ b/uafw-daemon/CMakeLists.txt @@ -4,6 +4,9 @@ PROJECT(uafw-manager C) #Include Source files for bluetooth service common files only SET(SRCS src/uafw-manager-main.c +src/uafw-manager-common.c +src/uafw-manager-request-handler.c +src/uafw-manager-event-sender.c ) IF("${CMAKE_BUILD_TYPE}" STREQUAL "") diff --git a/uafw-daemon/include/uafw-manager-common.h b/uafw-daemon/include/uafw-manager-common.h new file mode 100644 index 0000000..03dc056 --- /dev/null +++ b/uafw-daemon/include/uafw-manager-common.h @@ -0,0 +1,110 @@ +/* + * User Awareness Manager API + * + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * + * Author: Atul Rai + * + * 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 __UAFW_MANAGER_COMMON_H__ +#define __UAFW_MANAGER_COMMON_H__ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "UAFW_MANAGER" + +#define LOG_COLOR_RESET "\033[0m" +#define LOG_COLOR_RED "\033[31m" +#define LOG_COLOR_YELLOW "\033[33m" +#define LOG_COLOR_GREEN "\033[32m" +#define LOG_COLOR_BLUE "\033[36m" +#define LOG_COLOR_PURPLE "\033[35m" + +#define UA_DBG(fmt, args...) \ + SLOGD(fmt, ##args) +#define UA_INFO(fmt, args...) \ + SLOGI(fmt, ##args) +#define UA_INFO_C(fmt, arg...) \ + SLOGI_IF(TRUE, LOG_COLOR_GREEN" "fmt" "LOG_COLOR_RESET, ##arg) +#define UA_WARN(fmt, arg...) \ + SLOGI_IF(TRUE, LOG_COLOR_YELLOW" "fmt" "LOG_COLOR_RESET, ##arg) +#define UA_ERR(fmt, arg...) \ + SLOGI_IF(TRUE, LOG_COLOR_RED" "fmt" "LOG_COLOR_RESET, ##arg) + +#define FUNC_ENTRY UA_DBG("+") +#define FUNC_EXIT UA_DBG("-") + +#define ret_if(expr) \ +do { \ + if (expr) { \ + UA_ERR("(%s) return", #expr); \ + return; \ + } \ +} while (0) + +#define retv_if(expr, val) \ +do { \ + if (expr) { \ + UA_ERR("(%s) return", #expr); \ + return (val); \ + } \ +} while (0) + +typedef struct { + int result; + int function; + char *sender; + GDBusMethodInvocation *context; + gpointer data; +} uafw_request_context_t; + +int _uafw_manager_register(void); + +void _uafw_manager_unregister(void); + +GSList *_uafw_manager_get_request_list(void); + +void _uafw_manager_remove_req_ctxt_from_list( + uafw_request_context_t *req_info); + +void _uafw_manager_method_return( + GDBusMethodInvocation *invocation, + GArray *out_param, int result); + +int _uafw_manager_send_event( + const char* dest, int event, GVariant *param); + +GDBusConnection *_uafw_manager_get_gdbus_conn(void); + +const char *_uafw_manager_request_to_str(unsigned int req); + +const char *_uafw_manager_event_to_str(unsigned int event); + +const char *_uafw_manager_error_to_str(int error); + +#ifdef __cplusplus +} +#endif +#endif /* __UAFW_MANAGER_COMMON_H__ */ diff --git a/uafw-daemon/src/uafw-manager-common.c b/uafw-daemon/src/uafw-manager-common.c new file mode 100644 index 0000000..ff309a9 --- /dev/null +++ b/uafw-daemon/src/uafw-manager-common.c @@ -0,0 +1,131 @@ +/* + * User Awareness Manager API + * + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * + * Author: Atul Rai + * + * 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 "uafw-api.h" +#include "uafw-internal.h" + +#include "uafw-manager-common.h" + +const char *event_string[] = { + FOREACH_EVENT(GENERATE_EVENT_STRING) +}; + +const char *request_string[] = { + FOREACH_REQUEST(GENERATE_REQUEST_STRING) +}; + +const char *error_string[] = { + FOREACH_ERROR(GENERATE_ERROR_STRING) +}; + +static GDBusConnection *gdbus_conn = NULL; + +static GDBusConnection *__uafw_manager_get_private_gdbus_conn(void) +{ + FUNC_ENTRY; + GError *error = NULL; + char *address; + GDBusConnection *private_conn = NULL; + + address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &error); + if (address == NULL) { + UA_ERR("Failed to get bus address"); + if (error) { + UA_ERR("Error: %s", error->message); + g_clear_error(&error); + } + return NULL; + } + + private_conn = g_dbus_connection_new_for_address_sync(address, + G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | + G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION, + NULL, /* GDBusAuthObserver */ + NULL, + &error); + g_free(address); + if (!private_conn) { + UA_ERR("Unable to connect to dbus"); + if (error) { + UA_ERR("Error: %s", error->message); + g_clear_error(&error); + } + return NULL; + } + + FUNC_EXIT; + return private_conn; +} + +GDBusConnection *_uafw_manager_get_gdbus_conn(void) +{ + FUNC_ENTRY; + + if (gdbus_conn) { + if (g_dbus_connection_is_closed(gdbus_conn)) + gdbus_conn = __uafw_manager_get_private_gdbus_conn(); + + FUNC_EXIT; + return gdbus_conn; + } + + gdbus_conn = __uafw_manager_get_private_gdbus_conn(); + FUNC_EXIT; + return gdbus_conn; +} + +const char *_uafw_manager_request_to_str(unsigned int req) +{ + retv_if(UAFW_REQUEST_MAX <= req, NULL); + + return request_string[req]; +} + +const char *_uafw_manager_event_to_str(unsigned int event) +{ + retv_if(UA_EVENT_MAX <= event, NULL); + + return event_string[event]; +} + +const char *_uafw_manager_error_to_str(int error) +{ + int arr_size = (sizeof(error_string)/sizeof(char*)); + int err_id = -error; + + UA_DBG("arr_size: %d, err_id: %d", arr_size, err_id); + retv_if(arr_size <= err_id, NULL); + + return error_string[err_id]; +} + +void _uafw_manager_method_return( + GDBusMethodInvocation *invocation, GArray *out_param, int result) +{ + FUNC_ENTRY; + GVariant *out_var; + out_var = g_variant_new_from_data((const GVariantType *)"ay", + out_param->data, out_param->len, TRUE, NULL, NULL); + + g_dbus_method_invocation_return_value(invocation, + g_variant_new("(iv)", result, out_var)); + FUNC_EXIT; +} diff --git a/uafw-daemon/src/uafw-manager-event-sender.c b/uafw-daemon/src/uafw-manager-event-sender.c new file mode 100644 index 0000000..a965741 --- /dev/null +++ b/uafw-daemon/src/uafw-manager-event-sender.c @@ -0,0 +1,64 @@ +/* + * User Awareness Manager API + * + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * + * Author: Atul Rai + * + * 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 "uafw-api.h" +#include "uafw-internal.h" + +#include "uafw-manager-common.h" + +int _uafw_manager_send_event( + const char* dest, int event, GVariant *param) +{ + FUNC_ENTRY; + char *signal; + GError *error = NULL; + GDBusConnection *conn; + + conn = _uafw_manager_get_gdbus_conn(); + retv_if(conn == NULL, UAFW_ERROR_INTERNAL); + + switch (event) { + case UA_EVENT_USER_PRESENCE_DETECTED: + signal = UAFW_SIGNAL_USER_PRESENCE_DETECTED; + break; + default: + UA_ERR("Unhandled event"); + return UAFW_ERROR_INTERNAL; + } + + UA_INFO_C("Destination: [%s], event: [%s], signal: [%s]", + dest, _uafw_manager_event_to_str(event), signal); + + if (!g_dbus_connection_emit_signal(conn, dest, UAFW_EVENT_PATH, + UAFW_EVENT_INTERFACE, signal, param, &error)) { + UA_ERR("Error while sending Signal: %s", signal); + if (error) { + UA_ERR("Error Code [%d], Error Message [%s]", + error->code, error->message); + g_clear_error(&error); + } + + return UAFW_ERROR_INTERNAL; + } + + FUNC_EXIT; + return UAFW_ERROR_NONE; +} diff --git a/uafw-daemon/src/uafw-manager-main.c b/uafw-daemon/src/uafw-manager-main.c index 6cd700a..2d670f3 100644 --- a/uafw-daemon/src/uafw-manager-main.c +++ b/uafw-daemon/src/uafw-manager-main.c @@ -19,7 +19,77 @@ * */ +#include "uafw-internal.h" +#include "uafw-manager-common.h" + +static GMainLoop *main_loop; +static gboolean is_initialized = FALSE; + +static void __uafw_signal_handler( + int signo, siginfo_t *info, void *data) +{ + UA_WARN("signal [%d] is sent by [%d]", signo, info->si_pid); +} + +static void __uafw_glib_log( + const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *msg, + gpointer user_data) +{ + UA_ERR("[GLib Err] %s-0x%2.2X: %s", + log_domain, log_level, msg); +} + +static int __uafw_manager_init(void) +{ + FUNC_ENTRY; + int result; + + result = _uafw_manager_register(); + retv_if(UAFW_ERROR_NONE != result, result); + + FUNC_EXIT; + return result; +} + +static void __uafw_manager_cleanup(void) +{ + FUNC_ENTRY; + + FUNC_EXIT; +} + int main(void) { + FUNC_ENTRY; + struct sigaction sa; + + memset(&sa, 0, sizeof(sa)); + sa.sa_sigaction = __uafw_signal_handler; + sa.sa_flags = SA_SIGINFO; + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + + UA_INFO_C("### Starting the uafw-manager daemon"); + + retv_if(TRUE == is_initialized, 0); + + if (UAFW_ERROR_NONE != __uafw_manager_init()) + return -1; + + g_log_set_default_handler(__uafw_glib_log, NULL); + + main_loop = g_main_loop_new(NULL, FALSE); + + g_main_loop_run(main_loop); + + UA_INFO_C("g_main_loop_quit called!"); + if (main_loop != NULL) + g_main_loop_unref(main_loop); + + __uafw_manager_cleanup(); + + FUNC_EXIT; return 0; } diff --git a/uafw-daemon/src/uafw-manager-request-handler.c b/uafw-daemon/src/uafw-manager-request-handler.c new file mode 100644 index 0000000..552cab4 --- /dev/null +++ b/uafw-daemon/src/uafw-manager-request-handler.c @@ -0,0 +1,439 @@ +/* + * User Awareness Manager API + * + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * + * Author: Atul Rai + * + * 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 "uafw-internal.h" +#include "uafw-manager-common.h" + +/* For maintaining Application Sync API call requests */ +GSList *request_list = NULL; + +static GDBusConnection *uafw_manager_conn; +static guint gdbus_object_id = 0; +static guint owner_id = 0; +static guint owner_sig_id = 0; + +static const gchar uafw_manager_introspection_xml[] = +"" +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +""; + +static int __uafw_manager_sync_request_handler( + GDBusMethodInvocation *context, + int function, + GVariant *in_param1, + GVariant *in_param2, + GVariant *in_param3, + GVariant *in_param4, + GArray **out_param1) +{ + FUNC_ENTRY; + int result = UAFW_ERROR_NONE; + + switch (function) { + case UAFW_REQUEST_GET_PAIRING_REQUIRED: { + break; + } + default: + UA_WARN("UnSupported function [%s(0x%4.4X)]", + _uafw_manager_request_to_str(function), function); + result = UAFW_ERROR_NOT_SUPPORTED; + break; + } + + FUNC_EXIT; + return result; +} + +static void __uafw_manager_save_request_context( + GDBusMethodInvocation *context, int result, + char *sender, int function, gpointer data) +{ + FUNC_ENTRY; + uafw_request_context_t *info; + + UA_DBG("Save the request context for [%s(0x%4.4X)]", + _uafw_manager_request_to_str(function), function); + + info = g_malloc0(sizeof(uafw_request_context_t)); + info->context = context; + info->result = result; + info->sender = g_strdup(sender); + info->function = function; + info->data = data; + request_list = g_slist_append(request_list, info); + + FUNC_EXIT; +} + +static int __uafw_manager_async_request_handler( + GDBusMethodInvocation *context, + int function, + GVariant *in_param1, + GVariant *in_param2, + GVariant *in_param3, + GVariant *in_param4, + GArray **out_param1) +{ + FUNC_ENTRY; + int result = UAFW_ERROR_NONE; + char *sender = NULL; + + switch (function) { + case UAFW_REQUEST_START_PRESENCE_DETECTION: { + /* TODO: Extract input params and invoke appropriate function here */ + + if (UAFW_ERROR_NONE == result) { + /* Save request context, which will be used to reply later */ + sender = (char*)g_dbus_method_invocation_get_sender(context); + __uafw_manager_save_request_context(context, result, sender, + function, NULL/**/); + } + break; + } + default: + UA_WARN("UnSupported function [%s(0x%4.4X)]", + _uafw_manager_request_to_str(function), function); + result = UAFW_ERROR_NOT_SUPPORTED; + break; + } + + FUNC_EXIT; + return result; +} + +static gboolean __uafw_manager_is_sync_function(int function) +{ + switch (function) { + case UAFW_REQUEST_START_PRESENCE_DETECTION: + case UAFW_REQUEST_STOP_PRESENCE_DETECTION: + case UAFW_REQUEST_SET_AMBIENT_MODE: + case UAFW_REQUEST_SET_DEVICE_POWER_MODE: + return FALSE; + default: + return TRUE; + } +} + +static void __uafw_manager_method( + GDBusConnection *connection, + const gchar *sender, + const gchar *object_path, + const gchar *interface_name, + const gchar *method_name, + GVariant *parameters, + GDBusMethodInvocation *invocation, + gpointer user_data) +{ + FUNC_ENTRY; + + UA_DBG("Method[%s] Path[%s] Interface[%s]", + method_name, object_path, interface_name); + + if (0 == g_strcmp0(method_name, "uafw_request")) { + int function; + + GVariant *in_param1 = NULL; + GVariant *in_param2 = NULL; + GVariant *in_param3 = NULL; + GVariant *in_param4 = NULL; + GArray *out_param1 = NULL; + int result = 0; + + g_variant_get(parameters, "(i@ay@ay@ay@ay)", &function, + &in_param1, &in_param2, &in_param3, &in_param4); + + out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar)); + + UA_DBG("Request function = [%s (0x%x)]", + _uafw_manager_request_to_str(function), function); + + if (__uafw_manager_is_sync_function(function)) { + result = __uafw_manager_sync_request_handler(invocation, function, + in_param1, in_param2, in_param3, in_param4, &out_param1); + } else { + result = __uafw_manager_async_request_handler(invocation, function, + in_param1, in_param2, in_param3, in_param4, &out_param1); + if (UAFW_ERROR_NONE == result) { + UA_DBG("Request context is saved, do not send reply over dbus"); + goto done; + } + } + + if (UAFW_ERROR_NONE != result) { + UA_ERR("Request [%s (0x%4.4X)] is failed with [%s (0x%4.4X])", + _uafw_manager_request_to_str(function), function, + _uafw_manager_error_to_str(result), result); + } + + _uafw_manager_method_return(invocation, out_param1, result); + +done: + g_variant_unref(in_param1); + g_variant_unref(in_param2); + g_variant_unref(in_param3); + g_variant_unref(in_param4); + } + + FUNC_EXIT; + return; +} + +static const GDBusInterfaceVTable uafw_method_table = { + __uafw_manager_method, + NULL, + NULL, + {0} +}; + +static GDBusNodeInfo *__uafw_manager_create_method_node_info( + const gchar *introspection_data) +{ + FUNC_ENTRY; + GError *err = NULL; + GDBusNodeInfo *node_info = NULL; + + if (introspection_data == NULL) { + UA_ERR("Introspection XML not present"); + return NULL; + } + + node_info = g_dbus_node_info_new_for_xml(introspection_data, &err); + if (err) { + UA_ERR("Unable to create node: %s", err->message); + g_clear_error(&err); + } + + FUNC_EXIT; + return node_info; +} + +static int __uafw_manager_register_object( + GDBusConnection *conn, GDBusNodeInfo *node_info) +{ + FUNC_ENTRY; + GError *error = NULL; + + retv_if(NULL == node_info, UAFW_ERROR_INTERNAL); + + gdbus_object_id = g_dbus_connection_register_object(conn, + UAFW_DAEMON_PATH, + node_info->interfaces[0], + &uafw_method_table, + NULL, NULL, &error); + retv_if(0 == gdbus_object_id, UAFW_ERROR_INTERNAL); + + FUNC_EXIT; + return UAFW_ERROR_NONE; +} + +static int __uafw_manager_unregister_object(GDBusConnection *conn) +{ + FUNC_ENTRY; + + if (gdbus_object_id > 0) { + g_dbus_connection_unregister_object( + conn, gdbus_object_id); + gdbus_object_id = 0; + } + + FUNC_EXIT; + return UAFW_ERROR_NONE; +} + +static void __uafw_manager_cleanup_requests_from_sender(const char *name) +{ + FUNC_ENTRY; + GSList *l; + + ret_if(NULL == name); + + for (l = request_list; l != NULL;) { + uafw_request_context_t *info = l->data; + l = g_slist_next(l); + if (NULL == info || NULL == info->sender) + continue; + + if ((strcasecmp(info->sender, name) == 0)) { + request_list = g_slist_remove(request_list, info); + g_free(info->sender); + g_free(info); + } + } + FUNC_EXIT; +} + +static void __uafw_manager_name_owner_changed_cb(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + const char *name = NULL; + const char *old_owner = NULL; + const char *new_owner = NULL; + + g_variant_get(parameters, "(&s&s&s)", &name, &old_owner, &new_owner); + if (*new_owner != '\0') + return; + + /* Cleanup all pending request from this sender */ + __uafw_manager_cleanup_requests_from_sender(name); +} + +static void __uafw_manager_name_acquired_cb( + GDBusConnection *connection, const gchar *name, gpointer user_data) +{ + UA_INFO("DBus name [%s] acquired", name); +} + +static void __uafw_manager_name_lost_cb( + GDBusConnection *connection, const gchar *name, gpointer user_data) +{ + if (NULL == connection) { + UA_ERR("connection to the bus can't be made"); + /*TODO: Terminate daemon */ + return; + } + + UA_ERR("DBus name [%s] lost", name); + /*TODO: Terminate daemon */ +} + +static void __uafw_manager_bus_acquired_cb( + GDBusConnection *connection, const gchar *name, gpointer user_data) +{ + FUNC_ENTRY; + GDBusNodeInfo *node_info = NULL; + + UA_INFO("DBus bus acquired"); + + ret_if(connection == NULL); + + node_info = __uafw_manager_create_method_node_info( + uafw_manager_introspection_xml); + ret_if(node_info == NULL); + + __uafw_manager_register_object(connection, node_info); + g_dbus_node_info_unref(node_info); + + /* Subscribe for name owner changed signal */ + owner_sig_id = g_dbus_connection_signal_subscribe(connection, + UAFW_SERVICE_DBUS, UAFW_INTERFACE_DBUS, + NAME_OWNER_CHANGED, NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE, + __uafw_manager_name_owner_changed_cb, NULL, NULL); + + UA_DBG("owner_sig_id: [%d]", owner_sig_id); + uafw_manager_conn = connection; + FUNC_EXIT; +} + +int _uafw_manager_register(void) +{ + FUNC_ENTRY; + GDBusConnection *conn; + + conn = _uafw_manager_get_gdbus_conn(); + retv_if(NULL == conn, UAFW_ERROR_INTERNAL); + + owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, + UAFW_DBUS_NAME, + G_BUS_NAME_OWNER_FLAGS_NONE, + __uafw_manager_bus_acquired_cb, + __uafw_manager_name_acquired_cb, + __uafw_manager_name_lost_cb, + NULL, NULL); + retv_if(0 == owner_id, UAFW_ERROR_INTERNAL); + + UA_DBG("owner_id is [%d]", owner_sig_id); + + FUNC_EXIT; + return UAFW_ERROR_NONE; +} + +void _uafw_manager_unregister(void) +{ + FUNC_ENTRY; + + if (uafw_manager_conn) { + if (owner_sig_id > 0) { + g_dbus_connection_signal_unsubscribe( + uafw_manager_conn, owner_sig_id); + owner_sig_id = 0; + } + + __uafw_manager_unregister_object(uafw_manager_conn); + + if (uafw_manager_conn) { + g_object_unref(uafw_manager_conn); + uafw_manager_conn = NULL; + } + + if (owner_id > 0) { + g_bus_unown_name(owner_id); + owner_id = 0; + } + } + + FUNC_EXIT; +} + +GSList *_uafw_manager_get_request_list(void) +{ + return request_list; +} + +void _uafw_manager_remove_req_ctxt_from_list(uafw_request_context_t *req_info) +{ + FUNC_ENTRY; + GSList *l; + + ret_if(NULL == req_info); + ret_if(NULL == req_info->sender); + + for (l = request_list; l != NULL; l = g_slist_next(l)) { + uafw_request_context_t *info = l->data; + if (NULL == info || NULL == info->sender) + continue; + + /* No two same sync requests from same application can exist */ + if ((strcasecmp(info->sender, req_info->sender) == 0) && + req_info->function == info->function) { + request_list = g_slist_remove(request_list, req_info); + g_free(req_info->sender); + g_free(req_info); + break; + } + } + FUNC_EXIT; +} -- 2.7.4