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;
33 int stickerd_server_register(GVariant *parameters, GVariant **reply_body, const gchar *sender, GBusNameAppearedCallback name_appeared_handler,
34 GBusNameVanishedCallback name_vanished_handler, GHashTable **monitoring_hash)
36 GList *sender_list = NULL;
37 const char *bus_name = sender;
38 monitoring_info_s *m_info = NULL;
39 uid_t request_uid = 0;
40 GList *monitoring_list = NULL;
43 return STICKERD_SERVER_ERROR_IO_ERROR;
45 g_variant_get(parameters, "(i)", &request_uid);
46 monitoring_list = (GList *)g_hash_table_lookup(*monitoring_hash, GUINT_TO_POINTER(request_uid));
47 sender_list = g_list_find_custom(monitoring_list, bus_name, (GCompareFunc) strcmp);
49 if (sender_list == NULL) {
50 LOGD("Add a new sender");
51 m_info = (monitoring_info_s *)calloc(1, sizeof(monitoring_info_s));
53 LOGE("Failed to alloc memory");
54 return STICKERD_SERVER_ERROR_IO_ERROR;
57 m_info->bus_name = strdup(bus_name);
58 m_info->uid = request_uid;
59 m_info->watcher_id = g_bus_watch_name_on_connection(
62 G_BUS_NAME_WATCHER_FLAGS_NONE,
63 name_appeared_handler,
64 name_vanished_handler,
67 if (m_info->watcher_id == 0) {
68 LOGE("Failed to get identifier");
69 free(m_info->bus_name);
71 return STICKERD_SERVER_ERROR_IO_ERROR;
74 monitoring_list = g_list_append(monitoring_list, strdup(bus_name));
75 LOGD("sender: %s, len: %d", sender, g_list_length(monitoring_list));
76 if (g_hash_table_lookup(*monitoring_hash, GUINT_TO_POINTER(request_uid)) == NULL)
77 g_hash_table_insert(*monitoring_hash, GUINT_TO_POINTER(request_uid), monitoring_list);
79 LOGW("Sender(%s) already exist", sender);
82 *reply_body = g_variant_new("()");
83 if (*reply_body == NULL) {
86 free(m_info->bus_name);
89 monitoring_list = g_list_remove(monitoring_list, bus_name);
90 LOGE("Failed to make reply");
91 return STICKERD_SERVER_ERROR_OUT_OF_MEMORY;
93 return STICKERD_SERVER_ERROR_NONE;
96 int stickerd_server_register_dbus_interface(char *introspection_xml, GDBusInterfaceVTable interface_vtable)
99 int own_id, registration_id;
100 GError *error = NULL;
101 GDBusNodeInfo *introspection_data = NULL;
103 if (_gdbus_connection == NULL) {
104 _gdbus_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
105 if (_gdbus_connection == NULL) {
107 LOGE("g_bus_get_sync error message = %s", error->message);
110 result = STICKERD_SERVER_ERROR_IO_ERROR;
115 own_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
117 G_BUS_NAME_OWNER_FLAGS_NONE,
123 LOGE("Failed to register bus name");
124 result = STICKERD_SERVER_ERROR_IO_ERROR;
128 LOGD("own_id = %d", own_id);
130 introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, &error);
131 if (introspection_data == NULL) {
132 LOGE("Failed to get GDBusNodeInfo");
133 result = STICKERD_SERVER_ERROR_IO_ERROR;
135 LOGE("g_dbus_node_info_new_for_xml error message = %s", error->message);
141 registration_id = g_dbus_connection_register_object(_gdbus_connection, STICKER_OBJECT_PATH,
142 introspection_data->interfaces[0], &interface_vtable, NULL, NULL, NULL);
143 LOGD("registration_id = %d", registration_id);
144 if (registration_id == 0) {
145 LOGE("Failed to register object");
146 result = STICKERD_SERVER_ERROR_IO_ERROR;
150 return STICKERD_SERVER_ERROR_NONE;
153 if (introspection_data)
154 g_dbus_node_info_unref(introspection_data);
159 int delete_monitoring_list(GHashTable **monitoring_hash, const char *sender, uid_t uid)
161 GList *monitoring_list = NULL;
162 GList *del_list = NULL;
165 monitoring_list = (GList *)g_hash_table_lookup(*monitoring_hash, GUINT_TO_POINTER(uid));
166 if (monitoring_list == NULL) {
167 LOGE("The key(%d) is not found", uid);
168 return STICKERD_SERVER_ERROR_IO_ERROR;
171 monitoring_list = g_list_first(monitoring_list);
172 del_list = g_list_find_custom(monitoring_list, sender, (GCompareFunc) strcmp);
175 LOGD("Delete sender(%s)", sender);
176 bus_name = g_list_nth_data(del_list, 0);
179 monitoring_list = g_list_delete_link(monitoring_list, del_list);
181 if (monitoring_list == NULL) {
182 g_hash_table_steal(*monitoring_hash, GUINT_TO_POINTER(uid));
184 monitoring_list = g_list_first(monitoring_list);
185 g_hash_table_replace(*monitoring_hash, GUINT_TO_POINTER(uid), monitoring_list);
188 return STICKERD_SERVER_ERROR_NONE;
191 int stickerd_send_dbus_message(GVariant *body, const char *dest, char *cmd, CLIENT_LIB lib)
194 char *interface = NULL;
196 if (lib == STICKER_CLIENT_LIB_PROVIDER)
197 interface = STICKER_PROVIDER_INTERFACE_NAME;
198 else if (lib == STICKER_CLIENT_LIB_CONSUMER)
199 interface = STICKER_CONSUMER_INTERFACE_NAME;
201 if (g_variant_is_floating(body))
204 if (!g_dbus_connection_emit_signal(_gdbus_connection,
212 LOGE("Failed to send dbus message : %s", err->message);
216 return STICKERD_SERVER_ERROR_IO_ERROR;
219 LOGD("Send message to client : %s", cmd);
220 return STICKERD_SERVER_ERROR_NONE;