2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include "stickerd_dbus.h"
24 #include "stickerd_error.h"
29 #define LOG_TAG "STICKERD_DBUS"
31 static GDBusConnection *_gdbus_connection;
32 extern GMainLoop *main_loop;
34 int stickerd_server_register(GVariant *parameters, GVariant **reply_body, const gchar *sender, GBusNameAppearedCallback name_appeared_handler,
35 GBusNameVanishedCallback name_vanished_handler, GHashTable **monitoring_hash)
37 int ret = STICKERD_SERVER_ERROR_NONE;
38 char *m_info_bus_name = NULL;
39 char *list_bus_name = NULL;
40 monitoring_info_s *m_info = NULL;
41 GList *monitoring_list = NULL;
43 if (reply_body == NULL)
44 return STICKERD_SERVER_ERROR_INVALID_PARAMETER;
46 m_info_bus_name = strdup(sender);
47 list_bus_name = strdup(sender);
48 if (m_info_bus_name == NULL || list_bus_name == NULL) {
49 ret = STICKERD_SERVER_ERROR_IO_ERROR;
53 LOGD("Add a new sender");
54 m_info = (monitoring_info_s *)calloc(1, sizeof(monitoring_info_s));
56 LOGE("Failed to alloc memory");
57 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
61 m_info->bus_name = m_info_bus_name;
62 m_info->watcher_id = g_bus_watch_name_on_connection(
65 G_BUS_NAME_WATCHER_FLAGS_NONE,
66 name_appeared_handler,
67 name_vanished_handler,
70 if (m_info->watcher_id == 0) {
71 LOGE("Failed to get identifier");
72 ret = STICKERD_SERVER_ERROR_OPERATION_FAILED;
76 *reply_body = g_variant_new("(i)", m_info->watcher_id);
77 if (*reply_body == NULL) {
78 LOGE("Failed to make reply");
79 ret = STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
83 LOGD("sender: %s, watcher: %d", sender, m_info->watcher_id);
84 monitoring_list = g_list_append(monitoring_list, list_bus_name);
85 if (g_hash_table_lookup(*monitoring_hash, GUINT_TO_POINTER(m_info->watcher_id)) == NULL)
86 g_hash_table_insert(*monitoring_hash, GUINT_TO_POINTER(m_info->watcher_id), monitoring_list);
88 LOGW("Sender(%s) already exist", sender);
90 return STICKERD_SERVER_ERROR_NONE;
94 free(m_info_bus_name);
107 int stickerd_server_unregister(GVariant *parameters, GVariant **reply_body, const gchar *sender, GHashTable **monitoring_hash)
111 g_variant_get(parameters, "(i)", &watcher_id);
113 if (g_hash_table_lookup(*monitoring_hash, GUINT_TO_POINTER(watcher_id)) != NULL) {
114 g_bus_unwatch_name(watcher_id);
115 delete_monitoring_list(monitoring_hash, sender, watcher_id);
118 return STICKERD_SERVER_ERROR_NONE;
121 int stickerd_server_register_dbus_interface(char *introspection_xml, GDBusInterfaceVTable interface_vtable)
124 int own_id, registration_id;
125 GError *error = NULL;
126 GDBusNodeInfo *introspection_data = NULL;
128 if (_gdbus_connection == NULL) {
129 _gdbus_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
130 if (_gdbus_connection == NULL) {
132 LOGE("g_bus_get_sync error message = %s", error->message);
135 result = STICKERD_SERVER_ERROR_IO_ERROR;
140 own_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
142 G_BUS_NAME_OWNER_FLAGS_NONE,
148 LOGE("Failed to register bus name");
149 result = STICKERD_SERVER_ERROR_IO_ERROR;
153 LOGD("own_id = %d", own_id);
155 introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, &error);
156 if (introspection_data == NULL) {
157 LOGE("Failed to get GDBusNodeInfo");
158 result = STICKERD_SERVER_ERROR_IO_ERROR;
160 LOGE("g_dbus_node_info_new_for_xml error message = %s", error->message);
166 registration_id = g_dbus_connection_register_object(_gdbus_connection, STICKER_OBJECT_PATH,
167 introspection_data->interfaces[0], &interface_vtable, NULL, NULL, NULL);
168 LOGD("registration_id = %d", registration_id);
169 if (registration_id == 0) {
170 LOGE("Failed to register object");
171 result = STICKERD_SERVER_ERROR_IO_ERROR;
175 return STICKERD_SERVER_ERROR_NONE;
178 if (introspection_data)
179 g_dbus_node_info_unref(introspection_data);
184 int delete_monitoring_list(GHashTable **monitoring_hash, const char *sender, int watcher_id)
186 GList *monitoring_list = NULL;
187 GList *del_list = NULL;
190 monitoring_list = (GList *)g_hash_table_lookup(*monitoring_hash, GUINT_TO_POINTER(watcher_id));
191 if (monitoring_list == NULL) {
192 LOGE("The key(%d) is not found", watcher_id);
193 return STICKERD_SERVER_ERROR_IO_ERROR;
196 monitoring_list = g_list_first(monitoring_list);
197 del_list = g_list_find_custom(monitoring_list, sender, (GCompareFunc) strcmp);
200 LOGD("Delete sender(%s)", sender);
201 bus_name = g_list_nth_data(del_list, 0);
204 monitoring_list = g_list_delete_link(monitoring_list, del_list);
206 if (monitoring_list == NULL) {
207 g_hash_table_steal(*monitoring_hash, GUINT_TO_POINTER(watcher_id));
209 monitoring_list = g_list_first(monitoring_list);
210 g_hash_table_replace(*monitoring_hash, GUINT_TO_POINTER(watcher_id), monitoring_list);
213 return STICKERD_SERVER_ERROR_NONE;
216 int stickerd_send_dbus_message(GVariant *body, const char *dest, char *cmd, CLIENT_LIB lib)
219 char *interface = NULL;
221 if (lib == STICKER_CLIENT_LIB_PROVIDER)
222 interface = STICKER_PROVIDER_INTERFACE_NAME;
223 else if (lib == STICKER_CLIENT_LIB_CONSUMER)
224 interface = STICKER_CONSUMER_INTERFACE_NAME;
226 if (g_variant_is_floating(body))
229 if (!g_dbus_connection_emit_signal(_gdbus_connection,
237 LOGE("Failed to send dbus message : %s", err->message);
241 return STICKERD_SERVER_ERROR_IO_ERROR;
244 LOGD("Send message to client : %s", cmd);
245 return STICKERD_SERVER_ERROR_NONE;