Fix the coverity issue (Resource leak)
[platform/core/connectivity/bluetooth-frwk.git] / bt-core / bt-core-adapter.c
old mode 100755 (executable)
new mode 100644 (file)
index 9fea808..10ea862
 
 #include <vconf.h>
 #include <vconf-keys.h>
+#include <vconf-internal-radio-keys.h>
 #include <bundle.h>
 #include <eventsystem.h>
+#include <stdio.h>
+#ifdef TIZEN_FEATURE_ACTD
+#include <actd/unit_control.h>
+#endif
 
 #include "bt-core-main.h"
 #include "bt-core-adapter.h"
 #include "bt-core-dbus-handler.h"
 #include "bt-core-noti-handler.h"
 
+#include "bt-internal-types.h"
+
 #define BT_CORE_IDLE_TERM_TIME 200 /* 200ms */
 #define BT_CORE_CHECK_ADAPTER_OBJECT_PATH_MAX 50
 
+#ifdef TIZEN_FEATURE_BT_OTP
+#define BT_OTP_OBJECT_PATH     "/org/projectx/otp"
+#define BT_OTP_INTERFACE_NAME  "org.projectx.otp_service"
+#define BLE_DISABLED           "LeDisabled"
+#endif
 
 static bt_status_t adapter_status = BT_DEACTIVATED;
 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
 static gboolean is_recovery_mode = FALSE;
+static guint core_enable_timer_id;
+
 
 static int bt_status_before[BT_MODE_MAX] = { VCONFKEY_BT_STATUS_OFF, };
 static int bt_le_status_before[BT_MODE_MAX] = { VCONFKEY_BT_LE_STATUS_OFF, };
 
+static int __bt_eventsystem_set_value(const char *event, const char *key, const char *value);
+
 static void __bt_core_set_status(bt_status_t status)
 {
        adapter_status = status;
@@ -91,19 +107,66 @@ static gboolean __bt_core_idle_terminate(gpointer data)
 
 gboolean _bt_core_is_flight_mode_enabled(void)
 {
-#ifdef TIZEN_BT_FLIGHTMODE_ENABLED
        int isFlightMode = 0;
        int ret = -1;
 
-       ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
-       if (ret != 0) {
-               BT_ERR("vconf_get_bool failed");
+       if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
+               ret = vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &isFlightMode);
+               if (ret != 0)
+                       BT_ERR("vconf_get_bool failed");
+
+               return isFlightMode;
+       } else {
+               return FALSE;
        }
-       return isFlightMode;
+}
+
+#ifdef TIZEN_FEATURE_ACTD
+static int __bt_call_systemact_service(const char *service_name)
+{
+       int ret;
+
+       BT_DBG("Use System Activated : %s", service_name);
+
+       ret = actd_start_unit(UNIT_CONTROL_BUS_TYPE_SYSTEM, service_name, 5000);
+
+       if (ret != UNIT_CONTROL_OK) {
+               BT_ERR("Failed to activate the %s: %d", service_name, ret);
+               /* Return Success even if the activating result is fail */
+               return 0;
+       }
+
+       return 0;
+}
 #else
-       return FALSE;
-#endif
+static int __bt_call_systemact_service(const char *file_path)
+{
+       BT_DBG("+");
+       FILE *fp;
+
+       if (!file_path) {
+               BT_ERR("service file path is NULL");
+               return -1;
+       }
+
+       if (!access(file_path, F_OK)) {
+               remove(file_path);
+               usleep(100);
+       }
+
+       fp = fopen(file_path, "w");
+       if (!fp) {
+               BT_ERR("Failed to fopen service file");
+               return -1;
+       }
+
+
+       fclose(fp);
+       BT_DBG("-");
+       return 0;
+
 }
+#endif
 
 static int __execute_command(const char *cmd, char *const arg_list[])
 {
@@ -123,7 +186,12 @@ static int __execute_command(const char *cmd, char *const arg_list[])
                if (pid2 == -1) {
                        BT_ERR("fork failed");
                } else if (pid2 == 0) {
-                       execv(cmd, arg_list);
+                       if (arg_list != NULL) {
+                               execv(cmd, arg_list);
+                       } else {
+                               char *argv[] = { NULL };
+                               execv(cmd, argv);
+                       }
                        exit(256);
                }
                exit(0);
@@ -139,20 +207,53 @@ static int __execute_command(const char *cmd, char *const arg_list[])
        return 0;
 }
 
+static int __bt_stack_up(void)
+{
+       int ret;
+
+       /* activate HCI logger */
+       ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_LOGGER_START);
+       if (ret < 0) {
+               BT_ERR("Failed to call systemact service");
+               return -1;
+       }
+
+       /* activate Bluez */
+       ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
+       if (ret < 0) {
+               BT_ERR("Failed to call systemact service");
+               return -1;
+       }
+
+       /* activate bluetooth-share */
+       if (!TIZEN_PROFILE_WEARABLE) {
+               ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUETOOTH_SHARE_START);
+               if (ret < 0) {
+                       BT_ERR("Failed to call systemact service");
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
 int _bt_enable_adapter(void)
 {
        int ret;
        bt_status_t status;
+#if 0
        bt_le_status_t le_status;
-
+#endif
        BT_INFO("");
 
        status = _bt_core_get_status();
        if (status != BT_DEACTIVATED) {
                BT_ERR("Invalid state %d", status);
+               g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
                return -1;
        }
 
+#if 0 /* only the concept of private */
        le_status = _bt_core_get_le_status();
        if (le_status == BT_LE_ACTIVATED) {
                /* Turn on PSCAN, (ISCAN if needed) */
@@ -162,22 +263,30 @@ int _bt_enable_adapter(void)
                g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
                return 0;
        }
+#endif
 
        __bt_core_set_status(BT_ACTIVATING);
-#ifdef USB_BLUETOOTH
-       char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
-       ret = __execute_command("/usr/bin/hciconfig", argv_up);
-#else
-       ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
-#endif
+       if (TIZEN_FEATURE_BT_USB_DONGLE) {
+               /* activate Bluez */
+               ret = __bt_call_systemact_service(BT_SYSTEMACT_BLUEZ_START);
+               if (ret < 0)
+                       BT_ERR("If bluez already exist, skip this error");
+
+               ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
+               if (ret < 0)
+                       BT_ERR("Failed to call systemact service");
+       } else {
+               ret = __bt_stack_up();
+       }
        if (ret < 0) {
                BT_ERR("running script failed");
-#ifdef USB_BLUETOOTH
-               char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
-               ret = __execute_command("/usr/bin/hciconfig", argv_down);
-#else
-               ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
-#endif
+               if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                       ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
+                       if (ret < 0)
+                               BT_ERR("Failed to call systemact service");
+               } else {
+                       ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh", NULL);
+               }
                __bt_core_set_status(BT_DEACTIVATED);
                return -1;
        }
@@ -188,13 +297,15 @@ int _bt_enable_adapter(void)
 int _bt_disable_adapter(void)
 {
        bt_status_t status;
+#if 0
        bt_le_status_t le_status;
+#endif
 
        BT_INFO_C("Disable adapter");
 
+#if 0 /* only the concept of private */
        le_status = _bt_core_get_le_status();
        BT_DBG("le_status : %d", le_status);
-#if 0 /* only the concept of private */
        if (le_status == BT_LE_ACTIVATED) {
                /* Turn off PSCAN, (ISCAN if needed) */
                /* Return with 0 for the Disabled response. */
@@ -208,30 +319,76 @@ int _bt_disable_adapter(void)
        status = _bt_core_get_status();
        if (status == BT_ACTIVATING) {
                /* Forcely terminate */
-#ifdef USB_BLUETOOTH
-               char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
-               if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
+               if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                       if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0)
+                               BT_ERR("Failed to call systemact service");
+               } else {
+#ifdef TIZEN_FEATURE_RADIO
+                       int radio_status = VCONFKEY_RADIO_STATUS_OFF;
+
+                       /* Check if radio status on or off */
+                       if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
+                               BT_ERR("Fail to get radio status");
+
+                       BT_DBG("Radio status: %d", radio_status);
+
+                       if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
+                               if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0)
+                                       BT_ERR("running script failed");
+                       } else {
+                               if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
+                                       BT_ERR("running script failed");
+                       }
 #else
-               if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
+                       if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0)
+                               BT_ERR("running script failed");
 #endif
-                       BT_ERR("running script failed");
                }
                _bt_core_terminate();
                return 0;
        } else if (status != BT_ACTIVATED) {
                BT_ERR("Invalid state %d", status);
+               g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
        }
 
        __bt_core_set_status(BT_DEACTIVATING);
-#ifdef USB_BLUETOOTH
-       char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
-       if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
+       if (TIZEN_FEATURE_BT_USB_DONGLE) {
+               if (__bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN) < 0) {
+                       BT_ERR("Failed to call systemact service");
+                       __bt_core_set_status(BT_ACTIVATED);
+                       return -1;
+               }
+               g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
+       } else {
+#ifdef TIZEN_FEATURE_RADIO
+               int radio_status = VCONFKEY_RADIO_STATUS_OFF;
+
+               /* Check if radio status on or off */
+               if (vconf_get_int(VCONFKEY_RADIO_STATUS, &radio_status) < 0)
+                       BT_ERR("Fail to get radio status");
+
+               BT_DBG("Radio status: %d", radio_status);
+
+               if (radio_status == VCONFKEY_RADIO_STATUS_ON) {
+                       if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN_WITH_RADIO) < 0) {
+                               BT_ERR("running script failed");
+                               __bt_core_set_status(BT_ACTIVATED);
+                               return -1;
+                       }
+               } else {
+                       if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
+                               BT_ERR("running script failed");
+                               __bt_core_set_status(BT_ACTIVATED);
+                               return -1;
+                       }
+               }
 #else
-       if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
+               if (__bt_call_systemact_service(BT_SYSTEMACT_STACK_DOWN) < 0) {
+                               BT_ERR("running script failed");
+                               __bt_core_set_status(BT_ACTIVATED);
+                               return -1;
+               }
 #endif
-               BT_ERR("running script failed");
-               __bt_core_set_status( BT_ACTIVATED);
-               return -1;
        }
 
        return 0;
@@ -245,26 +402,29 @@ int _bt_enable_adapter_le(void)
        bt_status_t status;
        bt_le_status_t le_status;
        le_status = _bt_core_get_le_status();
+       retv_if(le_status == BT_LE_ACTIVATED, 0);
        retv_if(le_status != BT_LE_DEACTIVATED, -1);
 
        status = _bt_core_get_status();
        if (status == BT_DEACTIVATED) {
                __bt_core_set_le_status(BT_LE_ACTIVATING);
                BT_DBG("Activate BT");
-#ifdef USB_BLUETOOTH
-               char *argv_up[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "up", NULL};
-               ret = __execute_command("/usr/bin/hciconfig", argv_up);
-#else
-               ret = __execute_command("/usr/etc/bluetooth/bt-stack-up.sh", NULL);
-#endif
+               if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                       ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_UP);
+                       if (ret < 0)
+                               BT_ERR("Failed to call systemact service");
+               } else {
+                       ret = __bt_stack_up();
+               }
                if (ret < 0) {
                        BT_ERR("running script failed");
-#ifdef USB_BLUETOOTH
-                       char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
-                       ret = __execute_command("/usr/bin/hciconfig", argv_down);
-#else
-                       ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
-#endif
+                       if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                               ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
+                               if (ret < 0)
+                                       BT_ERR("Failed to call systemact service");
+                       } else {
+                               ret = __execute_command("/usr/etc/bluetooth/bt-dev-end.sh &", NULL);
+                       }
                        __bt_core_set_status(BT_DEACTIVATED);
                        __bt_core_set_le_status(BT_LE_DEACTIVATED);
                        return -1;
@@ -273,16 +433,33 @@ int _bt_enable_adapter_le(void)
                __bt_core_set_le_status(BT_LE_ACTIVATED);
                g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
        }
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
        ret = _bt_core_start_httpproxy();
-       if (ret < 0) {
+       if (ret < 0)
                BT_ERR("_bt_core_start_httpproxy() failed");
-       }
 #endif
 
        return 0;
 }
 
+#ifdef TIZEN_FEATURE_BT_OTP
+static void _bt_core_stop_otp()
+{
+       GError *error = NULL;
+       GDBusMessage *msg = NULL;
+       GDBusConnection *conn = _bt_core_get_gdbus_connection();
+       msg = g_dbus_message_new_signal(BT_OTP_OBJECT_PATH, BT_OTP_INTERFACE_NAME, BLE_DISABLED);
+       if (!g_dbus_connection_send_message(conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, 0, NULL)) {
+               if (error != NULL) {
+                       BT_ERR("Failed to send BLE_DISABLED signal to OTP : errCode[%x], \
+                                       message[%s]",
+                                       error->code, error->message);
+                       g_clear_error(&error);
+               }
+       }
+}
+#endif
+
 int _bt_disable_adapter_le(void)
 {
        BT_DBG("+");
@@ -294,21 +471,28 @@ int _bt_disable_adapter_le(void)
        retv_if(le_status == BT_LE_DEACTIVATED, 0);
        retv_if(le_status == BT_LE_DEACTIVATING, -1);
 
-#ifdef HPS_FEATURE
+#ifdef TIZEN_FEATURE_BT_HPS
        _bt_core_stop_httpproxy();
 #endif
 
+#ifdef TIZEN_FEATURE_BT_OTP
+       _bt_core_stop_otp();
+#endif
+
        status = _bt_core_get_status();
        BT_DBG("status : %d", status);
 
        if (status == BT_DEACTIVATED) {
                __bt_core_set_le_status(BT_LE_DEACTIVATING);
-#ifdef USB_BLUETOOTH
-               char *argv_down[] = {"/usr/bin/hciconfig", "/usr/bin/hciconfig", "hci0", "down", NULL};
-               if (__execute_command("/usr/bin/hciconfig", argv_down) < 0) {
-#else
-               if (__execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL) < 0) {
-#endif
+               int ret;
+               if (TIZEN_FEATURE_BT_USB_DONGLE) {
+                       ret = __bt_call_systemact_service(BT_SYSTEMACT_HCI_DOWN);
+                       if (ret < 0)
+                               BT_ERR("Failed to call systemact service");
+               } else {
+                       ret = __execute_command("/usr/etc/bluetooth/bt-stack-down.sh", NULL);
+               }
+               if (ret < 0) {
                        BT_ERR("running script failed");
                        __bt_core_set_le_status(BT_LE_ACTIVATED);
                        return -1;
@@ -345,14 +529,16 @@ int _bt_core_service_request_adapter(int service_function)
        return ret;
 }
 
-static gboolean __bt_core_check_the_adapter_path(GDBusConnection *conn)
+static gboolean __bt_core_check_the_adapter_path(void)
 {
        GError *err = NULL;
        GDBusProxy *manager_proxy = NULL;
        GVariant *result = NULL;
        char *adapter_path = NULL;
+       GDBusConnection *conn = NULL;
 
-       if(conn == NULL)
+       conn = _bt_core_get_gdbus_connection();
+       if (conn == NULL)
                return FALSE;
 
        manager_proxy =  g_dbus_proxy_new_sync(conn,
@@ -373,7 +559,7 @@ static gboolean __bt_core_check_the_adapter_path(GDBusConnection *conn)
        }
 
        result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
-                       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+                       G_DBUS_CALL_FLAGS_NONE, 1000, NULL, &err);
        if (!result) {
                if (err != NULL) {
                        BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
@@ -418,7 +604,7 @@ void _bt_core_update_status(void)
        int bt_le_status = VCONFKEY_BT_LE_STATUS_OFF;
        gboolean ret = FALSE;
 
-       ret = __bt_core_check_the_adapter_path(_bt_core_get_gdbus_connection());
+       ret = __bt_core_check_the_adapter_path();
        BT_INFO("check the real status of bt_adapter");
 
        if (ret != TRUE) {
@@ -432,9 +618,9 @@ void _bt_core_update_status(void)
 
                BT_INFO("bt_status = %d, bt_le_status = %d", bt_status, bt_le_status);
 
-               if(bt_status & VCONFKEY_BT_STATUS_ON)
+               if (bt_status & VCONFKEY_BT_STATUS_ON)
                        __bt_core_set_status(BT_ACTIVATED);
-               if(bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
+               if (bt_le_status & VCONFKEY_BT_LE_STATUS_ON)
                        __bt_core_set_le_status(BT_ACTIVATED);
        }
 }
@@ -454,26 +640,43 @@ gboolean _bt_core_enable_adapter(void)
                return TRUE;
 }
 
+static gboolean __bt_core_terminate_cb(gpointer data)
+{
+       _bt_core_terminate();
+
+       return FALSE;
+}
+
 gboolean _bt_core_disable_adapter(void)
 {
        int ret;
+       gboolean adapter_state;
 
        _bt_set_flightmode_request(FALSE);
-       if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0) != 0)
-               BT_ERR("Set vconf failed");
+
+       adapter_state = __bt_core_check_the_adapter_path();
+       if (adapter_state == FALSE)
+               BT_INFO("Default adapter is NOT normal.");
 
        ret = _bt_disable_adapter();
-       if (ret < 0)
-               return FALSE;
-       else
+       if (adapter_state == FALSE) {
+               g_timeout_add(BT_CORE_IDLE_TERM_TIME,
+                             __bt_core_terminate_cb, NULL);
                return TRUE;
+       } else {
+               if (ret < 0)
+                       return FALSE;
+               else
+                       return TRUE;
+       }
 }
 
 gboolean _bt_core_recover_adapter(void)
 {
        int ret;
+#if 0
        int ret_le;
-
+#endif
        BT_INFO_C("Recover bt adapter");
 
        _bt_set_flightmode_request(FALSE);
@@ -486,20 +689,27 @@ gboolean _bt_core_recover_adapter(void)
 
        if (_bt_core_get_status() == BT_ACTIVATED) {
                _bt_core_set_bt_status(BT_RECOVERY_MODE, 1);
+#ifdef TIZEN_FEATURE_BUSACT
                _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
+#endif
        }
        if (_bt_core_get_le_status() == BT_LE_ACTIVATED) {
                _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 1);
+#ifdef TIZEN_FEATURE_BUSACT
                _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
+#endif
        }
 
        ret = _bt_disable_adapter();
        if (ret < 0)
                BT_ERR("_bt_disable_adapter() failed");
+
+/* In platform, don't seperate BR/EDR and LE status */
+#if 0
        ret_le = _bt_disable_adapter_le();
        if (ret_le < 0)
                BT_ERR("_bt_disable_adapter_le() failed");
-
+#endif
        return TRUE;
 }
 
@@ -530,10 +740,10 @@ gboolean _bt_core_disable_adapter_le(void)
 gboolean __bt_core_reset_adapter(void)
 {
        /* Forcely terminate */
-       if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0) {
+       if (__execute_command("/usr/etc/bluetooth/bt-reset-env.sh", NULL) < 0)
                BT_ERR("running script failed");
-       }
-       _bt_core_terminate();
+
+       g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
 
        return TRUE;
 }
@@ -542,6 +752,8 @@ static gboolean __bt_core_enable_core_timeout_cb(gpointer data)
 {
        BT_DBG("+");
 
+       core_enable_timer_id = 0;
+
        _bt_core_init_vconf_value();
 
        return FALSE;
@@ -553,20 +765,62 @@ gboolean _bt_core_enable_core(void)
 
        _bt_core_update_status();
 
-       g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
+       if (core_enable_timer_id > 0)
+               g_source_remove(core_enable_timer_id);
+
+       core_enable_timer_id = g_timeout_add(200, (GSourceFunc)__bt_core_enable_core_timeout_cb, NULL);
 
        BT_DBG("-");
        return TRUE;
 }
 
+gboolean _bt_core_set_transfer_value(gboolean value)
+{
+       const char *event_val = NULL;
+       gboolean ret = TRUE;
+
+       BT_DBG("value: %d", value);
+
+       event_val = value ? EVT_VAL_BT_TRANSFERING : EVT_VAL_BT_NON_TRANSFERING;
+
+       if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_TRANSFERING_STATE,
+                                               event_val) != ES_R_OK) {
+               BT_ERR("Fail to set BT state value");
+               ret = FALSE;
+       }
+
+       if (ret == FALSE || value == FALSE) {
+               BT_DBG("Terminate bt-core process");
+               g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
+       }
+
+       BT_DBG("-");
+       return ret;
+}
+
 gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
 {
+       int ret;
+       BT_DBG("Test item : %s", type);
 
+#ifdef TIZEN_FEATURE_ACTD
+       if (g_strcmp0(type, "Enable_RF_Test") == 0) {
+               ret = __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_ON);
+               if (ret < 0)
+                       BT_ERR("Failed to call systemact service");
+       } else if (g_strcmp0(type, "Disable_RF_Test") == 0) {
+               ret = __bt_call_systemact_service(BT_SYSTEMACT_EDUTM_OFF);
+               if (ret < 0)
+                       BT_ERR("Failed to call systemact service");
+       } else {
+               BT_DBG("Terminate bt-core process");
+               g_timeout_add(BT_CORE_IDLE_TERM_TIME, __bt_core_idle_terminate, NULL);
+               return FALSE;
+       }
+#else
        char *cmd = NULL;
        char *arg_list[3] = { NULL, NULL, NULL };
 
-       BT_DBG("Test item : %s", type);
-
        if (g_strcmp0(type, "Enable_RF_Test") == 0) {
                cmd = "/usr/etc/bluetooth/bt-edutm-on.sh";
                arg_list[0] = "bt-edutm-on.sh";
@@ -588,16 +842,14 @@ gboolean _bt_core_factory_test_mode(const char *type, const char *arg)
                arg_list[0] = "bt-enable-rf-channel.sh";
                arg_list[1] = (char *)arg;
        } else {
-               _bt_core_terminate();
                return FALSE;
        }
 
        BT_DBG("Run %s", cmd);
-       if (__execute_command(cmd, arg_list) < 0) {
+       if (__execute_command(cmd, arg_list) < 0)
                BT_ERR("running script failed");
-       }
+#endif
 
-       _bt_core_terminate();
        return TRUE;
 }
 
@@ -640,13 +892,15 @@ static gboolean __bt_core_recovery_cb(gpointer data)
                if (ret < 0)
                        BT_ERR("_bt_enable_adapter() failed");
        }
+
+#if 0
        if (_bt_core_get_bt_le_status(BT_RECOVERY_MODE) == 1) {
                _bt_core_set_bt_le_status(BT_RECOVERY_MODE, 0);
                ret = _bt_enable_adapter_le();
                if (ret < 0)
                        BT_ERR("_bt_enable_adapter_le() failed");
        }
-
+#endif
        is_recovery_mode = FALSE;
 
        BT_DBG("-");
@@ -703,7 +957,9 @@ static gboolean __bt_core_disable_timeout_cb(gpointer data)
                if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_status_before_mode) == 0)
                        _bt_core_set_bt_status(BT_FLIGHT_MODE, bt_status_before_mode);
 
+#ifdef TIZEN_FEATURE_BUSACT
                _bt_core_service_request_adapter(BT_DISABLE_ADAPTER);
+#endif
                _bt_disable_adapter();
        }
 
@@ -713,7 +969,9 @@ static gboolean __bt_core_disable_timeout_cb(gpointer data)
                if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_le_status_before_mode) == 0)
                        _bt_core_set_bt_le_status(BT_FLIGHT_MODE, bt_le_status_before_mode);
 
+#ifdef TIZEN_FEATURE_BUSACT
                _bt_core_service_request_adapter(BT_DISABLE_ADAPTER_LE);
+#endif
                _bt_disable_adapter_le();
        }
 
@@ -764,6 +1022,15 @@ void _bt_core_adapter_added_cb(void)
                return;
        }
        _bt_set_flightmode_request(FALSE);
+
+       if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
+                                               EVT_VAL_BT_ON) != ES_R_OK)
+               BT_ERR("Fail to set BT state value");
+
+       if (__bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
+                                               EVT_VAL_BT_LE_ON) != ES_R_OK)
+               BT_ERR("Fail to set BT LE state value");
+
        _bt_core_terminate();
 }
 
@@ -774,7 +1041,7 @@ void _bt_core_adapter_removed_cb(void)
        gboolean flight_mode_status;
        static int timer_id = -1;
 
-       BT_DBG("");
+       BT_DBG("is_recovery_mode: %d", is_recovery_mode);
 
        __bt_core_set_status(BT_DEACTIVATED);
        __bt_core_set_le_status(BT_LE_DEACTIVATED);
@@ -791,8 +1058,7 @@ void _bt_core_adapter_removed_cb(void)
                                                EVT_VAL_BT_LE_OFF) != ES_R_OK)
                BT_ERR("Fail to set value");
 
-       if (is_recovery_mode == TRUE)
-       {
+       if (is_recovery_mode == TRUE) {
                if (timer_id < 0)
                        timer_id = g_timeout_add(2000, (GSourceFunc)__bt_core_recovery_cb, NULL);
                return;
@@ -814,7 +1080,7 @@ void _bt_core_adapter_removed_cb(void)
        }
        _bt_set_flightmode_request(FALSE);
 
-       if (flight_mode_value == 1 || power_saving_mode == 1){
+       if (flight_mode_value == 1 || power_saving_mode == 1) {
                BT_DBG("Bt Core not terminated");
                return;
        }