4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <glib-object.h>
23 #include <appcore-efl.h>
24 #include <privilege-control.h>
27 #include "bt-share-main.h"
28 #include "bluetooth-api.h"
29 #include "obex-event-handler.h"
30 #include "bt-share-ipc.h"
31 #include "bt-share-noti-handler.h"
32 #include "bt-share-resource.h"
33 #include "bt-share-notification.h"
34 #include "bt-share-common.h"
36 #include "bluetooth-share-api.h"
38 #define BLUETOOTH_SHARE_BUS "org.projectx.bluetooth.share"
40 static gboolean terminated;
42 GMainLoop *main_loop = NULL;
43 struct bt_appdata *app_state = NULL;
45 static void __bt_release_service(struct bt_appdata *ad)
50 _bt_deinit_vconf_notification();
51 _bt_delete_notification(ad->send_noti);
52 _bt_delete_notification(ad->receive_noti);
53 _bt_delete_notification(ad->opc_noti);
54 _bt_clear_receive_noti_list();
56 ad->receive_noti = NULL;
59 bluetooth_opc_deinit();
60 bluetooth_obex_server_deinit();
61 _bt_unregister_notification_cb(ad);
63 DBG("Terminating bluetooth-share daemon");
66 static void __bt_sigterm_handler(int signo)
71 g_main_loop_quit(main_loop);
79 static void __bt_update_notification_status_values()
81 struct bt_appdata *ad = app_state;
82 GSList *tr_data_list = NULL;
83 GSList *list_iter = NULL;
84 bt_tr_data_t *info = NULL;;
85 char str[NOTIFICATION_TEXT_LEN_MAX] = { 0 };
86 notification_h noti = NULL;
89 db = bt_share_open_db();
93 tr_data_list = bt_share_get_all_tr_data_list(db, BT_DB_OUTBOUND);
94 if (NULL != tr_data_list) {
95 list_iter = tr_data_list;
97 while (NULL != list_iter) {
98 info = list_iter->data;
102 if (info->tr_status == BT_TR_SUCCESS) {
103 ad->send_data.tr_success++;
104 } else if (info->tr_status == BT_TR_FAIL) {
105 ad->send_data.tr_fail++;
106 } else if (info->tr_status == BT_TR_ONGOING) {
107 /* In case of ongoing file transfer if bluetooth is switched off
108 we need to update the status to fail for these transaction */
109 ad->send_data.tr_fail++;
110 info->tr_status = BT_TR_FAIL;
111 bt_share_update_tr_data(db, BT_DB_OUTBOUND, info->id, info);
113 ERR("Invalid status\n");
116 list_iter = g_slist_next(list_iter);
119 if ((ad->send_data.tr_success + ad->send_data.tr_fail) != 0) {
120 snprintf(str, sizeof(str), BT_TR_STATUS,
121 ad->send_data.tr_success, ad->send_data.tr_fail);
123 noti = _bt_create_notification(BT_NOTI_T);
124 _bt_set_notification_app_launch(noti,
126 NOTI_TR_TYPE_OUT, NULL, NULL);
127 _bt_set_notification_property(noti, QP_NO_DELETE | QP_NO_TICKER);
128 _bt_insert_notification(noti,
131 ad->send_noti = noti;
134 bt_share_release_tr_data_list(tr_data_list);
139 tr_data_list = bt_share_get_all_tr_data_list(db, BT_DB_INBOUND);
140 if (NULL != tr_data_list) {
141 list_iter = tr_data_list;
143 while (NULL != list_iter) {
144 info = list_iter->data;
148 if (info->tr_status == BT_TR_SUCCESS)
149 ad->recv_data.tr_success++;
151 ad->recv_data.tr_fail++;
153 list_iter = g_slist_next(list_iter);
156 if ((ad->recv_data.tr_success + ad->recv_data.tr_fail) != 0) {
158 snprintf(str, sizeof(str), BT_TR_STATUS,
159 ad->recv_data.tr_success, ad->recv_data.tr_fail);
160 DBG("str = [%s] \n", str);
162 noti = _bt_create_notification(BT_NOTI_T);
163 _bt_set_notification_app_launch(noti, CREATE_TR_LIST,
164 NOTI_TR_TYPE_IN, NULL, NULL);
165 _bt_set_notification_property(noti, QP_NO_DELETE | QP_NO_TICKER);
166 _bt_insert_notification(noti,
167 BT_STR_RECEIVED, str,
169 ad->receive_noti = noti;
172 bt_share_release_tr_data_list(tr_data_list);
177 bt_share_close_db(db);
182 static notification_h __bt_update_notification_adapter_status(void)
185 notification_error_e ret;
187 noti = _bt_create_notification(BT_NOTI_T);
191 ret = notification_set_property(noti, QP_NO_DELETE | QP_NO_TICKER);
192 if (ret != NOTIFICATION_ERROR_NONE) {
196 ret = notification_set_application(noti, "ug-bluetooth-efl");
197 if (ret != NOTIFICATION_ERROR_NONE) {
201 ret = notification_set_display_applist(noti,
202 NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
203 if (ret != NOTIFICATION_ERROR_NONE) {
207 _bt_insert_notification(noti,
208 BT_STR_BLUETOOTH_ON, BT_STR_BLUETOOTH_AVAILABLE,
213 ERR("Fail to register notification");
214 notification_free(noti);
219 static gboolean __bt_dbus_request_name(void)
222 DBusConnection *conn;
225 dbus_error_init(&err);
227 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
229 if (dbus_error_is_set(&err))
232 ret_code = dbus_bus_request_name(conn,
234 DBUS_NAME_FLAG_DO_NOT_QUEUE,
236 if (dbus_error_is_set(&err))
239 if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER == ret_code) {
240 dbus_connection_unref(conn);
245 if (dbus_error_is_set(&err)) {
246 ERR("D-Bus Error: %s\n", err.message);
247 dbus_error_free(&err);
251 dbus_connection_unref(conn);
257 int _bt_init_obex_server(void)
259 char storage[STORAGE_PATH_LEN_MAX];
261 _bt_get_default_storage(storage);
262 if (bluetooth_obex_server_init(storage) !=
263 BLUETOOTH_ERROR_NONE) {
264 DBG("Fail to init obex server");
265 return BT_SHARE_FAIL;
268 bluetooth_obex_server_set_root(BT_FTP_FOLDER);
270 return BT_SHARE_ERROR_NONE;
273 void _bt_terminate_app(void)
276 g_main_loop_quit(main_loop);
283 struct bt_appdata ad;
284 DBG("Starting bluetooth-share daemon");
285 memset(&ad, 0, sizeof(struct bt_appdata));
288 signal(SIGTERM, __bt_sigterm_handler);
292 if (__bt_dbus_request_name() == FALSE) {
293 DBG("Aleady dbus instance existed\n");
297 /* init internationalization */
298 if (appcore_set_i18n(BT_COMMON_PKG, BT_COMMON_RES) < 0)
301 /* Set the uid / gid to 5000 */
302 if (set_app_privilege("com.samsung.bluetooth-share", NULL, NULL) !=
303 PC_OPERATION_SUCCESS)
304 ERR("Failed to set app privilege.\n");
306 bluetooth_register_callback(_bt_share_event_handler, NULL);
307 ret = bluetooth_opc_init();
308 if (ret != BLUETOOTH_ERROR_NONE) {
309 ERR("bluetooth_opc_init failed!!\n");
313 _bt_init_dbus_signal();
314 _bt_init_vconf_notification();
315 __bt_update_notification_status_values();
316 _bt_register_notification_cb(&ad);
318 if (_bt_init_obex_server() == BT_SHARE_ERROR_NONE)
319 ad.obex_server_init = TRUE;
321 if (terminated == TRUE) {
322 __bt_release_service(&ad);
327 noti = __bt_update_notification_adapter_status();
329 main_loop = g_main_loop_new(NULL, FALSE);
330 g_main_loop_run(main_loop);
332 _bt_delete_notification(noti);
333 __bt_release_service(&ad);