2 * Copyright (c) 2011 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.
19 #include <vconf-keys.h>
20 #include <vconf-internal-radio-keys.h>
22 #include <eventsystem.h>
24 #ifdef TIZEN_FEATURE_ACTD
25 #include <actd/unit_control.h>
28 #include "bt-core-main.h"
29 #include "bt-core-adapter.h"
30 #include "bt-core-common.h"
31 #include "bt-core-dbus-handler.h"
32 #include "bt-core-noti-handler.h"
34 #include "bt-internal-types.h"
36 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
37 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
39 #ifdef TIZEN_FEATURE_BT_OTP
40 #define BT_OTP_OBJECT_PATH "/org/projectx/otp"
41 #define BT_OTP_INTERFACE_NAME "org.projectx.otp_service"
42 #define BLE_DISABLED "LeDisabled"
45 static bt_status_t adapter_status = BT_DEACTIVATED;
46 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
47 static gboolean is_recovery_mode = FALSE;
48 static guint core_enable_timer_id;
51 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
52 static int bt_le_status_before[BT_MODE_MAX] = { VCONFKEY_BT_LE_STATUS_OFF, };
54 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value);
56 static void __bt_core_set_status(bt_status_t status)
58 adapter_status = status;
61 bt_status_t _bt_core_get_status(void)
63 return adapter_status;
66 static void __bt_core_set_le_status(bt_le_status_t status)
68 adapter_le_status = status;
71 bt_le_status_t _bt_core_get_le_status(void)
73 return adapter_le_status;
76 int _bt_core_get_bt_status(bt_mode_e mode)
78 return bt_status_before[mode];
81 int _bt_core_get_bt_le_status(bt_mode_e mode)
83 return bt_le_status_before[mode];
86 void _bt_core_set_bt_status(bt_mode_e mode, int status)
88 bt_status_before[mode] = status;
91 void _bt_core_set_bt_le_status(bt_mode_e mode, int status)
93 bt_le_status_before[mode] = status;
96 gboolean _bt_core_is_recovery_mode(void)
98 return is_recovery_mode;
101 static gboolean __bt_core_idle_terminate(gpointer data)
104 _bt_core_terminate();
108 gboolean _bt_core_is_flight_mode_enabled(void)
110 int isFlightMode = 0;
113 if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
114 ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
116 BT_ERR("vconf_get_bool failed");
124 #ifdef TIZEN_FEATURE_ACTD
125 static int __bt_call_systemact_service(const char *service_name)
129 BT_DBG("Use System Activated : %s", service_name);
131 ret = actd_start_unit(UNIT_CONTROL_BUS_TYPE_SYSTEM, service_name, 5000);
133 if (ret != UNIT_CONTROL_OK) {
134 BT_ERR("Failed to activate the %s: %d", service_name, ret);
135 /* Return Success even if the activating result is fail */
142 static int __bt_call_systemact_service(const char *file_path)
148 BT_ERR("service file path is NULL");
152 if (!access(file_path, F_OK)) {
157 fp = fopen(file_path, "w");
159 BT_ERR("Failed to fopen service file");
171 static int __execute_command(const char *cmd, char *const arg_list[])
181 BT_ERR("fork failed");
187 BT_ERR("fork failed");
188 } else if (pid2 == 0) {
189 if (arg_list != NULL) {
190 execv(cmd, arg_list);
192 char *argv[] = { NULL };
201 BT_DBG("parent : forked[%d]", pid);
202 waitpid(pid, &status, 0);
203 BT_DBG("child is terminated : %d", status);
210 static int __bt_stack_up(void)
214 /* activate HCI logger */
215 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_LOGGER_START);
217 BT_ERR("Failed to call systemact service");
222 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
224 BT_ERR("Failed to call systemact service");
228 /* activate bluetooth-share */
229 if (!TIZEN_PROFILE_WEARABLE) {
230 ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUETOOTH_SHARE_START);
232 BT_ERR("Failed to call systemact service");
240 int _bt_enable_adapter(void)
245 bt_le_status_t le_status;
249 status = _bt_core_get_status();
250 if (status != BT_DEACTIVATED) {
251 BT_ERR("Invalid state %d", status);
252 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
256 #if 0 /* only the concept of private */
257 le_status = _bt_core_get_le_status();
258 if (le_status == BT_LE_ACTIVATED) {
259 /* Turn on PSCAN, (ISCAN if needed) */
260 /* Return with 0 for the Enabled response. */
261 __bt_core_set_status(BT_ACTIVATED);
262 BT_INFO("BR/EDR is enabled.");
263 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
268 __bt_core_set_status(BT_ACTIVATING);
269 if (TIZEN_FEATURE_BT_USB_DONGLE) {
270 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
272 BT_ERR("Failed to call systemact service");
274 ret = __bt_stack_up();
277 BT_ERR("running script failed");
278 if (TIZEN_FEATURE_BT_USB_DONGLE) {
279 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
281 BT_ERR("Failed to call systemact service");
283 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
285 __bt_core_set_status(BT_DEACTIVATED);
292 int _bt_disable_adapter(void)
296 bt_le_status_t le_status;
299 BT_INFO_C("Disable adapter");
301 #if 0 /* only the concept of private */
302 le_status = _bt_core_get_le_status();
303 BT_DBG("le_status : %d", le_status);
304 if (le_status == BT_LE_ACTIVATED) {
305 /* Turn off PSCAN, (ISCAN if needed) */
306 /* Return with 0 for the Disabled response. */
307 __bt_core_set_status(BT_DEACTIVATED);
308 BT_INFO("BR/EDR is disabled. now LE only mode");
309 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
314 status = _bt_core_get_status();
315 if (status == BT_ACTIVATING) {
316 /* Forcely terminate */
317 if (TIZEN_FEATURE_BT_USB_DONGLE) {
318 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0)
319 BT_ERR("Failed to call systemact service");
321 #ifdef TIZEN_FEATURE_RADIO
322 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
324 /* Check if radio status on or off */
325 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
326 BT_ERR("Fail to get radio status");
328 BT_DBG("Radio status: %d", radio_status);
330 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
331 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0)
332 BT_ERR("running script failed");
334 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
335 BT_ERR("running script failed");
338 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
339 BT_ERR("running script failed");
342 _bt_core_terminate();
344 } else if (status != BT_ACTIVATED) {
345 BT_ERR("Invalid state %d", status);
346 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
349 __bt_core_set_status(BT_DEACTIVATING);
350 if (TIZEN_FEATURE_BT_USB_DONGLE) {
351 if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0) {
352 BT_ERR("Failed to call systemact service");
353 __bt_core_set_status(BT_ACTIVATED);
356 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
358 #ifdef TIZEN_FEATURE_RADIO
359 int radio_status = VCONFKEY_RADIO_STATUS_OFF;
361 /* Check if radio status on or off */
362 if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
363 BT_ERR("Fail to get radio status");
365 BT_DBG("Radio status: %d", radio_status);
367 if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
368 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0) {
369 BT_ERR("running script failed");
370 __bt_core_set_status(BT_ACTIVATED);
374 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
375 BT_ERR("running script failed");
376 __bt_core_set_status(BT_ACTIVATED);
381 if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
382 BT_ERR("running script failed");
383 __bt_core_set_status(BT_ACTIVATED);
392 int _bt_enable_adapter_le(void)
398 bt_le_status_t le_status;
399 le_status = _bt_core_get_le_status();
400 retv_if(le_status == BT_LE_ACTIVATED, 0);
401 retv_if(le_status != BT_LE_DEACTIVATED, -1);
403 status = _bt_core_get_status();
404 if (status == BT_DEACTIVATED) {
405 __bt_core_set_le_status(BT_LE_ACTIVATING);
406 BT_DBG("Activate BT");
407 if (TIZEN_FEATURE_BT_USB_DONGLE) {
408 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
410 BT_ERR("Failed to call systemact service");
412 ret = __bt_stack_up();
415 BT_ERR("running script failed");
416 if (TIZEN_FEATURE_BT_USB_DONGLE) {
417 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
419 BT_ERR("Failed to call systemact service");
421 ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
423 __bt_core_set_status(BT_DEACTIVATED);
424 __bt_core_set_le_status(BT_LE_DEACTIVATED);
428 __bt_core_set_le_status(BT_LE_ACTIVATED);
429 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
431 #ifdef TIZEN_FEATURE_BT_HPS
432 ret = _bt_core_start_httpproxy();
434 BT_ERR("_bt_core_start_httpproxy() failed");
440 #ifdef TIZEN_FEATURE_BT_OTP
441 static void _bt_core_stop_otp()
443 GError *error = NULL;
444 GDBusMessage *msg = NULL;
445 GDBusConnection *conn = _bt_core_get_gdbus_connection();
446 msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
447 if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
449 BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
451 error->code, error->message);
452 g_clear_error(&error);
458 int _bt_disable_adapter_le(void)
463 bt_le_status_t le_status;
465 le_status = _bt_core_get_le_status();
466 retv_if(le_status == BT_LE_DEACTIVATED, 0);
467 retv_if(le_status == BT_LE_DEACTIVATING, -1);
469 #ifdef TIZEN_FEATURE_BT_HPS
470 _bt_core_stop_httpproxy();
473 #ifdef TIZEN_FEATURE_BT_OTP
477 status = _bt_core_get_status();
478 BT_DBG("status : %d", status);
480 if (status == BT_DEACTIVATED) {
481 __bt_core_set_le_status(BT_LE_DEACTIVATING);
483 if (TIZEN_FEATURE_BT_USB_DONGLE) {
484 ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
486 BT_ERR("Failed to call systemact service");
488 ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
491 BT_ERR("running script failed");
492 __bt_core_set_le_status(BT_LE_ACTIVATED);
496 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
499 __bt_core_set_le_status(BT_LE_DEACTIVATED);
505 int _bt_core_service_request_adapter(int service_function)
509 GArray *in_param1 = NULL;
510 GArray *in_param2 = NULL;
511 GArray *in_param3 = NULL;
512 GArray *in_param4 = NULL;
513 GArray *out_param = NULL;
515 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
517 ret = _bt_core_service_request(BT_CORE_SERVICE, service_function,
518 in_param1, in_param2, in_param3, in_param4, &out_param);
520 BT_ERR("_bt_core_service_request_adapter() failed");
522 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
527 static gboolean __bt_core_check_the_adapter_path(void)
530 GDBusProxy *manager_proxy = NULL;
531 GVariant *result = NULL;
532 char *adapter_path = NULL;
533 GDBusConnection *conn = NULL;
535 conn = _bt_core_get_gdbus_connection();
539 manager_proxy = g_dbus_proxy_new_sync(conn,
540 G_DBUS_PROXY_FLAGS_NONE, NULL,
543 "org.freedesktop.DBus.ObjectManager",
546 if (!manager_proxy) {
548 BT_ERR("Unable to create proxy: %s", err->message);
551 BT_ERR("Fail to create proxy");
556 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
557 G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
560 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
563 BT_ERR("Fail to get DefaultAdapter");
568 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
569 BT_ERR("Incorrect result\n");
573 g_variant_get(result, "(&o)", &adapter_path);
575 if (adapter_path == NULL ||
576 strlen(adapter_path) >= BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX) {
577 BT_ERR("Adapter path is inproper\n");
581 g_variant_unref(result);
582 g_object_unref(manager_proxy);
588 g_variant_unref(result);
591 g_object_unref(manager_proxy);
596 void _bt_core_update_status(void)
598 int bt_status = VCONFKEY_BT_STATUS_OFF;
599 int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
600 gboolean ret = FALSE;
602 ret = __bt_core_check_the_adapter_path();
603 BT_INFO("check the real status of bt_adapter");
606 __bt_core_set_status(BT_DEACTIVATED);
607 __bt_core_set_le_status(BT_DEACTIVATED);
609 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status) < 0)
610 BT_ERR("no bluetooth device info, so BT was disabled at previous session");
611 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status) < 0)
612 BT_ERR("no bluetooth le info, so BT LE was disabled at previous session");
614 BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
616 if (bt_status & VCONFKEY_BT_STATUS_ON)
617 __bt_core_set_status(BT_ACTIVATED);
618 if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
619 __bt_core_set_le_status(BT_ACTIVATED);
623 gboolean _bt_core_enable_adapter(void)
627 _bt_set_flightmode_request(FALSE);
628 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
629 BT_ERR("Set vconf failed");
631 ret = _bt_enable_adapter();
638 static gboolean __bt_core_terminate_cb(gpointer data)
640 _bt_core_terminate();
645 gboolean _bt_core_disable_adapter(void)
648 gboolean adapter_state;
650 _bt_set_flightmode_request(FALSE);
651 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
652 BT_ERR("Set vconf failed");
654 adapter_state = __bt_core_check_the_adapter_path();
655 if (adapter_state == FALSE)
656 BT_INFO("Default adapter is NOT normal.");
658 ret = _bt_disable_adapter();
659 if (adapter_state == FALSE) {
660 g_timeout_add(BT_CORE_IDLE_TERM_TIME,
661 __bt_core_terminate_cb, NULL);
671 gboolean _bt_core_recover_adapter(void)
677 BT_INFO_C("Recover bt adapter");
679 _bt_set_flightmode_request(FALSE);
680 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
681 BT_ERR("Set vconf failed");
683 is_recovery_mode = TRUE;
685 _bt_core_update_status();
687 if (_bt_core_get_status() == BT_ACTIVATED) {
688 _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
689 #ifdef TIZEN_FEATURE_BUSACT
690 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
693 if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
694 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
695 #ifdef TIZEN_FEATURE_BUSACT
696 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
700 ret = _bt_disable_adapter();
702 BT_ERR("_bt_disable_adapter() failed");
704 /* In platform, don't seperate BR/EDR and LE status */
706 ret_le = _bt_disable_adapter_le();
708 BT_ERR("_bt_disable_adapter_le() failed");
713 gboolean _bt_core_enable_adapter_le(void)
717 ret = _bt_enable_adapter_le();
724 gboolean _bt_core_disable_adapter_le(void)
730 ret = _bt_disable_adapter_le();
737 gboolean __bt_core_reset_adapter(void)
739 /* Forcely terminate */
740 if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
741 BT_ERR("running script failed");
743 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
748 static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
752 core_enable_timer_id = 0;
754 _bt_core_init_vconf_value();
759 gboolean _bt_core_enable_core(void)
763 _bt_core_update_status();
765 if (core_enable_timer_id > 0)
766 g_source_remove(core_enable_timer_id);
768 core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
774 gboolean _bt_core_set_transfer_value(gboolean value)
776 const char *event_val = NULL;
779 BT_DBG("value: %d", value);
781 event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
783 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
784 event_val) != ES_R_OK) {
785 BT_ERR("Fail to set BT state value");
789 if (ret == FALSE || value == FALSE) {
790 BT_DBG("Terminate bt-core process");
791 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
798 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
800 BT_DBG("Test item : %s", type);
802 #ifdef TIZEN_FEATURE_ACTD
803 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
804 __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_ON);
805 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
806 __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_OFF);
808 BT_DBG("Terminate bt-core process");
809 g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
814 char *arg_list[3] = { NULL, NULL, NULL };
816 if (g_strcmp0(type, "Enable_RF_Test") == 0) {
817 cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
818 arg_list[0] = "bt-edutm-on.sh";
819 } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
820 cmd = "/usr/etc/bluetooth/bt-edutm-off.sh";
821 arg_list[0] = "bt-edutm-off.sh";
822 } else if (g_strcmp0(type, "Slave_Mode") == 0) {
823 cmd = "/usr/etc/bluetooth/bt-mode-slave.sh";
824 arg_list[0] = "bt-mode-slave.sh";
825 } else if (g_strcmp0(type, "Master_Mode") == 0) {
826 cmd = "/usr/etc/bluetooth/bt-mode-master.sh";
827 arg_list[0] = "bt-mode-master.sh";
828 } else if (g_strcmp0(type, "SSP_Debug_Mode") == 0) {
829 cmd = "/usr/etc/bluetooth/bt-set-ssp-debug-mode.sh";
830 arg_list[0] = "bt-set-ssp-debug-mode.sh";
831 arg_list[1] = (char *)arg;
832 } else if (g_strcmp0(type, "RF_Channel") == 0) {
833 cmd = "/usr/etc/bluetooth/bt-enable-rf-channel.sh";
834 arg_list[0] = "bt-enable-rf-channel.sh";
835 arg_list[1] = (char *)arg;
840 BT_DBG("Run %s", cmd);
841 if (__execute_command(cmd, arg_list) < 0)
842 BT_ERR("running script failed");
848 static gboolean __bt_core_recovery_cb(gpointer data)
851 gboolean is_request_failed = FALSE;
852 static gboolean is_first_failure = TRUE;
856 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
857 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
859 is_request_failed = TRUE;
862 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
863 ret = _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
865 is_request_failed = TRUE;
868 if (is_request_failed == TRUE) {
869 BT_ERR("Recovery is failed.");
870 if (is_first_failure == TRUE) {
871 g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
872 is_first_failure = FALSE;
875 is_first_failure = TRUE;
879 is_first_failure = TRUE;
881 if (_bt_core_get_bt_status(BT_RECOVERY_MODE) == 1) {
882 _bt_core_set_bt_status(BT_RECOVERY_MODE, 0);
883 ret = _bt_enable_adapter();
885 BT_ERR("_bt_enable_adapter() failed");
889 if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
890 _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
891 ret = _bt_enable_adapter_le();
893 BT_ERR("_bt_enable_adapter_le() failed");
896 is_recovery_mode = FALSE;
903 static gboolean __bt_core_enable_timeout_cb(gpointer data)
905 bt_status_t adapter_status;
906 bt_le_status_t adapter_status_le;
910 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
911 BT_ERR("Set vconf failed");
913 adapter_status = _bt_core_get_status();
914 adapter_status_le = _bt_core_get_le_status();
916 if (adapter_status == BT_DEACTIVATED &&
917 _bt_core_get_bt_status(BT_FLIGHT_MODE) != 0) {
918 _bt_core_set_bt_status(BT_FLIGHT_MODE, 0);
919 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER);
920 _bt_enable_adapter();
923 if (adapter_status_le == BT_LE_DEACTIVATED &&
924 _bt_core_get_bt_le_status(BT_FLIGHT_MODE) != 0) {
925 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, 0);
926 _bt_core_service_request_adapter(BT_ENABLE_ADAPTER_LE);
927 _bt_enable_adapter_le();
933 static gboolean __bt_core_disable_timeout_cb(gpointer data)
935 bt_status_t adapter_status;
936 bt_le_status_t adapter_status_le;
940 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
941 BT_ERR("Set vconf failed");
943 adapter_status = _bt_core_get_status();
944 adapter_status_le = _bt_core_get_le_status();
946 if (adapter_status == BT_ACTIVATED) {
947 int bt_status_before_mode = 0;
949 if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
950 _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
952 #ifdef TIZEN_FEATURE_BUSACT
953 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
955 _bt_disable_adapter();
958 if (adapter_status_le == BT_LE_ACTIVATED) {
959 int bt_le_status_before_mode = 0;
961 if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
962 _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
964 #ifdef TIZEN_FEATURE_BUSACT
965 _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
967 _bt_disable_adapter_le();
973 static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value)
980 bundle_add_str(b, key, value);
982 ret = eventsystem_send_system_event(event, b);
984 BT_DBG("eventsystem_send_system_event result: %d", ret);
991 void _bt_core_adapter_added_cb(void)
994 bt_le_status_t le_status;
995 gboolean flight_mode_status;
999 status = _bt_core_get_status();
1000 BT_DBG("status : %d", status);
1001 le_status = _bt_core_get_le_status();
1002 BT_DBG("le_status : %d", le_status);
1004 if (status == BT_ACTIVATING)
1005 __bt_core_set_status(BT_ACTIVATED);
1006 if (le_status == BT_LE_ACTIVATING)
1007 __bt_core_set_le_status(BT_LE_ACTIVATED);
1009 flight_mode_status = _bt_core_is_flight_mode_enabled();
1011 if (flight_mode_status == TRUE && _bt_is_flightmode_request() == TRUE) {
1012 _bt_set_flightmode_request(FALSE);
1013 g_timeout_add(2000, (GSourceFunc)__bt_core_disable_timeout_cb, NULL);
1016 _bt_set_flightmode_request(FALSE);
1018 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1019 EVT_VAL_BT_ON) != ES_R_OK)
1020 BT_ERR("Fail to set BT state value");
1022 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1023 EVT_VAL_BT_LE_ON) != ES_R_OK)
1024 BT_ERR("Fail to set BT LE state value");
1026 _bt_core_terminate();
1029 void _bt_core_adapter_removed_cb(void)
1031 int flight_mode_value = 0;
1032 int power_saving_mode = 0;
1033 gboolean flight_mode_status;
1034 static int timer_id = -1;
1036 BT_DBG("is_recovery_mode: %d", is_recovery_mode);
1038 __bt_core_set_status(BT_DEACTIVATED);
1039 __bt_core_set_le_status(BT_LE_DEACTIVATED);
1040 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1041 BT_ERR("Set vconf failed");
1042 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
1043 BT_ERR("Set vconf failed");
1045 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
1046 EVT_VAL_BT_OFF) != ES_R_OK)
1047 BT_ERR("Fail to set value");
1049 if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
1050 EVT_VAL_BT_LE_OFF) != ES_R_OK)
1051 BT_ERR("Fail to set value");
1053 if (is_recovery_mode == TRUE) {
1055 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
1059 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &flight_mode_value) != 0)
1060 BT_ERR("Fail to get the flight_mode_deactivated value");
1062 if (vconf_get_int(BT_OFF_DUE_TO_POWER_SAVING_MODE, &power_saving_mode) != 0)
1063 BT_ERR("Fail to get the ps_mode_deactivated value");
1065 flight_mode_status = _bt_core_is_flight_mode_enabled();
1067 if (flight_mode_status == FALSE && _bt_is_flightmode_request() == TRUE) {
1068 _bt_set_flightmode_request(FALSE);
1070 timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_enable_timeout_cb, NULL);
1073 _bt_set_flightmode_request(FALSE);
1075 if (flight_mode_value == 1 || power_saving_mode == 1) {
1076 BT_DBG("Bt Core not terminated");
1080 _bt_core_terminate();