154f6c123b582f1a0ee916143ebafbf3ac814008
[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         default:
186                 _ERR("Unknown Event");
187                 break;
188         }
189
190         _END();
191 }
192 /* LCOV_EXCL_STOP */
193
194 static int _enabler_proxy_init(mdg_manager_s *handle)
195 {
196         GError *error = NULL;
197         const gchar *name;
198         int ret;
199
200         handle->enabler_proxy = enabler_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
201                                                    G_DBUS_PROXY_FLAGS_NONE, MDG_DBUS_SERVICE,
202                                                    MDG_DBUS_ENABLER_PATH, NULL, &error);
203         if (NULL == handle->enabler_proxy) {
204                 /* LCOV_EXCL_START */
205                 if (error != NULL) {
206                         _ERR("Failed to connect to the D-BUS daemon [%s]", error->message);
207                         g_error_free(error);
208                 }
209                 return MDG_ERROR_IO_ERROR;
210                 /* LCOV_EXCL_STOP */
211         }
212
213         handle->system_bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
214         if (NULL == handle->system_bus) {
215                 /* LCOV_EXCL_START */
216                 if (error != NULL) {
217                         _ERR("Failed to connect to system bus [%s]", error->message);
218                         g_error_free(error);
219                 }
220                 return MDG_ERROR_IO_ERROR;
221                 /* LCOV_EXCL_STOP */
222         }
223
224         name = g_dbus_connection_get_unique_name(handle->system_bus);
225         _DBG("Unique dbus name %s", name);
226
227         enabler_call_add_ref_sync(handle->enabler_proxy, name, &ret, NULL, &error);
228         if (error) {
229                 /* LCOV_EXCL_START */
230                 _ERR("Failed to add reference [%s]", error->message);
231                 g_error_free(error);
232                 return MDG_ERROR_IO_ERROR;
233                 /* LCOV_EXCL_STOP */
234         }
235
236         return ret;
237 }
238
239 /* LCOV_EXCL_START */
240 static void _dbus_name_owner_notify(GObject *object, GParamSpec *pspec,
241                 gpointer *user_data)
242 {
243         GDBusProxy *proxy = G_DBUS_PROXY(object);
244         gchar *name_owner = g_dbus_proxy_get_name_owner(proxy);
245         mdg_manager_s *handle = (mdg_manager_s *)user_data;
246         mdg_check_null_ret("user_data", user_data);
247
248         LOGD("Name owner notify [%s]", name_owner);
249
250         if (NULL == name_owner)
251                 gdbus_deinitialize(handle);
252         free(name_owner);
253
254 }
255 /* LCOV_EXCL_STOP */
256
257 static int _group_proxy_init(mdg_manager_s *handle)
258 {
259         int id = -1;
260         GError *error = NULL;
261
262         handle->group_proxy = group_proxy_new_for_bus_sync(
263                                         G_BUS_TYPE_SYSTEM,
264                                         G_DBUS_PROXY_FLAGS_NONE,
265                                         MDG_DBUS_SERVICE,
266                                         MDG_DBUS_GROUP_PATH,
267                                         NULL,
268                                         &error);
269         if (NULL == handle->group_proxy) {
270                 /* LCOV_EXCL_START */
271                 if (error != NULL) {
272                         _ERR("Failed to connect to the D-BUS daemon [%s]", error->message);
273                         g_error_free(error);
274                 }
275                 return MDG_ERROR_IO_ERROR;
276                 /* LCOV_EXCL_STOP */
277         }
278
279         id = g_signal_connect(handle->group_proxy, "notify::g-name-owner",
280                         G_CALLBACK(_dbus_name_owner_notify), handle);
281         if (0 == id) {
282                 /* LCOV_EXCL_START */
283                 _ERR("g_signal_connect() Fail");
284                 g_object_unref(handle->group_proxy);
285                 handle->group_proxy = NULL;
286                 return MDG_ERROR_IO_ERROR;
287                 /* LCOV_EXCL_STOP */
288         }
289
290         g_signal_connect(handle->group_proxy,
291                 "event", G_CALLBACK(__event_cb), handle);
292
293         return MDG_ERROR_NONE;
294 }
295
296
297 static void _group_proxy_deinit(mdg_manager_s *handle)
298 {
299         g_object_unref(handle->group_proxy);
300         handle->group_proxy = NULL;
301 }
302
303 static void _enabler_proxy_deinit(mdg_manager_s *handle)
304 {
305         g_object_unref(handle->system_bus);
306         handle->system_bus = NULL;
307
308         g_object_unref(handle->enabler_proxy);
309         handle->enabler_proxy = NULL;
310 }
311
312 int gdbus_initialize(mdg_manager_s *handle)
313 {
314         int ret = MDG_ERROR_NONE;
315
316         _group_proxy_init(handle);
317         _enabler_proxy_init(handle);
318
319         if (handle->group_proxy == NULL)
320                 ret = -1; /* LCOV_EXCL_LINE */
321
322         if (handle->enabler_proxy == NULL)
323                 ret = -1; /* LCOV_EXCL_LINE */
324
325         return ret;
326 }
327
328 int gdbus_deinitialize(mdg_manager_s *handle)
329 {
330         int ret = MDG_ERROR_NONE;
331
332         _group_proxy_deinit(handle);
333         _enabler_proxy_deinit(handle);
334
335         return ret;
336 }
337