Remove address information
[platform/core/api/multi-device-group.git] / src / mdg_dbus.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
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
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <glib.h>
21 #include <gio/gio.h>
22 #include <stdlib.h>
23
24 #include <mdg.h>
25 #include <mdg_util.h>
26 #include <mdg_dbus.h>
27 #include <mdg_debug.h>
28 #include <mdg_gdbus.h>
29 #include <mdg_private.h>
30
31 /* LCOV_EXCL_START */
32 static void __event_cb(Group *object,
33         gint event_type, gint ret, GVariant *va, gpointer user_data)
34 {
35         NOTUSED(object);
36         mdg_check_null_ret("user_data", user_data);
37
38         mdg_manager_s *handle = user_data;
39
40         _BEGIN();
41
42         _DBG("Event occured : %d", event_type);
43
44         switch (event_type) {
45         case MDG_EVENT_GROUP_FOUND:
46         {
47                 mdg_group_s *group = NULL;
48
49                 group = mdg_get_group_from_variant(va);
50                 if (handle->group_found_cb.found_cb) {
51                         handle->group_found_cb.found_cb(group->type, group,
52                                 handle->group_found_cb.user_data);
53                 } else {
54                         _ERR("The callback not exists");
55                 }
56                 break;
57         }
58         case MDG_EVENT_GROUP_FIND_FINISH:
59         {
60                 if (handle->group_find_finish_cb.finish_cb) {
61                         handle->group_find_finish_cb.finish_cb(ret,
62                                 handle->group_find_finish_cb.user_data);
63                 } else {
64                         _ERR("The callback not exists");
65                 }
66                 break;
67         }
68         case MDG_EVENT_DEVICE_FOUND:
69         {
70                 mdg_device_s *device = NULL;
71
72                 device = mdg_get_device_from_variant(va);
73                 if (handle->device_found_cb.found_cb) {
74                         handle->device_found_cb.found_cb(device,
75                                 handle->device_found_cb.user_data);
76                 } else {
77                         _ERR("The callback not exists");
78                 }
79                 break;
80         }
81         case MDG_EVENT_DEVICE_FIND_FINISH:
82         {
83                 if (handle->device_find_finish_cb.finish_cb) {
84                         handle->device_find_finish_cb.finish_cb(ret,
85                                 handle->device_find_finish_cb.user_data);
86                 } else {
87                         _ERR("The callback not exists");
88                 }
89                 break;
90         }
91         case MDG_EVENT_INVITE_DEVICE_FINISH:
92         {
93                 if (handle->device_invite_finish_cb.finish_cb) {
94                         mdg_device_s *device = (mdg_device_s *)(handle->device_invite_finish_cb.device);
95                         if (device != NULL) {
96                                 device->is_invited = true;
97                         }
98                         handle->device_invite_finish_cb.finish_cb(ret, device,
99                                 handle->device_invite_finish_cb.user_data);
100                 } else {
101                         _ERR("The callback not exists");
102                 }
103                 break;
104         }
105         case MDG_EVENT_EJECT_DEVICE_FINISH:
106         {
107                 if (handle->device_eject_finish_cb.finish_cb) {
108                         handle->device_eject_finish_cb.finish_cb(ret,
109                                 handle->device_eject_finish_cb.user_data);
110                 } else {
111                         _ERR("The callback not exists");
112                 }
113                 break;
114         }
115         case MDG_EVENT_SEND_DATA_FINISH:
116         {
117                 if (handle->send_data_finish_cb.finish_cb) {
118                         handle->send_data_finish_cb.finish_cb(ret,
119                                 handle->send_data_finish_cb.user_data);
120                 } else {
121                         _ERR("The callback not exists");
122                 }
123                 break;
124         }
125         case MDG_EVENT_REQ_CHANNEL_LIST_FINISH:
126         {
127                 char *device_id;
128                 char *channel_id;
129
130                 mdg_get_channel_from_variant(va, &device_id, &channel_id);
131                 if (handle->request_channel_list_finish_cb.finish_cb) {
132                         handle->request_channel_list_finish_cb.finish_cb(device_id, channel_id,
133                                 handle->request_channel_list_finish_cb.user_data);
134                 } else {
135                         _ERR("The callback not exists");
136                 }
137                 break;
138         }
139         case MDG_EVENT_RECEIVE_DATA:
140         {
141                 char *device_id;
142                 char *channel_id;
143                 unsigned char *data;
144                 int data_len;
145
146                 mdg_get_data_from_variant(va, &device_id, &channel_id, &data, &data_len);
147
148                 channel_cb_s *channel = NULL;
149
150                 GSList *l;
151                 for (l = handle->channel_cb_list; l != NULL; l = l->next) {
152                         channel_cb_s *tmp_channel = (channel_cb_s *)l->data;
153                         if (g_strcmp0(tmp_channel->channel_id, channel_id) == 0)
154                                 channel = tmp_channel;
155                 }
156
157                 if (channel != NULL)
158                                 channel->cb(0, device_id, channel_id, data, data_len, channel->user_data);
159                 break;
160         }
161         case MDG_EVENT_SEND_FILE_PROGRESS:
162         {
163                 if (handle->send_file_cb.progress_cb) {
164                         int percent = 0;
165                         long send_size, total_size;
166                         char *file_path;
167                         mdg_get_progress_from_variant(va, &file_path, &send_size, &total_size, &percent);
168                         handle->send_file_cb.progress_cb(file_path, send_size, total_size, percent,
169                                 handle->send_file_cb.user_data);
170                 } else {
171                         _ERR("The callback not exists");
172                 }
173                 break;
174         }
175         case MDG_EVENT_SEND_FILE_FINISH:
176         {
177                 if (handle->send_file_cb.finish_cb) {
178                         mdg_device_s *device = (mdg_device_s *)(handle->send_file_cb.device);
179                         handle->send_file_cb.finish_cb(ret, device, handle->send_file_cb.user_data);
180                 } else {
181                         _ERR("The callback not exists");
182                 }
183                 break;
184         }
185         case MDG_EVENT_RECEIVE_FILE:
186         {
187                 if (handle->receive_file_cb.receive_cb) {
188                         char *device_id;
189                         char *file_path;
190                         mdg_get_receive_file_from_variant(va, &device_id, &file_path);
191                         handle->receive_file_cb.receive_cb(ret, device_id, file_path,
192                                 handle->receive_file_cb.user_data);
193                 } else {
194                         _ERR("The callback not exists");
195                 }
196         }
197         default:
198                 _ERR("Unknown Event");
199                 break;
200         }
201
202         _END();
203 }
204 /* LCOV_EXCL_STOP */
205
206 static int _enabler_proxy_init(mdg_manager_s *handle)
207 {
208         GError *error = NULL;
209         const gchar *name;
210         int ret;
211
212         handle->enabler_proxy = enabler_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
213                                                    G_DBUS_PROXY_FLAGS_NONE, MDG_DBUS_SERVICE,
214                                                    MDG_DBUS_ENABLER_PATH, NULL, &error);
215         if (NULL == handle->enabler_proxy) {
216                 /* LCOV_EXCL_START */
217                 if (error != NULL) {
218                         _ERR("Failed to connect to the D-BUS daemon [%s]", error->message);
219                         g_error_free(error);
220                 }
221                 return MDG_ERROR_IO_ERROR;
222                 /* LCOV_EXCL_STOP */
223         }
224
225         handle->system_bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
226         if (NULL == handle->system_bus) {
227                 /* LCOV_EXCL_START */
228                 if (error != NULL) {
229                         _ERR("Failed to connect to system bus [%s]", error->message);
230                         g_error_free(error);
231                 }
232                 return MDG_ERROR_IO_ERROR;
233                 /* LCOV_EXCL_STOP */
234         }
235
236         name = g_dbus_connection_get_unique_name(handle->system_bus);
237         _DBG("Unique dbus name %s", name);
238
239         enabler_call_add_ref_sync(handle->enabler_proxy, name, &ret, NULL, &error);
240         if (error) {
241                 /* LCOV_EXCL_START */
242                 _ERR("Failed to add reference [%s]", error->message);
243                 g_error_free(error);
244                 return MDG_ERROR_IO_ERROR;
245                 /* LCOV_EXCL_STOP */
246         }
247
248         return ret;
249 }
250
251 /* LCOV_EXCL_START */
252 static void _dbus_name_owner_notify(GObject *object, GParamSpec *pspec,
253                 gpointer *user_data)
254 {
255         GDBusProxy *proxy = G_DBUS_PROXY(object);
256         gchar *name_owner = g_dbus_proxy_get_name_owner(proxy);
257         mdg_manager_s *handle = (mdg_manager_s *)user_data;
258         mdg_check_null_ret("user_data", user_data);
259
260         LOGD("Name owner notify [%s]", name_owner);
261
262         if (NULL == name_owner)
263                 gdbus_deinitialize(handle);
264         free(name_owner);
265
266 }
267 /* LCOV_EXCL_STOP */
268
269 static int _group_proxy_init(mdg_manager_s *handle)
270 {
271         int id = -1;
272         GError *error = NULL;
273
274         handle->group_proxy = group_proxy_new_for_bus_sync(
275                                         G_BUS_TYPE_SYSTEM,
276                                         G_DBUS_PROXY_FLAGS_NONE,
277                                         MDG_DBUS_SERVICE,
278                                         MDG_DBUS_GROUP_PATH,
279                                         NULL,
280                                         &error);
281         if (NULL == handle->group_proxy) {
282                 /* LCOV_EXCL_START */
283                 if (error != NULL) {
284                         _ERR("Failed to connect to the D-BUS daemon [%s]", error->message);
285                         g_error_free(error);
286                 }
287                 return MDG_ERROR_IO_ERROR;
288                 /* LCOV_EXCL_STOP */
289         }
290
291         id = g_signal_connect(handle->group_proxy, "notify::g-name-owner",
292                         G_CALLBACK(_dbus_name_owner_notify), handle);
293         if (0 == id) {
294                 /* LCOV_EXCL_START */
295                 _ERR("g_signal_connect() Fail");
296                 g_object_unref(handle->group_proxy);
297                 handle->group_proxy = NULL;
298                 return MDG_ERROR_IO_ERROR;
299                 /* LCOV_EXCL_STOP */
300         }
301
302         g_signal_connect(handle->group_proxy,
303                 "event", G_CALLBACK(__event_cb), handle);
304
305         return MDG_ERROR_NONE;
306 }
307
308
309 static void _group_proxy_deinit(mdg_manager_s *handle)
310 {
311         g_object_unref(handle->group_proxy);
312         handle->group_proxy = NULL;
313 }
314
315 static void _enabler_proxy_deinit(mdg_manager_s *handle)
316 {
317         g_object_unref(handle->system_bus);
318         handle->system_bus = NULL;
319
320         g_object_unref(handle->enabler_proxy);
321         handle->enabler_proxy = NULL;
322 }
323
324 int gdbus_initialize(mdg_manager_s *handle)
325 {
326         int ret = MDG_ERROR_NONE;
327
328         _group_proxy_init(handle);
329         _enabler_proxy_init(handle);
330
331         if (handle->group_proxy == NULL)
332                 ret = -1; /* LCOV_EXCL_LINE */
333
334         if (handle->enabler_proxy == NULL)
335                 ret = -1; /* LCOV_EXCL_LINE */
336
337         return ret;
338 }
339
340 int gdbus_deinitialize(mdg_manager_s *handle)
341 {
342         int ret = MDG_ERROR_NONE;
343
344         _group_proxy_deinit(handle);
345         _enabler_proxy_deinit(handle);
346
347         return ret;
348 }
349