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-manager-plugin.h"
22 #include "stc-app-lifecycle.h"
24 static gboolean __stc_manager_gdbus_statistics_init(stc_s *stc)
26 __STC_LOG_FUNC_ENTER__;
30 StcObjectSkeleton *object = NULL;
31 StcStatistics *statistics = NULL;
32 s = g_strdup_printf(STC_DBUS_SERVICE_STATISTICS_PATH);
34 /* Add interface to default object path */
35 object = stc_object_skeleton_new(s);
38 /* Make the newly created object export the interface
39 * net.stc.statistics (note
40 * that @object takes its own reference to @statistics).
43 statistics = stc_statistics_skeleton_new();
44 stc_object_skeleton_set_statistics(object, statistics);
45 g_object_unref(statistics);
47 /* Register for method callbacks as signal callbacks */
49 g_signal_connect(statistics, "handle-init",
50 G_CALLBACK(handle_statistics_init),
53 g_signal_connect(statistics, "handle-get",
54 G_CALLBACK(handle_statistics_get),
57 g_signal_connect(statistics, "handle-get-all",
58 G_CALLBACK(handle_statistics_get_all),
61 g_signal_connect(statistics, "handle-reset",
62 G_CALLBACK(handle_statistics_reset),
65 /* Export the object (@manager takes its own reference to @object) */
66 g_dbus_object_manager_server_export(stc->obj_mgr,
67 G_DBUS_OBJECT_SKELETON(object));
68 g_object_unref(object);
70 stc->statistics_obj = (gpointer)statistics;
72 __STC_LOG_FUNC_EXIT__;
76 static gboolean __stc_manager_gdbus_restriction_init(stc_s *stc)
78 __STC_LOG_FUNC_ENTER__;
82 StcObjectSkeleton *object = NULL;
83 StcRestriction *restriction = NULL;
84 s = g_strdup_printf(STC_DBUS_SERVICE_RESTRICTION_PATH);
86 /* Add interface to default object path */
87 object = stc_object_skeleton_new(s);
90 /* Make the newly created object export the interface
91 * net.stc.restriction (note
92 * that @object takes its own reference to @restriction).
95 restriction = stc_restriction_skeleton_new();
96 stc_object_skeleton_set_restriction(object, restriction);
97 g_object_unref(restriction);
99 /* Register for method callbacks as signal callbacks */
101 g_signal_connect(restriction, "handle-set",
102 G_CALLBACK(handle_restriction_set), stc);
104 g_signal_connect(restriction, "handle-exclude",
105 G_CALLBACK(handle_restriction_exclude), stc);
107 g_signal_connect(restriction, "handle-get",
108 G_CALLBACK(handle_restriction_get), stc);
110 g_signal_connect(restriction, "handle-get-all",
111 G_CALLBACK(handle_restriction_get_all), stc);
113 g_signal_connect(restriction, "handle-get-state",
114 G_CALLBACK(handle_restriction_get_state),
117 g_signal_connect(restriction, "handle-unset",
118 G_CALLBACK(handle_restriction_unset), stc);
120 /* Export the object (@manager takes its own reference to @object) */
121 g_dbus_object_manager_server_export(stc->obj_mgr,
122 G_DBUS_OBJECT_SKELETON(object));
123 g_object_unref(object);
125 stc->restriction_obj = (gpointer)restriction;
127 __STC_LOG_FUNC_EXIT__;
131 static void __stc_manager_gdbus_on_bus_acquired(GDBusConnection *connection,
135 __STC_LOG_FUNC_ENTER__;
136 stc_s* stc = (stc_s*)user_data;
138 stc->obj_mgr = g_dbus_object_manager_server_new("/net/stc");
140 STC_LOGD("path : %s", name);
142 stc->connection = connection;
144 if (__stc_manager_gdbus_statistics_init(stc) == FALSE) {
145 STC_LOGE("Can not signal connect to statistics");
146 /* Deinitialize and quit manager */
149 if (__stc_manager_gdbus_restriction_init(stc) == FALSE) {
150 STC_LOGE("Cannot signal connect to restriction");
151 /* Deinitialize and quit manager */
154 g_dbus_object_manager_server_set_connection(stc->obj_mgr,
157 stc_default_connection_monitor_init(stc);
158 stc_register_state_changed_cb(stc, stc_manager_app_status_changed, NULL);
160 __STC_LOG_FUNC_EXIT__;
163 static void __stc_manager_gdbus_on_name_acquired(GDBusConnection *connection,
167 STC_LOGD("name : %s", name);
170 static void __stc_manager_gdbus_on_name_lost(GDBusConnection *connection,
174 STC_LOGD("name : %s", name);
177 void stc_manager_gdbus_init(gpointer stc_data)
179 __STC_LOG_FUNC_ENTER__;
180 stc_s *stc = (stc_s *)stc_data;
182 stc->gdbus_owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
184 G_BUS_NAME_OWNER_FLAGS_NONE,
185 __stc_manager_gdbus_on_bus_acquired,
186 __stc_manager_gdbus_on_name_acquired,
187 __stc_manager_gdbus_on_name_lost,
191 __STC_LOG_FUNC_EXIT__;
194 void stc_manager_gdbus_deinit(gpointer stc_data)
196 __STC_LOG_FUNC_ENTER__;
197 stc_s *stc = (stc_s *)stc_data;
199 stc_deregister_state_changed_cb(stc);
200 stc_default_connection_monitor_deinit(stc);
202 g_bus_unown_name(stc->gdbus_owner_id);
204 stc->statistics_obj = NULL;
205 stc->restriction_obj = NULL;
206 __STC_LOG_FUNC_EXIT__;
209 GVariant *stc_manager_gdbus_call_sync(GDBusConnection *connection,
210 const char *dest, const char *path,
211 const char *interface_name,
212 const char *method, GVariant *params)
214 GError *error = NULL;
215 GVariant *reply = NULL;
217 if (connection == NULL) {
218 STC_LOGE("Failed to get GDBusconnection");
222 reply = g_dbus_connection_call_sync(connection,
229 G_DBUS_CALL_FLAGS_NONE,
230 (5 * 1000), /* 5 seconds timeout */
236 STC_LOGE("g_dbus_connection_call_sync() failed"
237 "error [%d: %s]", error->code, error->message);
240 STC_LOGE("g_dbus_connection_call_sync() failed");
249 guint stc_manager_gdbus_subscribe_signal(GDBusConnection *connection,
251 const gchar *interface_name,
253 const gchar *object_path,
255 GDBusSignalFlags flags,
256 GDBusSignalCallback callback,
258 GDestroyNotify user_data_free_func)
260 if (connection == NULL) {
261 STC_LOGE("Failed to get GDBusconnection");
265 return g_dbus_connection_signal_subscribe(connection,
271 G_DBUS_SIGNAL_FLAGS_NONE,
274 user_data_free_func);
277 void stc_manager_gdbus_unsubscribe_signal(GDBusConnection *connection,
278 guint subscription_id)
280 if (connection == NULL) {
281 STC_LOGE("Failed to get GDBusconnection");
285 g_dbus_connection_signal_unsubscribe(connection, subscription_id);
288 void stc_manager_gdbus_dict_foreach(GVariantIter *iter, dbus_dict_cb cb,
291 __STC_LOG_FUNC_ENTER__;
294 GVariant *value = NULL;
297 __STC_LOG_FUNC_EXIT__;
301 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
302 DEBUG_GDBUS_KEY_VALUE(key, value);
304 cb(key, value, user_data);
307 __STC_LOG_FUNC_EXIT__;
310 gboolean stc_manager_dbus_emit_signal(GDBusConnection *connection,
311 const gchar *object_path,
312 const gchar *interface_name,
313 const gchar *signal_name,
314 GVariant *parameters)
317 GError *error = NULL;
319 if (connection == NULL) {
320 STC_LOGE("GDBusconnection is NULL");
324 DEBUG_GDBUS_VARIANT("Signal params: ", parameters);
326 rv = g_dbus_connection_emit_signal(connection,
334 STC_LOGE("Failed to emit signal [%s] interface [%s] Error [%s]",
335 signal_name, interface_name, error->message);
338 STC_LOGD("[%s] signal sent on [%s] interface", signal_name,