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>
28 /* For multi-user support */
29 #include <tzplatform_config.h>
32 #include "bt-share-main.h"
33 #include "bluetooth-api.h"
34 #include "obex-event-handler.h"
35 #include "bt-share-ipc.h"
36 #include "bt-share-noti-handler.h"
37 #include "bt-share-resource.h"
38 #include "bt-share-notification.h"
39 #include "bt-share-common.h"
40 #include "bt-share-cynara.h"
41 #include "bt-share-db.h"
44 #include <sys/types.h>
48 #include "bluetooth-share-api.h"
49 #include "notification_internal.h"
51 #define BLUETOOTH_SHARE_BUS "org.projectx.bluetooth.share"
53 static gboolean terminated;
55 GMainLoop *main_loop = NULL;
56 struct bt_appdata *app_state = NULL;
58 void _bt_terminate_bluetooth_share(void)
63 g_main_loop_quit(main_loop);
70 static void __bt_release_service(struct bt_appdata *ad)
75 _bt_deinit_vconf_notification();
76 _bt_delete_notification(ad->send_noti);
77 _bt_delete_notification(ad->receive_noti);
78 _bt_delete_notification(ad->opc_noti);
79 _bt_clear_receive_noti_list();
81 ad->receive_noti = NULL;
84 bluetooth_opc_deinit();
85 bluetooth_obex_server_deinit();
86 _bt_unregister_notification_cb(ad);
88 DBG("Terminating bluetooth-share daemon");
91 static void __bt_sigterm_handler(int signo)
96 g_main_loop_quit(main_loop);
104 static void __bt_update_notification_status_values()
106 struct bt_appdata *ad = app_state;
107 GSList *tr_data_list = NULL;
108 GSList *list_iter = NULL;
109 bt_tr_data_t *info = NULL;;
110 char str[NOTIFICATION_TEXT_LEN_MAX] = { 0 };
111 notification_h noti = NULL;
114 /* Update notification status durning BT off */
115 if (_bt_update_notification_status(ad) == FALSE) {
116 DBG("Notification item is not existed.");
120 DBG("Initialize transfer information");
121 db = bt_share_open_db();
125 tr_data_list = bt_share_get_all_tr_data_list(db, BT_DB_OUTBOUND);
126 if (NULL != tr_data_list) {
127 list_iter = tr_data_list;
129 while (NULL != list_iter) {
130 info = list_iter->data;
134 if (info->tr_status == BT_TR_SUCCESS) {
135 ad->send_data.tr_success++;
136 } else if (info->tr_status == BT_TR_FAIL) {
137 ad->send_data.tr_fail++;
138 } else if (info->tr_status == BT_TR_ONGOING) {
139 /* In case of ongoing file transfer if bluetooth is switched off
140 we need to update the status to fail for these transaction */
141 ad->send_data.tr_fail++;
142 info->tr_status = BT_TR_FAIL;
143 bt_share_update_tr_data(db, BT_DB_OUTBOUND, info->id, info);
145 ERR("Invalid status\n");
148 list_iter = g_slist_next(list_iter);
151 if ((ad->send_data.tr_success + ad->send_data.tr_fail) != 0) {
152 snprintf(str, sizeof(str), "%s %d %d", BT_TR_STATUS,
153 ad->send_data.tr_success, ad->send_data.tr_fail);
155 noti = _bt_create_notification(BT_NOTI_T);
156 //_bt_set_notification_app_launch(noti,
158 // NOTI_TR_TYPE_OUT, NULL, NULL);
159 _bt_set_notification_property(noti, QP_NO_DELETE | QP_NO_TICKER);
160 // _bt_insert_notification(noti,
163 ad->send_noti = noti;
166 bt_share_release_tr_data_list(tr_data_list);
171 tr_data_list = bt_share_get_all_tr_data_list(db, BT_DB_INBOUND);
172 if (NULL != tr_data_list) {
173 list_iter = tr_data_list;
175 while (NULL != list_iter) {
176 info = list_iter->data;
180 if (info->tr_status == BT_TR_SUCCESS)
181 ad->recv_data.tr_success++;
183 ad->recv_data.tr_fail++;
185 list_iter = g_slist_next(list_iter);
188 if ((ad->recv_data.tr_success + ad->recv_data.tr_fail) != 0) {
190 snprintf(str, sizeof(str), "%s %d %d", BT_TR_STATUS,
191 ad->recv_data.tr_success, ad->recv_data.tr_fail);
192 DBG("str = [%s] \n", str);
194 noti = _bt_create_notification(BT_NOTI_T);
195 //_bt_set_notification_app_launch(noti, CREATE_TR_LIST,
196 // NOTI_TR_TYPE_IN, NULL, NULL);
197 _bt_set_notification_property(noti, QP_NO_DELETE | QP_NO_TICKER);
198 // _bt_insert_notification(noti,
199 // BT_STR_RECEIVED, str,
200 // BT_ICON_QP_RECEIVE);
201 ad->receive_noti = noti;
204 bt_share_release_tr_data_list(tr_data_list);
209 bt_share_close_db(db);
214 static notification_h __bt_update_notification_adapter_status(void)
217 notification_error_e ret;
219 noti = _bt_create_notification(BT_NOTI_T);
223 ret = notification_set_property(noti, QP_NO_DELETE | QP_NO_TICKER);
224 if (ret != NOTIFICATION_ERROR_NONE) {
228 ret = notification_set_application(noti, "ug-bluetooth-efl");
229 if (ret != NOTIFICATION_ERROR_NONE) {
233 ret = notification_set_display_applist(noti,
234 NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY);
235 if (ret != NOTIFICATION_ERROR_NONE) {
239 // _bt_insert_notification(noti,
240 // BT_STR_BLUETOOTH_ON, BT_STR_BLUETOOTH_AVAILABLE,
241 // BT_ICON_QP_BT_ON);
245 ERR("Fail to register notification");
246 notification_free(noti);
251 static int __bt_lang_changed_cb(void *data, void *user_data)
253 if (appcore_set_i18n(BT_COMMON_PKG, BT_COMMON_RES) < 0)
259 static gboolean __bt_dbus_request_name(void)
262 DBusConnection *conn;
265 dbus_error_init(&err);
267 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
269 if (dbus_error_is_set(&err))
272 ret_code = dbus_bus_request_name(conn,
274 DBUS_NAME_FLAG_DO_NOT_QUEUE,
276 if (dbus_error_is_set(&err))
279 if(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER == ret_code) {
280 dbus_connection_unref(conn);
285 if (dbus_error_is_set(&err)) {
286 ERR("D-Bus Error: %s\n", err.message);
287 dbus_error_free(&err);
291 dbus_connection_unref(conn);
297 int _bt_init_obex_server(void)
299 char storage[STORAGE_PATH_LEN_MAX];
301 _bt_get_default_storage(storage);
302 if (bluetooth_obex_server_init(storage) !=
303 BLUETOOTH_ERROR_NONE) {
304 DBG("Fail to init obex server");
305 return BT_SHARE_FAIL;
308 bluetooth_obex_server_set_root(BT_FTP_FOLDER);
310 return BT_SHARE_ERROR_NONE;
313 void _bt_terminate_app(void)
316 g_main_loop_quit(main_loop);
320 void __bt_create_transfer_db(void)
325 /* Check if the DB exists; if not, create it and initialize it */
326 ret = stat(BT_TRANSFER_DB, &sts);
327 if (ret == -1 && errno == ENOENT)
329 DBG("DB %s doesn't exist, it needs to be created and initialized", BT_TRANSFER_DB);
330 DBG("script path: %s", SCRIPT_INIT_DB);
331 ret = system(SCRIPT_INIT_DB);
332 if (ret != EXIT_SUCCESS)
333 ERR("Exit code of epp not clean: %i", ret);
340 struct bt_appdata ad;
341 DBG("Starting bluetooth-share daemon");
342 memset(&ad, 0, sizeof(struct bt_appdata));
345 signal(SIGTERM, __bt_sigterm_handler);
347 __bt_create_transfer_db();
349 if (__bt_dbus_request_name() == FALSE) {
350 DBG("Aleady dbus instance existed\n");
354 ret = appcore_set_event_callback(APPCORE_EVENT_LANG_CHANGE, __bt_lang_changed_cb, NULL);
356 DBG("Failed to excute the change of language");
359 /* init internationalization */
360 if (appcore_set_i18n(BT_COMMON_PKG, BT_COMMON_RES) < 0)
363 uid_t network_user = 551; /* uid of network_fw */
364 gid_t network_group = 551; /* gid of network_fw */
366 initgroups("network_fw", network_group);
367 ret = setgid(network_group);
368 DBG("setgid return : %d", ret);
369 ret = setuid(network_user);
370 DBG("setuid return : %d", ret);
372 if (_bt_share_cynara_init()) {
373 ERR("Failed to initialize Cynara.\n");
377 bluetooth_register_callback(_bt_share_event_handler, NULL);
378 ret = bluetooth_opc_init();
379 if (ret != BLUETOOTH_ERROR_NONE) {
380 ERR("bluetooth_opc_init failed!!\n");
384 _bt_init_dbus_signal();
385 _bt_init_vconf_notification(&ad);
386 __bt_update_notification_status_values();
387 _bt_register_notification_cb(&ad);
389 if (_bt_init_obex_server() == BT_SHARE_ERROR_NONE)
390 ad.obex_server_init = TRUE;
392 if (terminated == TRUE) {
393 __bt_release_service(&ad);
394 bluetooth_unregister_callback();
399 noti = __bt_update_notification_adapter_status();
401 main_loop = g_main_loop_new(NULL, FALSE);
402 g_main_loop_run(main_loop);
404 _bt_delete_notification(noti);
405 __bt_release_service(&ad);
406 bluetooth_unregister_callback();
407 _bt_share_cynara_finish();