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