bf556e6c689043288886ba340cc1f710940dc013
[platform/core/connectivity/stc-manager.git] / src / stc-manager-gdbus.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
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"
23 #include "helper-iptables.h"
24
25 static gboolean __stc_manager_gdbus_statistics_init(stc_s *stc)
26 {
27         __STC_LOG_FUNC_ENTER__;
28         gboolean ret = TRUE;
29         gchar *s = NULL;
30
31         StcObjectSkeleton *object = NULL;
32         StcStatistics *statistics = NULL;
33         s = g_strdup_printf(STC_DBUS_SERVICE_STATISTICS_PATH);
34
35         /* Add interface to default object path */
36         object = stc_object_skeleton_new(s);
37         g_free(s);
38
39         /* Make the newly created object export the interface
40          * net.stc.statistics (note
41          * that @object takes its own reference to @statistics).
42          */
43
44         statistics = stc_statistics_skeleton_new();
45         stc_object_skeleton_set_statistics(object, statistics);
46         g_object_unref(statistics);
47
48         /* Register for method callbacks as signal callbacks */
49
50         g_signal_connect(statistics, "handle-init",
51                          G_CALLBACK(handle_statistics_init),
52                          stc);
53
54         g_signal_connect(statistics, "handle-get",
55                          G_CALLBACK(handle_statistics_get),
56                          stc);
57
58         g_signal_connect(statistics, "handle-get-all",
59                          G_CALLBACK(handle_statistics_get_all),
60                          stc);
61
62         g_signal_connect(statistics, "handle-reset",
63                          G_CALLBACK(handle_statistics_reset),
64                          stc);
65
66         /* Export the object (@manager takes its own reference to @object) */
67         g_dbus_object_manager_server_export(stc->obj_mgr,
68                                             G_DBUS_OBJECT_SKELETON(object));
69         g_object_unref(object);
70
71         stc->statistics_obj = (gpointer)statistics;
72
73         __STC_LOG_FUNC_EXIT__;
74         return ret;
75 }
76
77 static gboolean __stc_manager_gdbus_restriction_init(stc_s *stc)
78 {
79         __STC_LOG_FUNC_ENTER__;
80         gboolean ret = TRUE;
81         gchar *s = NULL;
82
83         StcObjectSkeleton *object = NULL;
84         StcRestriction *restriction = NULL;
85         s = g_strdup_printf(STC_DBUS_SERVICE_RESTRICTION_PATH);
86
87         /* Add interface to default object path */
88         object = stc_object_skeleton_new(s);
89         g_free(s);
90
91         /* Make the newly created object export the interface
92          * net.stc.restriction (note
93          * that @object takes its own reference to @restriction).
94          */
95
96         restriction = stc_restriction_skeleton_new();
97         stc_object_skeleton_set_restriction(object, restriction);
98         g_object_unref(restriction);
99
100         /* Register for method callbacks as signal callbacks */
101
102         g_signal_connect(restriction, "handle-set",
103                          G_CALLBACK(handle_restriction_set), stc);
104
105         g_signal_connect(restriction, "handle-exclude",
106                          G_CALLBACK(handle_restriction_exclude), stc);
107
108         g_signal_connect(restriction, "handle-get",
109                          G_CALLBACK(handle_restriction_get), stc);
110
111         g_signal_connect(restriction, "handle-get-all",
112                          G_CALLBACK(handle_restriction_get_all), stc);
113
114         g_signal_connect(restriction, "handle-get-state",
115                          G_CALLBACK(handle_restriction_get_state),
116                          stc);
117
118         g_signal_connect(restriction, "handle-unset",
119                          G_CALLBACK(handle_restriction_unset), stc);
120
121         /* Export the object (@manager takes its own reference to @object) */
122         g_dbus_object_manager_server_export(stc->obj_mgr,
123                                             G_DBUS_OBJECT_SKELETON(object));
124         g_object_unref(object);
125
126         stc->restriction_obj = (gpointer)restriction;
127
128         __STC_LOG_FUNC_EXIT__;
129         return ret;
130 }
131
132 static void __stc_manager_gdbus_on_bus_acquired(GDBusConnection *connection,
133                                                 const gchar *name,
134                                                 gpointer user_data)
135 {
136         __STC_LOG_FUNC_ENTER__;
137         stc_s* stc = (stc_s*)user_data;
138
139         stc->obj_mgr = g_dbus_object_manager_server_new("/net/stc");
140
141         STC_LOGD("path : %s", name);
142
143         stc->connection = connection;
144
145         if (__stc_manager_gdbus_statistics_init(stc) == FALSE) {
146                 STC_LOGE("Can not signal connect to statistics");
147                 /* Deinitialize and quit manager */
148         }
149
150         if (__stc_manager_gdbus_restriction_init(stc) == FALSE) {
151                 STC_LOGE("Cannot signal connect to restriction");
152                 /* Deinitialize and quit manager */
153         }
154
155         g_dbus_object_manager_server_set_connection(stc->obj_mgr,
156                                                     stc->connection);
157
158         iptables_init();
159         stc_default_connection_monitor_init(stc);
160         stc_register_state_changed_cb(stc, stc_manager_app_status_changed, NULL);
161
162         __STC_LOG_FUNC_EXIT__;
163 }
164
165 static void __stc_manager_gdbus_on_name_acquired(GDBusConnection *connection,
166                                                  const gchar *name,
167                                                  gpointer user_data)
168 {
169         STC_LOGD("name : %s", name);
170 }
171
172 static void __stc_manager_gdbus_on_name_lost(GDBusConnection *connection,
173                                              const gchar *name,
174                                              gpointer user_data)
175 {
176         STC_LOGD("name : %s", name);
177 }
178
179 void stc_manager_gdbus_init(gpointer stc_data)
180 {
181         __STC_LOG_FUNC_ENTER__;
182         stc_s *stc = (stc_s *)stc_data;
183
184         stc->gdbus_owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
185                                              STC_DBUS_SERVICE,
186                                              G_BUS_NAME_OWNER_FLAGS_NONE,
187                                              __stc_manager_gdbus_on_bus_acquired,
188                                              __stc_manager_gdbus_on_name_acquired,
189                                              __stc_manager_gdbus_on_name_lost,
190                                              stc,
191                                              NULL);
192
193         __STC_LOG_FUNC_EXIT__;
194 }
195
196 void stc_manager_gdbus_deinit(gpointer stc_data)
197 {
198         __STC_LOG_FUNC_ENTER__;
199         stc_s *stc = (stc_s *)stc_data;
200
201         stc_deregister_state_changed_cb(stc);
202         stc_default_connection_monitor_deinit(stc);
203
204         g_bus_unown_name(stc->gdbus_owner_id);
205
206         stc->statistics_obj = NULL;
207         stc->restriction_obj = NULL;
208         __STC_LOG_FUNC_EXIT__;
209 }
210
211 GVariant *stc_manager_gdbus_call_sync(GDBusConnection *connection,
212                                       const char *dest, const char *path,
213                                       const char *interface_name,
214                                       const char *method, GVariant *params)
215 {
216         GError *error = NULL;
217         GVariant *reply = NULL;
218
219         if (connection == NULL) {
220                 STC_LOGE("Failed to get GDBusconnection");
221                 return reply;
222         }
223
224         reply = g_dbus_connection_call_sync(connection,
225                                             dest,
226                                             path,
227                                             interface_name,
228                                             method,
229                                             params,
230                                             NULL,
231                                             G_DBUS_CALL_FLAGS_NONE,
232                                             (5 * 1000),  /* 5 seconds timeout */
233                                             NULL,
234                                             &error);
235
236         if (reply == NULL) {
237                 if (error != NULL) {
238                         STC_LOGE("g_dbus_connection_call_sync() failed"
239                                  "error [%d: %s]", error->code, error->message);
240                         g_error_free(error);
241                 } else {
242                         STC_LOGE("g_dbus_connection_call_sync() failed");
243                 }
244
245                 return NULL;
246         }
247
248         return reply;
249 }
250
251 guint stc_manager_gdbus_subscribe_signal(GDBusConnection *connection,
252                                          const gchar *sender,
253                                          const gchar *interface_name,
254                                          const gchar *member,
255                                          const gchar *object_path,
256                                          const gchar *arg0,
257                                          GDBusSignalFlags flags,
258                                          GDBusSignalCallback callback,
259                                          gpointer user_data,
260                                          GDestroyNotify user_data_free_func)
261 {
262         if (connection == NULL) {
263                 STC_LOGE("Failed to get GDBusconnection");
264                 return 0;
265         }
266
267         return g_dbus_connection_signal_subscribe(connection,
268                                                   sender,
269                                                   interface_name,
270                                                   member,
271                                                   object_path,
272                                                   NULL,
273                                                   G_DBUS_SIGNAL_FLAGS_NONE,
274                                                   callback,
275                                                   user_data,
276                                                   user_data_free_func);
277 }
278
279 void stc_manager_gdbus_unsubscribe_signal(GDBusConnection *connection,
280                                           guint subscription_id)
281 {
282         if (connection == NULL) {
283                 STC_LOGE("Failed to get GDBusconnection");
284                 return;
285         }
286
287         g_dbus_connection_signal_unsubscribe(connection, subscription_id);
288 }
289
290 void stc_manager_gdbus_dict_foreach(GVariantIter *iter, dbus_dict_cb cb,
291                                     void *user_data)
292 {
293         __STC_LOG_FUNC_ENTER__;
294
295         gchar *key = NULL;
296         GVariant *value = NULL;
297
298         if (!cb) {
299                 __STC_LOG_FUNC_EXIT__;
300                 return;
301         }
302
303         while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
304                 DEBUG_GDBUS_KEY_VALUE(key, value);
305                 if (key && cb)
306                         cb(key, value, user_data);
307         }
308
309         __STC_LOG_FUNC_EXIT__;
310 }
311
312 gboolean stc_manager_dbus_emit_signal(GDBusConnection *connection,
313                                       const gchar *object_path,
314                                       const gchar *interface_name,
315                                       const gchar *signal_name,
316                                       GVariant *parameters)
317 {
318         gboolean rv = FALSE;
319         GError *error = NULL;
320
321         if (connection == NULL) {
322                 STC_LOGE("GDBusconnection is NULL");
323                 return 0;
324         }
325
326         DEBUG_GDBUS_VARIANT("Signal params: ", parameters);
327
328         rv = g_dbus_connection_emit_signal(connection,
329                                            NULL,
330                                            object_path,
331                                            interface_name,
332                                            signal_name,
333                                            parameters,
334                                            &error);
335         if (rv != TRUE) {
336                 STC_LOGE("Failed to emit signal [%s] interface [%s] Error [%s]",
337                          signal_name, interface_name, error->message);
338                 g_error_free(error);
339         } else {
340                 STC_LOGD("[%s] signal sent on [%s] interface", signal_name,
341                          interface_name);
342         }
343
344         return rv;
345 }