2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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.
17 #include "stc-manager-gdbus.h"
18 #include "stc-statistics.h"
19 #include "stc-restriction.h"
20 #include "stc-default-connection.h"
21 #include "stc-application-lifecycle.h"
23 static gboolean __stc_manager_gdbus_statistics_init(stc_s *stc)
25 __STC_LOG_FUNC_ENTER__;
29 StcObjectSkeleton *object = NULL;
30 StcStatistics *statistics = NULL;
31 s = g_strdup_printf(STC_DBUS_SERVICE_STATISTICS_PATH);
33 /* Add interface to default object path */
34 object = stc_object_skeleton_new(s);
37 /* Make the newly created object export the interface
38 * net.stc.statistics (note
39 * that @object takes its own reference to @statistics).
42 statistics = stc_statistics_skeleton_new();
43 stc_object_skeleton_set_statistics(object, statistics);
44 g_object_unref(statistics);
46 /* Register for method callbacks as signal callbacks */
48 g_signal_connect(statistics, "handle-init",
49 G_CALLBACK(handle_statistics_init),
52 g_signal_connect(statistics, "handle-get",
53 G_CALLBACK(handle_statistics_get),
56 g_signal_connect(statistics, "handle-get-all",
57 G_CALLBACK(handle_statistics_get_all),
60 g_signal_connect(statistics, "handle-reset",
61 G_CALLBACK(handle_statistics_reset),
64 /* Export the object (@manager takes its own reference to @object) */
65 g_dbus_object_manager_server_export(stc->obj_mgr,
66 G_DBUS_OBJECT_SKELETON(object));
67 g_object_unref(object);
69 stc->statistics_obj = (gpointer)statistics;
71 __STC_LOG_FUNC_EXIT__;
75 static gboolean __stc_manager_gdbus_restriction_init(stc_s *stc)
77 __STC_LOG_FUNC_ENTER__;
81 StcObjectSkeleton *object = NULL;
82 StcRestriction *restriction = NULL;
83 s = g_strdup_printf(STC_DBUS_SERVICE_RESTRICTION_PATH);
85 /* Add interface to default object path */
86 object = stc_object_skeleton_new(s);
89 /* Make the newly created object export the interface
90 * net.stc.restriction (note
91 * that @object takes its own reference to @restriction).
94 restriction = stc_restriction_skeleton_new();
95 stc_object_skeleton_set_restriction(object, restriction);
96 g_object_unref(restriction);
98 /* Register for method callbacks as signal callbacks */
100 g_signal_connect(restriction, "handle-set",
101 G_CALLBACK(handle_restriction_set), stc);
103 g_signal_connect(restriction, "handle-exclude",
104 G_CALLBACK(handle_restriction_exclude), stc);
106 g_signal_connect(restriction, "handle-get",
107 G_CALLBACK(handle_restriction_get), stc);
109 g_signal_connect(restriction, "handle-get-all",
110 G_CALLBACK(handle_restriction_get_all), stc);
112 g_signal_connect(restriction, "handle-get-state",
113 G_CALLBACK(handle_restriction_get_state),
116 g_signal_connect(restriction, "handle-remove",
117 G_CALLBACK(handle_restriction_remove), stc);
119 /* Export the object (@manager takes its own reference to @object) */
120 g_dbus_object_manager_server_export(stc->obj_mgr,
121 G_DBUS_OBJECT_SKELETON(object));
122 g_object_unref(object);
124 stc->restriction_obj = (gpointer)restriction;
126 __STC_LOG_FUNC_EXIT__;
130 static void __stc_manager_gdbus_on_bus_acquired(GDBusConnection *connection,
134 __STC_LOG_FUNC_ENTER__;
135 stc_s* stc = (stc_s*)user_data;
137 stc->obj_mgr = g_dbus_object_manager_server_new("/net/stc");
139 STC_LOGD("path : %s", name);
141 stc->connection = connection;
143 if (__stc_manager_gdbus_statistics_init(stc) == FALSE) {
144 STC_LOGE("Can not signal connect to statistics");
145 /* Deinitialize and quit manager */
148 if (__stc_manager_gdbus_restriction_init(stc) == FALSE) {
149 STC_LOGE("Cannot signal connect to restriction");
150 /* Deinitialize and quit manager */
153 g_dbus_object_manager_server_set_connection(stc->obj_mgr,
156 stc_application_lifecycle_monitor_init(stc);
157 stc_default_connection_monitor_init(stc);
159 __STC_LOG_FUNC_EXIT__;
162 static void __stc_manager_gdbus_on_name_acquired(GDBusConnection *connection,
166 STC_LOGD("name : %s", name);
169 static void __stc_manager_gdbus_on_name_lost(GDBusConnection *connection,
173 STC_LOGD("name : %s", name);
176 void stc_manager_gdbus_init(gpointer stc_data)
178 __STC_LOG_FUNC_ENTER__;
179 stc_s *stc = (stc_s *)stc_data;
181 stc->gdbus_owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
183 G_BUS_NAME_OWNER_FLAGS_NONE,
184 __stc_manager_gdbus_on_bus_acquired,
185 __stc_manager_gdbus_on_name_acquired,
186 __stc_manager_gdbus_on_name_lost,
190 __STC_LOG_FUNC_EXIT__;
193 void stc_manager_gdbus_deinit(gpointer stc_data)
195 __STC_LOG_FUNC_ENTER__;
196 stc_s *stc = (stc_s *)stc_data;
197 stc_application_lifecycle_monitor_deinit(stc);
198 stc_default_connection_monitor_deinit(stc);
199 g_bus_unown_name(stc->gdbus_owner_id);
200 stc->statistics_obj = NULL;
201 stc->restriction_obj = NULL;
202 __STC_LOG_FUNC_EXIT__;
205 GVariant *stc_manager_gdbus_call_sync(GDBusConnection *connection,
206 const char *dest, const char *path,
207 const char *interface_name,
208 const char *method, GVariant *params)
210 GError *error = NULL;
211 GVariant *reply = NULL;
213 if (connection == NULL) {
214 STC_LOGE("Failed to get GDBusconnection");
218 reply = g_dbus_connection_call_sync(connection,
225 G_DBUS_CALL_FLAGS_NONE,
226 (5 * 1000), /* 5 seconds timeout */
232 STC_LOGE("g_dbus_connection_call_sync() failed"
233 "error [%d: %s]", error->code, error->message);
236 STC_LOGE("g_dbus_connection_call_sync() failed");
245 guint stc_manager_gdbus_subscribe_signal(GDBusConnection *connection,
247 const gchar *interface_name,
249 const gchar *object_path,
251 GDBusSignalFlags flags,
252 GDBusSignalCallback callback,
254 GDestroyNotify user_data_free_func)
256 if (connection == NULL) {
257 STC_LOGE("Failed to get GDBusconnection");
261 return g_dbus_connection_signal_subscribe(connection,
267 G_DBUS_SIGNAL_FLAGS_NONE,
270 user_data_free_func);
273 void stc_manager_gdbus_unsubscribe_signal(GDBusConnection *connection,
274 guint subscription_id)
276 if (connection == NULL) {
277 STC_LOGE("Failed to get GDBusconnection");
281 g_dbus_connection_signal_unsubscribe(connection, subscription_id);
284 void stc_manager_gdbus_dict_foreach(GVariantIter *iter, dbus_dict_cb cb,
287 __STC_LOG_FUNC_ENTER__;
290 GVariant *value = NULL;
293 __STC_LOG_FUNC_EXIT__;
297 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
298 DEBUG_GDBUS_KEY_VALUE(key, value);
300 cb(key, value, user_data);
303 __STC_LOG_FUNC_EXIT__;
306 gboolean stc_manager_dbus_emit_signal(GDBusConnection *connection,
307 const gchar *object_path,
308 const gchar *interface_name,
309 const gchar *signal_name,
310 GVariant *parameters)
313 GError *error = NULL;
315 if (connection == NULL) {
316 STC_LOGE("GDBusconnection is NULL");
320 DEBUG_GDBUS_VARIANT("Signal params: ", parameters);
322 rv = g_dbus_connection_emit_signal(connection,
330 STC_LOGE("Failed to emit signal [%s] interface [%s] Error [%s]",
331 signal_name, interface_name, error->message);
334 STC_LOGD("[%s] signal sent on [%s] interface", signal_name,