2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
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.
27 #include <mdg_debug.h>
28 #include <mdg_gdbus.h>
29 #include <mdg_private.h>
32 static void __event_cb(Group *object,
33 gint event_type, gint ret, GVariant *va, gpointer user_data)
36 mdg_check_null_ret("user_data", user_data);
38 mdg_manager_s *handle = user_data;
42 _DBG("Event occured : %d", event_type);
45 case MDG_EVENT_GROUP_FOUND:
47 mdg_group_s *group = NULL;
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);
54 _ERR("The callback not exists");
58 case MDG_EVENT_GROUP_FIND_FINISH:
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);
64 _ERR("The callback not exists");
68 case MDG_EVENT_DEVICE_FOUND:
70 mdg_device_s *device = NULL;
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);
77 _ERR("The callback not exists");
81 case MDG_EVENT_DEVICE_FIND_FINISH:
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);
87 _ERR("The callback not exists");
91 case MDG_EVENT_INVITE_DEVICE_FINISH:
93 if (handle->device_invite_finish_cb.finish_cb) {
94 mdg_device_s *device = (mdg_device_s *)(handle->device_invite_finish_cb.device);
96 device->is_invited = true;
98 handle->device_invite_finish_cb.finish_cb(ret, device,
99 handle->device_invite_finish_cb.user_data);
101 _ERR("The callback not exists");
105 case MDG_EVENT_EJECT_DEVICE_FINISH:
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);
111 _ERR("The callback not exists");
115 case MDG_EVENT_SEND_DATA_FINISH:
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);
121 _ERR("The callback not exists");
125 case MDG_EVENT_REQ_CHANNEL_LIST_FINISH:
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);
135 _ERR("The callback not exists");
139 case MDG_EVENT_RECEIVE_DATA:
146 mdg_get_data_from_variant(va, &device_id, &channel_id, &data, &data_len);
148 channel_cb_s *channel = NULL;
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;
158 channel->cb(0, device_id, channel_id, data, data_len, channel->user_data);
161 case MDG_EVENT_SEND_FILE_PROGRESS:
163 if (handle->send_file_cb.progress_cb) {
165 long send_size, total_size;
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);
171 _ERR("The callback not exists");
175 case MDG_EVENT_SEND_FILE_FINISH:
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);
181 _ERR("The callback not exists");
185 case MDG_EVENT_RECEIVE_FILE:
187 if (handle->receive_file_cb.receive_cb) {
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);
194 _ERR("The callback not exists");
198 _ERR("Unknown Event");
206 static int _enabler_proxy_init(mdg_manager_s *handle)
208 GError *error = NULL;
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 */
218 _ERR("Failed to connect to the D-BUS daemon [%s]", error->message);
221 return MDG_ERROR_IO_ERROR;
225 handle->system_bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
226 if (NULL == handle->system_bus) {
227 /* LCOV_EXCL_START */
229 _ERR("Failed to connect to system bus [%s]", error->message);
232 return MDG_ERROR_IO_ERROR;
236 name = g_dbus_connection_get_unique_name(handle->system_bus);
237 _DBG("Unique dbus name %s", name);
239 enabler_call_add_ref_sync(handle->enabler_proxy, name, &ret, NULL, &error);
241 /* LCOV_EXCL_START */
242 _ERR("Failed to add reference [%s]", error->message);
244 return MDG_ERROR_IO_ERROR;
251 /* LCOV_EXCL_START */
252 static void _dbus_name_owner_notify(GObject *object, GParamSpec *pspec,
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);
260 LOGD("Name owner notify [%s]", name_owner);
262 if (NULL == name_owner)
263 gdbus_deinitialize(handle);
269 static int _group_proxy_init(mdg_manager_s *handle)
272 GError *error = NULL;
274 handle->group_proxy = group_proxy_new_for_bus_sync(
276 G_DBUS_PROXY_FLAGS_NONE,
281 if (NULL == handle->group_proxy) {
282 /* LCOV_EXCL_START */
284 _ERR("Failed to connect to the D-BUS daemon [%s]", error->message);
287 return MDG_ERROR_IO_ERROR;
291 id = g_signal_connect(handle->group_proxy, "notify::g-name-owner",
292 G_CALLBACK(_dbus_name_owner_notify), handle);
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;
302 g_signal_connect(handle->group_proxy,
303 "event", G_CALLBACK(__event_cb), handle);
305 return MDG_ERROR_NONE;
309 static void _group_proxy_deinit(mdg_manager_s *handle)
311 g_object_unref(handle->group_proxy);
312 handle->group_proxy = NULL;
315 static void _enabler_proxy_deinit(mdg_manager_s *handle)
317 g_object_unref(handle->system_bus);
318 handle->system_bus = NULL;
320 g_object_unref(handle->enabler_proxy);
321 handle->enabler_proxy = NULL;
324 int gdbus_initialize(mdg_manager_s *handle)
326 int ret = MDG_ERROR_NONE;
328 _group_proxy_init(handle);
329 _enabler_proxy_init(handle);
331 if (handle->group_proxy == NULL)
332 ret = -1; /* LCOV_EXCL_LINE */
334 if (handle->enabler_proxy == NULL)
335 ret = -1; /* LCOV_EXCL_LINE */
340 int gdbus_deinitialize(mdg_manager_s *handle)
342 int ret = MDG_ERROR_NONE;
344 _group_proxy_deinit(handle);
345 _enabler_proxy_deinit(handle);