/*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the License for the specific language governing permissions and
* limitations under the License.
*
- */\r
-#include <glib.h>\r
-\r
-#include "ma.h"\r
-#include "ma-log.h"\r
-#include "ma-util.h"\r
-#include "ma-service.h"\r
-#include "ma-subowner.h"\r
-#include "ma-service-interface.h"\r
-#include "ma-generated-code.h"\r
-\r
-static NetMa *ma_dbus_object;\r
-static Enabler *ma_activator_dbus_object;\r
-\r
-/* global list to care resource handle for each client */\r
-static GList *ma_dbus_client_list;\r
-static GMutex ma_dbus_client_list_mutex;\r
-\r
-typedef struct _ma_dbus_client_s {\r
- gchar *bus_name;\r
-} ma_dbus_client_s;\r
-\r
-NetMa* ma_dbus_get_object()\r
-{\r
- return ma_dbus_object;\r
-}\r
-\r
-static int _ma_dbus_client_list_cleanup(GList *client_list)\r
-{\r
- ma_dbus_client_s *client;\r
-\r
- ma_check_null_ret_error("client_list", client_list, FALSE);\r
-\r
- client = client_list->data;\r
-\r
- g_free(client->bus_name);\r
- client->bus_name = NULL;\r
- g_free(client);\r
- g_list_free(client_list);\r
-\r
- return MA_ERROR_NONE;\r
-}\r
-\r
-static int _ma_dbus_client_list_compare_bus_name(const void *a, const void *b)\r
-{\r
- const ma_dbus_client_s *client = a;\r
- return g_strcmp0(client->bus_name, b);\r
-}\r
-\r
-static inline GList* _ma_dbus_client_list_find_client(const gchar *owner)\r
-{\r
- return g_list_find_custom(ma_dbus_client_list, owner,\r
- _ma_dbus_client_list_compare_bus_name);\r
-}\r
-\r
-static void _ma_dbus_name_owner_changed_cb(GDBusConnection *conn,\r
- const gchar *sender_name,\r
- const gchar *object_path,\r
- const gchar *interface_name,\r
- const gchar *signal_name,\r
- GVariant *parameters,\r
- gpointer user_data)\r
-{\r
- int ret;\r
- GList *client = NULL;\r
- gchar *name, *old_owner, *new_owner;\r
-\r
- NOTUSED(conn);\r
- NOTUSED(sender_name);\r
- NOTUSED(object_path);\r
- NOTUSED(interface_name);\r
- NOTUSED(signal_name);\r
- NOTUSED(user_data);\r
-\r
- g_variant_get(parameters, "(&s&s&s)", &name, &old_owner, &new_owner);\r
-\r
- if (0 == strlen(new_owner)) {\r
- g_mutex_lock(&ma_dbus_client_list_mutex);\r
- client = _ma_dbus_client_list_find_client(old_owner);\r
- if (client) { /* found bus name in our bus list */\r
- MA_LOGD("bus(%s) stopped", old_owner);\r
- ma_dbus_client_list = g_list_remove_link(ma_dbus_client_list, client);\r
- }\r
- g_mutex_unlock(&ma_dbus_client_list_mutex);\r
-\r
- if (client) {\r
- ret = _ma_dbus_client_list_cleanup(client);\r
- if (MA_ERROR_NONE != ret)\r
- MA_LOGE("_ma_dbus_client_list_cleanup() Fail(%d)", ret);\r
- }\r
- }\r
-}\r
-\r
-static int _ma_dbus_subscribe_name_owner_changed(GDBusConnection *conn)\r
-{\r
- unsigned int id;\r
-\r
- id = g_dbus_connection_signal_subscribe(conn,\r
- "org.freedesktop.DBus", /* bus name */\r
- "org.freedesktop.DBus", /* interface */\r
- "NameOwnerChanged", /* member */\r
- "/org/freedesktop/DBus", /* path */\r
- NULL, /* arg0 */\r
- G_DBUS_SIGNAL_FLAGS_NONE,\r
- _ma_dbus_name_owner_changed_cb,\r
- NULL,\r
- NULL);\r
- if (0 == id) {\r
- MA_LOGE("g_dbus_connection_signal_subscribe() Fail");\r
- return MA_ERROR_IO_ERROR;\r
- }\r
-\r
- return MA_ERROR_NONE;\r
-}\r
-\r
-static gboolean _ma_dbus_handle_get_ownerid(NetMa *object,\r
- GDBusMethodInvocation *invocation,\r
- gpointer user_data)\r
-{\r
- gchar *uuid_str = NULL;\r
- int ret = MA_ERROR_NONE;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- ma_check_null_ret_error("service", service, FALSE);\r
-\r
- ret = ma_request_get_ownerid(service, &uuid_str);\r
- if (MA_ERROR_NONE != ret)\r
- MA_LOGE("Failed to ma_request_get_ownerid = %d", ret);\r
-\r
- net_ma_complete_get_ownerid(object, invocation, uuid_str, ret);\r
-\r
- if (uuid_str)\r
- g_free(uuid_str);\r
-\r
- return TRUE;\r
-}\r
-\r
-static gboolean _ma_dbus_handle_disc_mot_enb_devs(NetMa *object,\r
- GDBusMethodInvocation *invocation,\r
- gint timeout,\r
- gpointer user_data)\r
-{\r
- int ret = MA_ERROR_NONE;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- ma_check_null_ret_error("service", service, FALSE);\r
-\r
- ret = ma_request_disc_mot_enb_devs(service, timeout);\r
- if (MA_ERROR_NONE != ret)\r
- MA_LOGE("Failed to ma_request_disc_mot_enb_devs !");\r
-\r
- net_ma_complete_disc_mot_enb_devs(object, invocation, ret);\r
-\r
- return TRUE;\r
-}\r
-\r
-static gboolean _ma_dbus_handle_disc_mowned_devs(NetMa *object,\r
- GDBusMethodInvocation *invocation,\r
- gint timeout,\r
- gpointer user_data)\r
-{\r
- int ret = MA_ERROR_NONE;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- ma_check_null_ret_error("service", service, FALSE);\r
-\r
-\r
- ret = ma_request_disc_owned_devs(service, timeout);\r
- if (MA_ERROR_NONE != ret)\r
- MA_LOGE("Failed to ma_request_disc_owned_devs !");\r
-\r
- net_ma_complete_disc_mowned_devs(object, invocation, ret);\r
-\r
- return TRUE;\r
-}\r
-\r
-static gboolean _ma_dbus_handle_mot(NetMa *object,\r
- GDBusMethodInvocation *invocation, gchar *arg_target, gchar *arg_pin,\r
- gpointer user_data)\r
-{\r
- int ret = MA_ERROR_NONE;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- ma_check_null_ret_error("service", service, FALSE);\r
-\r
-\r
- ret = ma_request_mot(service, arg_target, arg_pin);\r
- if (MA_ERROR_NONE != ret)\r
- MA_LOGE("Failed to ma_request_disc_moted_devs !");\r
-\r
- net_ma_complete_mot(object, invocation, ret);\r
-\r
- return TRUE;\r
-}\r
-\r
-static gboolean _ma_dbus_handle_acl(NetMa *object,\r
- GDBusMethodInvocation *invocation, gchar *arg_target, gchar *arg_subject,\r
- gchar *arg_uri, gchar *arg_rt, gchar *arg_interface, gint arg_permission,\r
- gpointer user_data)\r
-{\r
- int ret = MA_ERROR_NONE;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- ma_check_null_ret_error("service", service, FALSE);\r
-\r
-\r
- ret = ma_request_prov_acl(service, arg_target, arg_subject, arg_uri, arg_rt,\r
- arg_interface, arg_permission);\r
- if (MA_ERROR_NONE != ret)\r
- MA_LOGE("Failed to ma_request_prov_acl !");\r
-\r
- net_ma_complete_acl(object, invocation, ret);\r
-\r
- return TRUE;\r
-}\r
-\r
-static gboolean _ma_dbus_handle_cred(NetMa *object,\r
- GDBusMethodInvocation *invocation, gchar *arg_dev1, gchar *arg_dev2,\r
- gpointer user_data)\r
-{\r
- int ret = MA_ERROR_NONE;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- ma_check_null_ret_error("service", service, FALSE);\r
-\r
-\r
- ret = ma_request_prov_cred(service, arg_dev1, arg_dev2);\r
- if (MA_ERROR_NONE != ret)\r
- MA_LOGE("Failed to ma_request_prov_cred !");\r
-\r
- net_ma_complete_cred(object, invocation, ret);\r
-\r
- return TRUE;\r
-}\r
-\r
-static gboolean _ma_dbus_handle_pairwise(NetMa *object,\r
- GDBusMethodInvocation *invocation, gchar *target_1,\r
- gchar *subject_1, gchar *uri_1, gchar *rt_1,\r
- gchar *interface_1, int permission_1, gchar *target_2,\r
- gchar *subject_2, gchar *uri_2, gchar *rt_2,\r
- gchar *interface_2, int permission_2, gpointer user_data)\r
-{\r
- int ret = MA_ERROR_NONE;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- ma_check_null_ret_error("service", service, FALSE);\r
-\r
-\r
- ret = ma_request_rsrc_pairwise(service, target_1, subject_1, uri_1, rt_1,\r
- interface_1, permission_1, target_2, subject_2, uri_2,\r
- rt_2, interface_2, permission_2);\r
- if (MA_ERROR_NONE != ret)\r
- MA_LOGE("Failed to ma_request_pairwise = %d", ret);\r
-\r
- net_ma_complete_pairwise(object, invocation, ret);\r
-\r
- return TRUE;\r
-}\r
-\r
-static gboolean _ma_dbus_handle_unlink(NetMa *object,\r
- GDBusMethodInvocation *invocation, gchar *uuid_dev1, gchar *uuid_dev2,\r
- gpointer user_data)\r
-{\r
- int ret = MA_ERROR_NONE;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- ma_check_null_ret_error("service", service, FALSE);\r
-\r
- ret = ma_request_unlink_rsrc(service, uuid_dev1, uuid_dev2);\r
- if (MA_ERROR_NONE != ret)\r
- MA_LOGE("Failed to ma_request_unpair = %d", ret);\r
-\r
- net_ma_complete_unpair(object, invocation, ret);\r
-\r
- return TRUE;\r
-}\r
-\r
-static gboolean _ma_dbus_handle_remove_subowner(NetMa *object,\r
- GDBusMethodInvocation *invocation, gchar *arg_target, gpointer user_data)\r
-{\r
- int ret = MA_ERROR_NONE;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- ma_check_null_ret_error("service", service, FALSE);\r
-\r
-\r
- ret = ma_request_remove_subowner(service, arg_target);\r
- if (MA_ERROR_NONE != ret)\r
- MA_LOGE("Failed to ma_request_remove_subowner !");\r
-\r
- net_ma_complete_remove_subowner(object, invocation, ret);\r
-\r
- return TRUE;\r
-}\r
-\r
-static gboolean _ma_dbus_handle_remove_device(NetMa *object,\r
- GDBusMethodInvocation *invocation, gchar *arg_target, gpointer user_data)\r
-{\r
- int ret = MA_ERROR_NONE;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- ma_check_null_ret_error("service", service, FALSE);\r
-\r
-\r
- ret = ma_request_remove_device(service, arg_target);\r
- if (MA_ERROR_NONE != ret)\r
- MA_LOGE("Failed to ma_request_remove_subowner !");\r
-\r
- net_ma_complete_remove_device(object, invocation, ret);\r
-\r
- return TRUE;\r
-}\r
-\r
-static gboolean _ma_dbus_handle_pair(NetMa *object,\r
- GDBusMethodInvocation *invocation, gchar *pin,\r
- gchar *target_1, gchar *subject_1, gchar *uri_1,\r
- gchar *rt_1, gchar *interface_1, int permission_1,\r
- gchar *target_2, gchar *subject_2, gchar *uri_2,\r
- gchar *rt_2, gchar *interface_2, int permission_2,\r
- gpointer user_data)\r
-{\r
- int ret = MA_ERROR_NONE;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- ma_check_null_ret_error("service", service, FALSE);\r
-\r
-\r
- ret = ma_request_pair(service, pin, target_1, subject_1, uri_1, rt_1,\r
- interface_1, permission_1, target_2, subject_2, uri_2,\r
- rt_2, interface_2, permission_2);\r
- if (MA_ERROR_NONE != ret)\r
- MA_LOGE("Failed to ma_request_pair = %d", ret);\r
-\r
- net_ma_complete_pair(object, invocation, ret);\r
-\r
- return TRUE;\r
-}\r
-\r
-static gboolean _ma_dbus_handle_unpair(NetMa *object,\r
- GDBusMethodInvocation *invocation, gchar *uuid_owner, gchar *uuid_owned,\r
- gpointer user_data)\r
-{\r
- int ret = MA_ERROR_NONE;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- ma_check_null_ret_error("service", service, FALSE);\r
-\r
- ret = ma_request_unpair(service, uuid_owner, uuid_owned);\r
- if (MA_ERROR_NONE != ret)\r
- MA_LOGE("Failed to ma_request_unpair = %d", ret);\r
-\r
- net_ma_complete_unpair(object, invocation, ret);\r
-\r
- return TRUE;\r
-}\r
-\r
-static void _ma_dbus_on_bus_acquired(GDBusConnection *conn, const gchar *name,\r
- gpointer user_data)\r
-{\r
- gboolean ret;\r
- GError *error = NULL;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- NOTUSED(name);\r
-\r
- ma_dbus_object = net_ma_skeleton_new();\r
- if (NULL == ma_dbus_object) {\r
- MA_LOGE("net_ma_skeleton_new() Fail");\r
- return;\r
- }\r
- g_signal_connect(ma_dbus_object, "handle-get-ownerid",\r
- G_CALLBACK(_ma_dbus_handle_get_ownerid), service);\r
- g_signal_connect(ma_dbus_object, "handle-disc-mot-enb-devs",\r
- G_CALLBACK(_ma_dbus_handle_disc_mot_enb_devs), service);\r
- g_signal_connect(ma_dbus_object, "handle-disc-mowned-devs",\r
- G_CALLBACK(_ma_dbus_handle_disc_mowned_devs), service);\r
- g_signal_connect(ma_dbus_object, "handle-mot",\r
- G_CALLBACK(_ma_dbus_handle_mot), service);\r
- g_signal_connect(ma_dbus_object, "handle-pairwise",\r
- G_CALLBACK(_ma_dbus_handle_pairwise), service);\r
- g_signal_connect(ma_dbus_object, "handle-unlink",\r
- G_CALLBACK(_ma_dbus_handle_unlink), service);\r
- g_signal_connect(ma_dbus_object, "handle-remove-subowner",\r
- G_CALLBACK(_ma_dbus_handle_remove_subowner), service);\r
- g_signal_connect(ma_dbus_object, "handle-remove-device",\r
- G_CALLBACK(_ma_dbus_handle_remove_device), service);\r
- g_signal_connect(ma_dbus_object, "handle-acl",\r
- G_CALLBACK(_ma_dbus_handle_acl), service);\r
- g_signal_connect(ma_dbus_object, "handle-cred",\r
- G_CALLBACK(_ma_dbus_handle_cred), service);\r
- g_signal_connect(ma_dbus_object, "handle-pair",\r
- G_CALLBACK(_ma_dbus_handle_pair), service);\r
- g_signal_connect(ma_dbus_object, "handle-unpair",\r
- G_CALLBACK(_ma_dbus_handle_unpair), service);\r
-\r
- ret = g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(ma_dbus_object),\r
- conn, MA_DBUS_OBJPATH, &error);\r
- if (FALSE == ret) {\r
- MA_LOGE("g_dbus_interface_skeleton_export() Fail(%s)", error->message);\r
- g_error_free(error);\r
- }\r
-\r
- ret = _ma_dbus_subscribe_name_owner_changed(conn);\r
- if (MA_ERROR_NONE != ret) {\r
- MA_LOGE("_ma_dbus_subscribe_name_owner_changed() Fail(%d)", ret);\r
- return;\r
- }\r
-}\r
-\r
-static void _ma_dbus_on_name_lost(GDBusConnection *conn, const gchar *name,\r
- gpointer user_data)\r
-{\r
- NOTUSED(conn);\r
- NOTUSED(user_data);\r
-\r
- MA_LOGD("Lost the name %s", name);\r
-}\r
-\r
-static void _ma_dbus_on_name_acquired(GDBusConnection *conn, const gchar *name,\r
- gpointer user_data)\r
-{\r
- NOTUSED(conn);\r
- NOTUSED(user_data);\r
-\r
- MA_LOGD("Acquired the name %s", name);\r
-}\r
-\r
-static gboolean _ma_dbus_handle_enable(Enabler *object,\r
- GDBusMethodInvocation *invocation,\r
- gpointer user_data)\r
-{\r
- int ret = MA_ERROR_NONE;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- __MA_LOG_FUNC_ENTER__;\r
-\r
- /* Do API response first */\r
- enabler_complete_enable(object, invocation, ret);\r
- service->ma_activated = TRUE;\r
-\r
- /* Intialize subowner client */\r
- ret = ma_request_enable(service);\r
- if (MA_ERROR_NONE != ret) {\r
- MA_LOGE("ma_request_enable() Fail(%d)", ret);\r
- return FALSE;\r
- }\r
-\r
- /* Notify d2ds enabled */\r
- net_ma_emit_subowner_enabled(ma_dbus_get_object(), ret);\r
-\r
- __MA_LOG_FUNC_EXIT__;\r
-\r
- return TRUE;\r
-}\r
-\r
-static gboolean _ma_dbus_handle_disable(Enabler *object,\r
- GDBusMethodInvocation *invocation,\r
- gpointer user_data)\r
-{\r
- int ret = MA_ERROR_NONE;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- __MA_LOG_FUNC_ENTER__;\r
-\r
- /* Make response first */\r
- enabler_complete_disable(object, invocation, ret);\r
-\r
- /* De-Intialize subowner client */\r
- ret = ma_request_disable(service);\r
- if (MA_ERROR_NONE != ret) {\r
- MA_LOGE("ma_request_disable() Fail(%d)", ret);\r
- return FALSE;\r
- }\r
-\r
- /* Terminate daemon */\r
- ma_service_exit(service);\r
-\r
- return TRUE;\r
-}\r
-\r
-static void _ma_dbus_on_activator_bus_acquired(GDBusConnection *conn,\r
- const gchar *name, gpointer user_data)\r
-{\r
- gboolean ret;\r
- GError *error = NULL;\r
- ma_service *service = (ma_service *)user_data;\r
-\r
- NOTUSED(name);\r
-\r
- __MA_LOG_FUNC_ENTER__;\r
-\r
- ma_activator_dbus_object = enabler_skeleton_new();\r
- if (NULL == ma_activator_dbus_object) {\r
- MA_LOGE("enabler_skeleton_new() Fail");\r
- return;\r
- }\r
-\r
- g_signal_connect(ma_activator_dbus_object, "handle-enable",\r
- G_CALLBACK(_ma_dbus_handle_enable), service);\r
- g_signal_connect(ma_activator_dbus_object, "handle-disable",\r
- G_CALLBACK(_ma_dbus_handle_disable), service);\r
-\r
- ret = g_dbus_interface_skeleton_export(\r
- G_DBUS_INTERFACE_SKELETON(ma_activator_dbus_object),\r
- conn, MA_DBUS_ENABLER_OBJPATH, &error);\r
- if (FALSE == ret) {\r
- MA_LOGE("g_dbus_interface_skeleton_export() Fail(%s)", error->message);\r
- g_error_free(error);\r
- }\r
-\r
- __MA_LOG_FUNC_EXIT__;\r
-\r
-}\r
-\r
-static gboolean _ma_dbus_interface_init(ma_service *service)\r
-{\r
- guint id;\r
- guint activation_dbus_id;\r
- ma_check_null_ret_error("service", service, FALSE);\r
-\r
- id = g_bus_own_name(G_BUS_TYPE_SYSTEM,\r
- MA_DBUS_INTERFACE,\r
- G_BUS_NAME_OWNER_FLAGS_REPLACE,\r
- _ma_dbus_on_bus_acquired,\r
- _ma_dbus_on_name_acquired,\r
- _ma_dbus_on_name_lost,\r
- service,\r
- NULL);\r
- if (0 == id) {\r
- MA_LOGE("g_bus_own_name() Fail");\r
- return FALSE;\r
- }\r
-\r
- /* Get D-Bus owner to activate subowner service daemon */\r
- activation_dbus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,\r
- MA_DBUS_INTERFACE".enabler",\r
- G_BUS_NAME_OWNER_FLAGS_REPLACE,\r
- _ma_dbus_on_activator_bus_acquired,\r
- NULL,\r
- NULL,\r
- service,\r
- NULL);\r
-\r
- service->dbus_id = id;\r
- service->activation_dbus_id = activation_dbus_id;\r
-\r
- return TRUE;\r
-}\r
-\r
-void ma_notify_mot_env_devs(GVariant *devices)\r
-{\r
- /* Notify d2ds enabled */\r
- net_ma_emit_disc_mot_enb_devs_done(ma_dbus_get_object(), devices);\r
-}\r
-\r
-void ma_notify_mowned_devs(GVariant *devices)\r
-{\r
- /* Notify d2ds enabled */\r
- net_ma_emit_disc_mowned_devs_done(ma_dbus_get_object(), devices);\r
-}\r
-\r
-static void _ma_dbus_deinit(ma_service *service)\r
-{\r
- ma_check_null_ret("service", service);\r
-\r
- g_bus_unown_name(service->dbus_id);\r
- g_bus_unown_name(service->activation_dbus_id);\r
-}\r
-\r
-gboolean ma_service_interface_init(ma_service *service)\r
-{\r
- guint ret;\r
- ma_check_null_ret_error("service", service, FALSE);\r
-\r
- __MA_LOG_FUNC_ENTER__;\r
-\r
- /* Initialize dbus interface */\r
- ret = _ma_dbus_interface_init(service);\r
- if (FALSE == ret) {\r
- MA_LOGE("%s failed!!!", __func__);\r
- return FALSE;\r
- }\r
-\r
- __MA_LOG_FUNC_EXIT__;\r
-\r
- return TRUE;\r
-}\r
-\r
-void ma_service_interface_deinit(ma_service *service)\r
-{\r
- ma_check_null_ret("service", service);\r
-\r
- /* De-initialize dbus interface */\r
- _ma_dbus_deinit(service);\r
- /* De-initialize mot-agent instance */\r
- ma_delete_mot_agent(service);\r
-}\r
-\r
+ */
+#include <glib.h>
+
+#include "ma.h"
+#include "ma-log.h"
+#include "ma-util.h"
+#include "ma-service.h"
+#include "ma-subowner.h"
+#include "ma-service-interface.h"
+#include "ma-generated-code.h"
+
+static NetMa *ma_dbus_object;
+static Enabler *ma_activator_dbus_object;
+
+/* global list to care resource handle for each client */
+static GList *ma_dbus_client_list;
+static GMutex ma_dbus_client_list_mutex;
+
+typedef struct _ma_dbus_client_s {
+ gchar *bus_name;
+} ma_dbus_client_s;
+
+NetMa* ma_dbus_get_object()
+{
+ return ma_dbus_object;
+}
+
+static int _ma_dbus_client_list_cleanup(GList *client_list)
+{
+ ma_dbus_client_s *client;
+
+ ma_check_null_ret_error("client_list", client_list, FALSE);
+
+ client = client_list->data;
+
+ g_free(client->bus_name);
+ client->bus_name = NULL;
+ g_free(client);
+ g_list_free(client_list);
+
+ return MA_ERROR_NONE;
+}
+
+static int _ma_dbus_client_list_compare_bus_name(const void *a, const void *b)
+{
+ const ma_dbus_client_s *client = a;
+ return g_strcmp0(client->bus_name, b);
+}
+
+static inline GList* _ma_dbus_client_list_find_client(const gchar *owner)
+{
+ return g_list_find_custom(ma_dbus_client_list, owner,
+ _ma_dbus_client_list_compare_bus_name);
+}
+
+static void _ma_dbus_name_owner_changed_cb(GDBusConnection *conn,
+ const gchar *sender_name,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *signal_name,
+ GVariant *parameters,
+ gpointer user_data)
+{
+ int ret;
+ GList *client = NULL;
+ gchar *name, *old_owner, *new_owner;
+
+ NOTUSED(conn);
+ NOTUSED(sender_name);
+ NOTUSED(object_path);
+ NOTUSED(interface_name);
+ NOTUSED(signal_name);
+ NOTUSED(user_data);
+
+ g_variant_get(parameters, "(&s&s&s)", &name, &old_owner, &new_owner);
+
+ if (0 == strlen(new_owner)) {
+ g_mutex_lock(&ma_dbus_client_list_mutex);
+ client = _ma_dbus_client_list_find_client(old_owner);
+ if (client) { /* found bus name in our bus list */
+ MA_LOGD("bus(%s) stopped", old_owner);
+ ma_dbus_client_list = g_list_remove_link(ma_dbus_client_list, client);
+ }
+ g_mutex_unlock(&ma_dbus_client_list_mutex);
+
+ if (client) {
+ ret = _ma_dbus_client_list_cleanup(client);
+ if (MA_ERROR_NONE != ret)
+ MA_LOGE("_ma_dbus_client_list_cleanup() Fail(%d)", ret);
+ }
+ }
+}
+
+static int _ma_dbus_subscribe_name_owner_changed(GDBusConnection *conn)
+{
+ unsigned int id;
+
+ id = g_dbus_connection_signal_subscribe(conn,
+ "org.freedesktop.DBus", /* bus name */
+ "org.freedesktop.DBus", /* interface */
+ "NameOwnerChanged", /* member */
+ "/org/freedesktop/DBus", /* path */
+ NULL, /* arg0 */
+ G_DBUS_SIGNAL_FLAGS_NONE,
+ _ma_dbus_name_owner_changed_cb,
+ NULL,
+ NULL);
+ if (0 == id) {
+ MA_LOGE("g_dbus_connection_signal_subscribe() Fail");
+ return MA_ERROR_IO_ERROR;
+ }
+
+ return MA_ERROR_NONE;
+}
+
+static gboolean _ma_dbus_handle_get_ownerid(NetMa *object,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ gchar *uuid_str = NULL;
+ int ret = MA_ERROR_NONE;
+ ma_service *service = (ma_service *)user_data;
+
+ ma_check_null_ret_error("service", service, FALSE);
+
+ ret = ma_request_get_ownerid(service, &uuid_str);
+ if (MA_ERROR_NONE != ret)
+ MA_LOGE("Failed to ma_request_get_ownerid = %d", ret);
+
+ net_ma_complete_get_ownerid(object, invocation, uuid_str, ret);
+
+ if (uuid_str)
+ g_free(uuid_str);
+
+ return TRUE;
+}
+
+static gboolean _ma_dbus_handle_disc_mot_enb_devs(NetMa *object,
+ GDBusMethodInvocation *invocation,
+ gint timeout,
+ gpointer user_data)
+{
+ int ret = MA_ERROR_NONE;
+ ma_service *service = (ma_service *)user_data;
+
+ ma_check_null_ret_error("service", service, FALSE);
+
+ ret = ma_request_disc_mot_enb_devs(service, timeout);
+ if (MA_ERROR_NONE != ret)
+ MA_LOGE("Failed to ma_request_disc_mot_enb_devs !");
+
+ net_ma_complete_disc_mot_enb_devs(object, invocation, ret);
+
+ return TRUE;
+}
+
+static gboolean _ma_dbus_handle_disc_mowned_devs(NetMa *object,
+ GDBusMethodInvocation *invocation,
+ gint timeout,
+ gpointer user_data)
+{
+ int ret = MA_ERROR_NONE;
+ ma_service *service = (ma_service *)user_data;
+
+ ma_check_null_ret_error("service", service, FALSE);
+
+
+ ret = ma_request_disc_owned_devs(service, timeout);
+ if (MA_ERROR_NONE != ret)
+ MA_LOGE("Failed to ma_request_disc_owned_devs !");
+
+ net_ma_complete_disc_mowned_devs(object, invocation, ret);
+
+ return TRUE;
+}
+
+static gboolean _ma_dbus_handle_mot(NetMa *object,
+ GDBusMethodInvocation *invocation, gchar *arg_target, gchar *arg_pin,
+ gpointer user_data)
+{
+ int ret = MA_ERROR_NONE;
+ ma_service *service = (ma_service *)user_data;
+
+ ma_check_null_ret_error("service", service, FALSE);
+
+
+ ret = ma_request_mot(service, arg_target, arg_pin);
+ if (MA_ERROR_NONE != ret)
+ MA_LOGE("Failed to ma_request_disc_moted_devs !");
+
+ net_ma_complete_mot(object, invocation, ret);
+
+ return TRUE;
+}
+
+static gboolean _ma_dbus_handle_acl(NetMa *object,
+ GDBusMethodInvocation *invocation, gchar *arg_target, gchar *arg_subject,
+ gchar *arg_uri, gchar *arg_rt, gchar *arg_interface, gint arg_permission,
+ gpointer user_data)
+{
+ int ret = MA_ERROR_NONE;
+ ma_service *service = (ma_service *)user_data;
+
+ ma_check_null_ret_error("service", service, FALSE);
+
+
+ ret = ma_request_prov_acl(service, arg_target, arg_subject, arg_uri, arg_rt,
+ arg_interface, arg_permission);
+ if (MA_ERROR_NONE != ret)
+ MA_LOGE("Failed to ma_request_prov_acl !");
+
+ net_ma_complete_acl(object, invocation, ret);
+
+ return TRUE;
+}
+
+static gboolean _ma_dbus_handle_cred(NetMa *object,
+ GDBusMethodInvocation *invocation, gchar *arg_dev1, gchar *arg_dev2,
+ gpointer user_data)
+{
+ int ret = MA_ERROR_NONE;
+ ma_service *service = (ma_service *)user_data;
+
+ ma_check_null_ret_error("service", service, FALSE);
+
+
+ ret = ma_request_prov_cred(service, arg_dev1, arg_dev2);
+ if (MA_ERROR_NONE != ret)
+ MA_LOGE("Failed to ma_request_prov_cred !");
+
+ net_ma_complete_cred(object, invocation, ret);
+
+ return TRUE;
+}
+
+static gboolean _ma_dbus_handle_pairwise(NetMa *object,
+ GDBusMethodInvocation *invocation, gchar *target_1,
+ gchar *subject_1, gchar *uri_1, gchar *rt_1,
+ gchar *interface_1, int permission_1, gchar *target_2,
+ gchar *subject_2, gchar *uri_2, gchar *rt_2,
+ gchar *interface_2, int permission_2, gpointer user_data)
+{
+ int ret = MA_ERROR_NONE;
+ ma_service *service = (ma_service *)user_data;
+
+ ma_check_null_ret_error("service", service, FALSE);
+
+
+ ret = ma_request_rsrc_pairwise(service, target_1, subject_1, uri_1, rt_1,
+ interface_1, permission_1, target_2, subject_2, uri_2,
+ rt_2, interface_2, permission_2);
+ if (MA_ERROR_NONE != ret)
+ MA_LOGE("Failed to ma_request_pairwise = %d", ret);
+
+ net_ma_complete_pairwise(object, invocation, ret);
+
+ return TRUE;
+}
+
+static gboolean _ma_dbus_handle_unlink(NetMa *object,
+ GDBusMethodInvocation *invocation, gchar *uuid_dev1, gchar *uuid_dev2,
+ gpointer user_data)
+{
+ int ret = MA_ERROR_NONE;
+ ma_service *service = (ma_service *)user_data;
+
+ ma_check_null_ret_error("service", service, FALSE);
+
+ ret = ma_request_unlink_rsrc(service, uuid_dev1, uuid_dev2);
+ if (MA_ERROR_NONE != ret)
+ MA_LOGE("Failed to ma_request_unpair = %d", ret);
+
+ net_ma_complete_unpair(object, invocation, ret);
+
+ return TRUE;
+}
+
+static gboolean _ma_dbus_handle_remove_subowner(NetMa *object,
+ GDBusMethodInvocation *invocation, gchar *arg_target, gpointer user_data)
+{
+ int ret = MA_ERROR_NONE;
+ ma_service *service = (ma_service *)user_data;
+
+ ma_check_null_ret_error("service", service, FALSE);
+
+
+ ret = ma_request_remove_subowner(service, arg_target);
+ if (MA_ERROR_NONE != ret)
+ MA_LOGE("Failed to ma_request_remove_subowner !");
+
+ net_ma_complete_remove_subowner(object, invocation, ret);
+
+ return TRUE;
+}
+
+static gboolean _ma_dbus_handle_remove_device(NetMa *object,
+ GDBusMethodInvocation *invocation, gchar *arg_target, gpointer user_data)
+{
+ int ret = MA_ERROR_NONE;
+ ma_service *service = (ma_service *)user_data;
+
+ ma_check_null_ret_error("service", service, FALSE);
+
+
+ ret = ma_request_remove_device(service, arg_target);
+ if (MA_ERROR_NONE != ret)
+ MA_LOGE("Failed to ma_request_remove_subowner !");
+
+ net_ma_complete_remove_device(object, invocation, ret);
+
+ return TRUE;
+}
+
+static gboolean _ma_dbus_handle_pair(NetMa *object,
+ GDBusMethodInvocation *invocation, gchar *pin,
+ gchar *target_1, gchar *subject_1, gchar *uri_1,
+ gchar *rt_1, gchar *interface_1, int permission_1,
+ gchar *target_2, gchar *subject_2, gchar *uri_2,
+ gchar *rt_2, gchar *interface_2, int permission_2,
+ gpointer user_data)
+{
+ int ret = MA_ERROR_NONE;
+ ma_service *service = (ma_service *)user_data;
+
+ ma_check_null_ret_error("service", service, FALSE);
+
+
+ ret = ma_request_pair(service, pin, target_1, subject_1, uri_1, rt_1,
+ interface_1, permission_1, target_2, subject_2, uri_2,
+ rt_2, interface_2, permission_2);
+ if (MA_ERROR_NONE != ret)
+ MA_LOGE("Failed to ma_request_pair = %d", ret);
+
+ net_ma_complete_pair(object, invocation, ret);
+
+ return TRUE;
+}
+
+static gboolean _ma_dbus_handle_unpair(NetMa *object,
+ GDBusMethodInvocation *invocation, gchar *uuid_owner, gchar *uuid_owned,
+ gpointer user_data)
+{
+ int ret = MA_ERROR_NONE;
+ ma_service *service = (ma_service *)user_data;
+
+ ma_check_null_ret_error("service", service, FALSE);
+
+ ret = ma_request_unpair(service, uuid_owner, uuid_owned);
+ if (MA_ERROR_NONE != ret)
+ MA_LOGE("Failed to ma_request_unpair = %d", ret);
+
+ net_ma_complete_unpair(object, invocation, ret);
+
+ return TRUE;
+}
+
+static void _ma_dbus_on_bus_acquired(GDBusConnection *conn, const gchar *name,
+ gpointer user_data)
+{
+ gboolean ret;
+ GError *error = NULL;
+ ma_service *service = (ma_service *)user_data;
+
+ NOTUSED(name);
+
+ ma_dbus_object = net_ma_skeleton_new();
+ if (NULL == ma_dbus_object) {
+ MA_LOGE("net_ma_skeleton_new() Fail");
+ return;
+ }
+ g_signal_connect(ma_dbus_object, "handle-get-ownerid",
+ G_CALLBACK(_ma_dbus_handle_get_ownerid), service);
+ g_signal_connect(ma_dbus_object, "handle-disc-mot-enb-devs",
+ G_CALLBACK(_ma_dbus_handle_disc_mot_enb_devs), service);
+ g_signal_connect(ma_dbus_object, "handle-disc-mowned-devs",
+ G_CALLBACK(_ma_dbus_handle_disc_mowned_devs), service);
+ g_signal_connect(ma_dbus_object, "handle-mot",
+ G_CALLBACK(_ma_dbus_handle_mot), service);
+ g_signal_connect(ma_dbus_object, "handle-pairwise",
+ G_CALLBACK(_ma_dbus_handle_pairwise), service);
+ g_signal_connect(ma_dbus_object, "handle-unlink",
+ G_CALLBACK(_ma_dbus_handle_unlink), service);
+ g_signal_connect(ma_dbus_object, "handle-remove-subowner",
+ G_CALLBACK(_ma_dbus_handle_remove_subowner), service);
+ g_signal_connect(ma_dbus_object, "handle-remove-device",
+ G_CALLBACK(_ma_dbus_handle_remove_device), service);
+ g_signal_connect(ma_dbus_object, "handle-acl",
+ G_CALLBACK(_ma_dbus_handle_acl), service);
+ g_signal_connect(ma_dbus_object, "handle-cred",
+ G_CALLBACK(_ma_dbus_handle_cred), service);
+ g_signal_connect(ma_dbus_object, "handle-pair",
+ G_CALLBACK(_ma_dbus_handle_pair), service);
+ g_signal_connect(ma_dbus_object, "handle-unpair",
+ G_CALLBACK(_ma_dbus_handle_unpair), service);
+
+ ret = g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(ma_dbus_object),
+ conn, MA_DBUS_OBJPATH, &error);
+ if (FALSE == ret) {
+ MA_LOGE("g_dbus_interface_skeleton_export() Fail(%s)", error->message);
+ g_error_free(error);
+ }
+
+ ret = _ma_dbus_subscribe_name_owner_changed(conn);
+ if (MA_ERROR_NONE != ret) {
+ MA_LOGE("_ma_dbus_subscribe_name_owner_changed() Fail(%d)", ret);
+ return;
+ }
+}
+
+static void _ma_dbus_on_name_lost(GDBusConnection *conn, const gchar *name,
+ gpointer user_data)
+{
+ NOTUSED(conn);
+ NOTUSED(user_data);
+
+ MA_LOGD("Lost the name %s", name);
+}
+
+static void _ma_dbus_on_name_acquired(GDBusConnection *conn, const gchar *name,
+ gpointer user_data)
+{
+ NOTUSED(conn);
+ NOTUSED(user_data);
+
+ MA_LOGD("Acquired the name %s", name);
+}
+
+static gboolean _ma_dbus_handle_enable(Enabler *object,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ int ret = MA_ERROR_NONE;
+ ma_service *service = (ma_service *)user_data;
+
+ __MA_LOG_FUNC_ENTER__;
+
+ /* Do API response first */
+ enabler_complete_enable(object, invocation, ret);
+ service->ma_activated = TRUE;
+
+ /* Intialize subowner client */
+ ret = ma_request_enable(service);
+ if (MA_ERROR_NONE != ret) {
+ MA_LOGE("ma_request_enable() Fail(%d)", ret);
+ return FALSE;
+ }
+
+ /* Notify d2ds enabled */
+ net_ma_emit_subowner_enabled(ma_dbus_get_object(), ret);
+
+ __MA_LOG_FUNC_EXIT__;
+
+ return TRUE;
+}
+
+static gboolean _ma_dbus_handle_disable(Enabler *object,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ int ret = MA_ERROR_NONE;
+ ma_service *service = (ma_service *)user_data;
+
+ __MA_LOG_FUNC_ENTER__;
+
+ /* Make response first */
+ enabler_complete_disable(object, invocation, ret);
+
+ /* De-Intialize subowner client */
+ ret = ma_request_disable(service);
+ if (MA_ERROR_NONE != ret) {
+ MA_LOGE("ma_request_disable() Fail(%d)", ret);
+ return FALSE;
+ }
+
+ /* Terminate daemon */
+ ma_service_exit(service);
+
+ return TRUE;
+}
+
+static void _ma_dbus_on_activator_bus_acquired(GDBusConnection *conn,
+ const gchar *name, gpointer user_data)
+{
+ gboolean ret;
+ GError *error = NULL;
+ ma_service *service = (ma_service *)user_data;
+
+ NOTUSED(name);
+
+ __MA_LOG_FUNC_ENTER__;
+
+ ma_activator_dbus_object = enabler_skeleton_new();
+ if (NULL == ma_activator_dbus_object) {
+ MA_LOGE("enabler_skeleton_new() Fail");
+ return;
+ }
+
+ g_signal_connect(ma_activator_dbus_object, "handle-enable",
+ G_CALLBACK(_ma_dbus_handle_enable), service);
+ g_signal_connect(ma_activator_dbus_object, "handle-disable",
+ G_CALLBACK(_ma_dbus_handle_disable), service);
+
+ ret = g_dbus_interface_skeleton_export(
+ G_DBUS_INTERFACE_SKELETON(ma_activator_dbus_object),
+ conn, MA_DBUS_ENABLER_OBJPATH, &error);
+ if (FALSE == ret) {
+ MA_LOGE("g_dbus_interface_skeleton_export() Fail(%s)", error->message);
+ g_error_free(error);
+ }
+
+ __MA_LOG_FUNC_EXIT__;
+
+}
+
+static gboolean _ma_dbus_interface_init(ma_service *service)
+{
+ guint id;
+ guint activation_dbus_id;
+ ma_check_null_ret_error("service", service, FALSE);
+
+ id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
+ MA_DBUS_INTERFACE,
+ G_BUS_NAME_OWNER_FLAGS_REPLACE,
+ _ma_dbus_on_bus_acquired,
+ _ma_dbus_on_name_acquired,
+ _ma_dbus_on_name_lost,
+ service,
+ NULL);
+ if (0 == id) {
+ MA_LOGE("g_bus_own_name() Fail");
+ return FALSE;
+ }
+
+ /* Get D-Bus owner to activate subowner service daemon */
+ activation_dbus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
+ MA_DBUS_INTERFACE".enabler",
+ G_BUS_NAME_OWNER_FLAGS_REPLACE,
+ _ma_dbus_on_activator_bus_acquired,
+ NULL,
+ NULL,
+ service,
+ NULL);
+
+ service->dbus_id = id;
+ service->activation_dbus_id = activation_dbus_id;
+
+ return TRUE;
+}
+
+void ma_notify_mot_env_devs(GVariant *devices)
+{
+ /* Notify d2ds enabled */
+ net_ma_emit_disc_mot_enb_devs_done(ma_dbus_get_object(), devices);
+}
+
+void ma_notify_mowned_devs(GVariant *devices)
+{
+ /* Notify d2ds enabled */
+ net_ma_emit_disc_mowned_devs_done(ma_dbus_get_object(), devices);
+}
+
+static void _ma_dbus_deinit(ma_service *service)
+{
+ ma_check_null_ret("service", service);
+
+ g_bus_unown_name(service->dbus_id);
+ g_bus_unown_name(service->activation_dbus_id);
+}
+
+gboolean ma_service_interface_init(ma_service *service)
+{
+ guint ret;
+ ma_check_null_ret_error("service", service, FALSE);
+
+ __MA_LOG_FUNC_ENTER__;
+
+ /* Initialize dbus interface */
+ ret = _ma_dbus_interface_init(service);
+ if (FALSE == ret) {
+ MA_LOGE("%s failed!!!", __func__);
+ return FALSE;
+ }
+
+ __MA_LOG_FUNC_EXIT__;
+
+ return TRUE;
+}
+
+void ma_service_interface_deinit(ma_service *service)
+{
+ ma_check_null_ret("service", service);
+
+ /* De-initialize dbus interface */
+ _ma_dbus_deinit(service);
+ /* De-initialize mot-agent instance */
+ ma_delete_mot_agent(service);
+}
+