Fix _bt_dpm_get_bluetooth_limited_discoverable_state condition
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-adapter.c
index ee04807..40c888d 100644 (file)
@@ -22,9 +22,6 @@
 #include <string.h>
 #include <vconf.h>
 #include <syspopup_caller.h>
-#include <aul.h>
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus.h>
 #include <bundle.h>
 #include <eventsystem.h>
 #include <bundle_internal.h>
 #include "bt-service-network.h"
 #include "bt-service-obex-server.h"
 #include "bt-service-opp-client.h"
+#include "bt-service-map-client.h"
 #include "bt-service-agent.h"
 #include "bt-service-main.h"
 #include "bt-service-avrcp.h"
 #include "bt-service-device.h"
-#ifdef TIZEN_DPM_ENABLE
+#ifdef TIZEN_FEATURE_BT_DPM
 #include "bt-service-dpm.h"
 #endif
 
@@ -67,9 +65,12 @@ typedef struct {
        GList *g_alarm_list;
 } bt_service_alarm_mgr_t;
 
+#define BT_RECOVERY_MAX_COUNT 3
+
 static bt_service_alarm_mgr_t alarm_mgr = {0, };
 
 static gboolean is_discovering;
+static gboolean discovery_req;
 static gboolean cancel_by_user;
 static bt_status_t adapter_status = BT_DEACTIVATED;
 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
@@ -78,6 +79,8 @@ static void *adapter_agent = NULL;
 static GDBusProxy *core_proxy = NULL;
 static guint timer_id = 0;
 static guint le_timer_id = 0;
+static int recovery_cnt = BT_RECOVERY_MAX_COUNT;
+static guint recovery_timer = 0;
 
 static uint status_reg_id;
 
@@ -87,6 +90,9 @@ static uint status_reg_id;
 
 #define BT_DISABLE_TIME 500 /* 500 ms */
 
+#define BT_RECOVERY_TIME_W 2000 /*2 sec*/
+#define BT_RECOVERY_TIME 5000 /*5 sec*/
+
 static int alarm_cb(alarm_id_t alarm_id, void* user_param);
 static void alarm_data_free(void *data);
 
@@ -95,7 +101,7 @@ GDBusProxy *_bt_init_core_proxy(void)
        GDBusProxy *proxy;
        GDBusConnection *conn;
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        if (!conn)
                return NULL;
 
@@ -116,7 +122,7 @@ GDBusProxy *_bt_init_core_proxy(void)
 
 static GDBusProxy *__bt_get_core_proxy(void)
 {
-       return (core_proxy) ? core_proxy : _bt_init_core_proxy();
+       return (core_proxy) ? core_proxy : _bt_init_core_proxy();
 }
 
 static gboolean __bt_is_factory_test_mode(void)
@@ -156,10 +162,10 @@ static gboolean __bt_timeout_handler(gpointer user_data)
                visible_timer.event_id = 0;
                visible_timer.timeout = 0;
 
-#ifndef TIZEN_PROFILE_WEARABLE
-               if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
-                       BT_ERR("Set vconf failed\n");
-#endif
+               if (!TIZEN_PROFILE_WEARABLE) {
+                       if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
+                               BT_ERR("Set vconf failed\n");
+               }
                return FALSE;
        }
 
@@ -183,14 +189,16 @@ static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
                visible_timer.event_id = 0;
                visible_timer.timeout = 0;
 
-#ifndef TIZEN_PROFILE_WEARABLE
-               if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
-                       BT_ERR("Set vconf failed\n");
-#endif
+               if (!TIZEN_PROFILE_WEARABLE) {
+                       if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
+                               BT_ERR("Set vconf failed\n");
+               }
        }
+
        /* Switch Off visibility in Bluez */
        _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
        visible_timer.alarm_id = 0;
+
        return 0;
 }
 
@@ -207,39 +215,60 @@ static void __bt_visibility_alarm_remove()
        }
 }
 
-int __bt_set_visible_time(int timeout)
+int _bt_start_visibility_timer(int timeout)
 {
        int result;
-       alarm_id_t alarm_id;
-
-       __bt_visibility_alarm_remove();
+       alarm_id_t alarm_id = 0;
+#ifdef TIZEN_FEATURE_BT_DPM
+       int discoverable_state = DPM_BT_ERROR;
+#endif
 
-       visible_timer.timeout = timeout;
+       if (timeout <= 0) {
+               BT_ERR("Invalid timeout : %d", timeout);
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+       }
 
-#ifndef TIZEN_PROFILE_WEARABLE
-#ifdef TIZEN_DPM_ENABLE
-       if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
-#endif
-       if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
-               BT_ERR("Set vconf failed");
-#ifdef TIZEN_DPM_ENABLE
+#ifdef TIZEN_FEATURE_BT_DPM
+       _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
+       if (discoverable_state == DPM_RESTRICTED) {
+               BT_ERR("Limited discoverable mode is not allowed");
+               return BLUETOOTH_ERROR_ACCESS_DENIED;
        }
 #endif
-#endif
 
+       __bt_visibility_alarm_remove();
 
-       if (timeout <= 0)
-               return BLUETOOTH_ERROR_NONE;
-
-       result = _bt_service_set_alarm(visible_timer.timeout,
-                                               __bt_visibility_alarm_cb, NULL, &alarm_id);
-       if (result != BLUETOOTH_ERROR_NONE)
+       result = _bt_service_set_alarm(timeout, __bt_visibility_alarm_cb, NULL,
+                                      &alarm_id);
+       if (result != BLUETOOTH_ERROR_NONE) {
+               BT_ERR("_bt_service_set_alarm is failed : 0x%X", result);
                return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       if (!TIZEN_PROFILE_WEARABLE) {
+               if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
+                       BT_ERR("Set vconf failed");
+       }
+
+       visible_timer.timeout = timeout;
        visible_timer.alarm_id = alarm_id;
        /* Take start time */
        time(&(visible_timer.start_time));
-       visible_timer.event_id = g_timeout_add_seconds(1,
-                               __bt_timeout_handler, NULL);
+       visible_timer.event_id = g_timeout_add_seconds(1, __bt_timeout_handler, NULL);
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_stop_visibility_timer(void)
+{
+       __bt_visibility_alarm_remove();
+
+       visible_timer.timeout = 0;
+
+       if (!TIZEN_PROFILE_WEARABLE) {
+               if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
+                       BT_ERR("Set vconf failed");
+       }
 
        return BLUETOOTH_ERROR_NONE;
 }
@@ -282,6 +311,7 @@ static int __bt_get_bonded_device_info(gchar *device_path,
        GDBusProxy *device_proxy;
        gchar *address = NULL;
        gchar *name = NULL;
+       gchar *alias = NULL;
        unsigned int cod = 0;
        gint rssi = 0;
        gboolean trust = FALSE;
@@ -300,7 +330,7 @@ static int __bt_get_bonded_device_info(gchar *device_path,
        BT_CHECK_PARAMETER(device_path, return);
        BT_CHECK_PARAMETER(dev_info, return);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
@@ -340,12 +370,9 @@ static int __bt_get_bonded_device_info(gchar *device_path,
                } else if (!g_strcmp0(key, "Address")) {
                        g_variant_get(value, "s", &address);
                } else if (!g_strcmp0(key, "Alias")) {
-                       g_variant_get(value, "s", &name);
+                       g_variant_get(value, "s", &alias);
                } else if (!g_strcmp0(key, "Name")) {
-                       if (!name)
-                               g_variant_get(value, "s", &name);
-               } else if(!g_strcmp0(key, "IsAliasSet")) {
-                       dev_info->is_alias_set = g_variant_get_boolean(value);
+                       g_variant_get(value, "s", &name);
                } else if (!g_strcmp0(key, "Class")) {
                        cod = g_variant_get_uint32(value);
                } else if (!g_strcmp0(key, "Connected")) {
@@ -361,9 +388,9 @@ static int __bt_get_bonded_device_info(gchar *device_path,
                } else if (!g_strcmp0(key, "ManufacturerData")) {
                        manufacturer_data = g_byte_array_new();
                        g_variant_get(value, "ay", &char_value_iter);
-                       while (g_variant_iter_loop(char_value_iter, "y",  &char_value)) {
+                       while (g_variant_iter_loop(char_value_iter, "y",  &char_value))
                                g_byte_array_append(manufacturer_data, &char_value, 1);
-                       }
+
                        g_variant_iter_free(char_value_iter);
 
                        if (manufacturer_data) {
@@ -383,6 +410,7 @@ static int __bt_get_bonded_device_info(gchar *device_path,
 
        if ((paired == FALSE) && (trust == FALSE)) {
                g_free(address);
+               g_free(alias);
                g_free(name);
                return BLUETOOTH_ERROR_NOT_PAIRED;
        }
@@ -392,7 +420,7 @@ static int __bt_get_bonded_device_info(gchar *device_path,
 
        _bt_divide_device_class(&dev_info->device_class, cod);
 
-       g_strlcpy(dev_info->device_name.name, name,
+       g_strlcpy(dev_info->device_name.name, alias ? alias : name,
                        BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
 
        dev_info->rssi = rssi;
@@ -401,6 +429,7 @@ static int __bt_get_bonded_device_info(gchar *device_path,
        dev_info->connected = connected;
        ret = BLUETOOTH_ERROR_NONE;
        g_free(address);
+       g_free(alias);
        g_free(name);
 
        return ret;
@@ -409,6 +438,7 @@ static int __bt_get_bonded_device_info(gchar *device_path,
 void _bt_set_discovery_status(gboolean mode)
 {
        is_discovering = mode;
+       discovery_req = FALSE;
 }
 
 void _bt_set_cancel_by_user(gboolean value)
@@ -454,64 +484,25 @@ static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
        char *phone_name = NULL;
        char *ptr = NULL;
 
-       if (node == NULL)
-               return;
+       ret_if(node == NULL);
 
        if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
                phone_name = vconf_keynode_get_str(node);
 
                if (phone_name && strlen(phone_name) != 0) {
-                        if (!g_utf8_validate(phone_name, -1,
+                       if (!g_utf8_validate(phone_name, -1,
                                                        (const char **)&ptr))
-                                *ptr = '\0';
+                                       *ptr = '\0';
 
+                       BT_INFO("device_name is changed to %s", phone_name);
                        _bt_set_local_name(phone_name);
-               }
-       }
-}
-
-#ifdef TIZEN_PROFILE_MOBILE
-static void __bt_set_visible_mode(void)
-{
-       int timeout = 0;
-
-       if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
-                BT_ERR("Fail to get the timeout value");
-
-#ifdef TIZEN_DPM_ENABLE
-       if (timeout == -1 ||
-       _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
-               if (_bt_set_discoverable_mode(
-                       BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
-                       timeout) != BLUETOOTH_ERROR_NONE) {
-                       if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
-                               BT_ERR("Set vconf failed");
-               }
-       } else {
-               if (_bt_set_discoverable_mode(
-                       BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
-                       timeout) != BLUETOOTH_ERROR_NONE) {
-                               BT_ERR("Set connectable mode failed");
-               }
-       }
-#else
-       if (timeout == -1) {
-               if (_bt_set_discoverable_mode(
-                       BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
-                       timeout) != BLUETOOTH_ERROR_NONE) {
-                       if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
-                               BT_ERR("Set vconf failed");
+               } else {
+                       BT_ERR("phone_name is NOT valid");
                }
        } else {
-               if (_bt_set_discoverable_mode(
-                       BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE,
-                       timeout) != BLUETOOTH_ERROR_NONE) {
-                               BT_ERR("Set connectable mode failed");
-               }
+               BT_ERR("vconf type is NOT string");
        }
-#endif
 }
-#endif
 
 static void __bt_set_local_name(void)
 {
@@ -555,15 +546,16 @@ static int __bt_set_enabled(void)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-#ifdef TIZEN_PROFILE_MOBILE
-       __bt_set_visible_mode();
-#else
-#ifdef TIZEN_TV
-       if (_bt_set_discoverable_mode(
-               BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0) != BLUETOOTH_ERROR_NONE)
-                       BT_ERR("Fail to set discoverable mode");
-#endif
-#endif
+       if (TIZEN_PROFILE_MOBILE || TIZEN_PROFILE_IVI) {
+               /* BT setting UI will control Mobile's visible mode. So in the FRWK...set the visible mode as off: */
+               if (_bt_set_discoverable_mode(
+                       BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0) != BLUETOOTH_ERROR_NONE)
+                               BT_ERR("Set connectable mode failed");
+       } else if (TIZEN_PROFILE_TV) {
+               if (_bt_set_discoverable_mode(
+                       BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0) != BLUETOOTH_ERROR_NONE)
+                               BT_ERR("Fail to set discoverable mode");
+       }
 
        /* Update Bluetooth Status to notify other modules */
        if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
@@ -585,6 +577,7 @@ static int __bt_set_enabled(void)
 #endif
 
        __bt_set_local_name();
+       _bt_set_discovery_status(FALSE);
 
        return BLUETOOTH_ERROR_NONE;
 }
@@ -625,12 +618,7 @@ void _bt_set_disabled(int result)
 
        _bt_cancel_queued_transfers();
        _bt_adapter_set_status(BT_DEACTIVATED);
-
-       if (_bt_adapter_get_le_status() != BT_LE_DEACTIVATED) {
-               /* Send disabled event */
-               _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
-                               g_variant_new("(i)", result));
-       }
+       _bt_set_discovery_status(FALSE);
 
        BT_INFO("Adapter disabled");
 }
@@ -695,7 +683,7 @@ void _bt_set_le_disabled(int result)
 
        /* Send disabled event */
        _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_DISABLED,
-                       g_variant_new_int32(result));
+                                               g_variant_new("(i)", result));
 }
 
 void *_bt_get_adapter_agent(void)
@@ -734,7 +722,6 @@ int _bt_enable_core(void)
        return BLUETOOTH_ERROR_NONE;
 }
 
-#if defined(TIZEN_FEATURE_FLIGHTMODE_ENABLED) || (!defined(TIZEN_PROFILE_WEARABLE))
 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
 {
        gboolean flight_mode = FALSE;
@@ -762,40 +749,39 @@ static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
 
        _bt_enable_core();
 }
-#endif
 
 void _bt_service_register_vconf_handler(void)
 {
        BT_DBG("+");
 
-#ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
-       if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
-                       (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
-               BT_ERR("Unable to register key handler");
-#else
-       BT_DBG("Telephony is disabled");
-#endif
+       if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
+               if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
+                               (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
+                       BT_ERR("Unable to register key handler");
+       } else {
+               BT_DBG("Telephony is disabled");
+       }
 
-#ifndef TIZEN_PROFILE_WEARABLE
-       if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
-                       (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
-               BT_ERR("Unable to register key handler");
-#endif
+       if (!TIZEN_PROFILE_WEARABLE) {
+               if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
+                               (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
+                       BT_ERR("Unable to register key handler");
+       }
 }
 
 void _bt_service_unregister_vconf_handler(void)
 {
        BT_DBG("+");
 
-#ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
-       vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
-                       (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
-#endif
+       if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
+               vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
+                               (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
+       }
 
-#ifndef TIZEN_PROFILE_WEARABLE
-       vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
-                       (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
-#endif
+       if (!TIZEN_PROFILE_WEARABLE) {
+               vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
+                               (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
+       }
 }
 
 static void __bt_state_event_handler(const char *event_name, bundle *data, void *user_data)
@@ -811,6 +797,34 @@ static void __bt_state_event_handler(const char *event_name, bundle *data, void
        BT_DBG("bt_state: (%s)", bt_le_status);
 }
 
+static gboolean __bt_adapter_recovery_cb(gpointer data)
+{
+       int ret = 0;
+
+       BT_DBG("+");
+
+       recovery_timer = 0;
+
+       _bt_service_initialize();
+
+       ret = _bt_enable_adapter_check_status();
+       if (ret == BLUETOOTH_ERROR_NONE) {
+               ret = _bt_enable_adapter();
+               if (ret < 0)
+                       BT_ERR("_bt_enable_adapter() failed");
+
+               ret = _bt_enable_adapter_le();
+               if (ret < 0)
+                       BT_ERR("_bt_enable_adapter_le() failed");
+       }
+
+       recovery_cnt--;
+
+       BT_DBG("-");
+
+       return FALSE;
+}
+
 void _bt_handle_adapter_added(void)
 {
        BT_DBG("+");
@@ -825,15 +839,26 @@ void _bt_handle_adapter_added(void)
        }
 */
 
+       recovery_cnt = -1;
+
        status = _bt_adapter_get_status();
        le_status = _bt_adapter_get_le_status();
-       BT_DBG("status : %d", status);
-       BT_DBG("le_status : %d", le_status);
-
-       adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
-       if (!adapter_agent) {
-               BT_ERR("Fail to register agent");
-               return;
+       BT_INFO("status : %d", status);
+       BT_INFO("le_status : %d", le_status);
+       if (!TIZEN_FEATURE_BT_USB_DONGLE) {
+               adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
+               if (!adapter_agent) {
+                       BT_ERR("Fail to register agent");
+                       return;
+               }
+       } else {
+               if (adapter_agent == NULL) {
+                       adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
+                       if (!adapter_agent) {
+                               BT_ERR("Fail to register agent");
+                               return;
+                       }
+               }
        }
 
        if (_bt_register_media_player() != BLUETOOTH_ERROR_NONE)
@@ -864,8 +889,6 @@ void _bt_handle_adapter_added(void)
                _bt_adapter_set_status(BT_ACTIVATED);
        }
 
-       _bt_service_register_vconf_handler();
-
        /* eventsystem */
        if (eventsystem_register_event(SYS_EVENT_BT_STATE, &status_reg_id,
                        (eventsystem_handler)__bt_state_event_handler, NULL) != ES_R_OK) {
@@ -894,21 +917,56 @@ void _bt_handle_adapter_removed(void)
 
        ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
                                (vconf_callback_fn)__bt_phone_name_changed_cb);
-       if (0 != ret) {
+       if (0 != ret)
                ERR("vconf_ignore_key_changed failed\n");
-       }
 
-       _bt_destroy_agent(adapter_agent);
-       adapter_agent = NULL;
+       /* unregister all the services/servers/profiles registered on bluez-adapter
+       once adapter is removed, reinitializing of the state-varaibles becomes
+       a problem */
+       if (_bt_unregister_obex_server() != BLUETOOTH_ERROR_NONE)
+               BT_ERR("Fail to unregister obex server");
 
-#ifndef USB_BLUETOOTH
-       _bt_reliable_terminate_service(NULL);
-#endif
+       if (_bt_unregister_media_player() != BLUETOOTH_ERROR_NONE)
+               BT_ERR("Fail to unregister media player");
 
-       if (eventsystem_unregister_event(status_reg_id) != ES_R_OK) {
-               BT_ERR("Fail to unregister system event");
+       /* Other unregister APIs should be placed here */
+
+       if (!TIZEN_FEATURE_BT_USB_DONGLE) {
+               _bt_destroy_agent(adapter_agent);
+               adapter_agent = NULL;
+
+               if (recovery_cnt > 0) {
+                       /* Send disabled event */
+                       _bt_set_disabled(BLUETOOTH_ERROR_NONE);
+                       _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
+                               g_variant_new("(i)", BLUETOOTH_ERROR_NONE));
+
+                       if (recovery_timer > 0)
+                               g_source_remove(recovery_timer);
+
+                       if (TIZEN_PROFILE_WEARABLE) {
+                               recovery_timer = g_timeout_add(BT_RECOVERY_TIME_W,
+                                               (GSourceFunc)__bt_adapter_recovery_cb, NULL);
+                       } else {
+                               recovery_timer = g_timeout_add(BT_RECOVERY_TIME,
+                                               (GSourceFunc)__bt_adapter_recovery_cb, NULL);
+                       }
+
+                       if (eventsystem_unregister_event(status_reg_id) != ES_R_OK)
+                               BT_ERR("Fail to unregister system event");
+                       return;
+               }
+
+               if (recovery_timer == 0)
+                       _bt_reliable_terminate_service(NULL);
+       } else {
+               _bt_set_disabled(BLUETOOTH_ERROR_NONE);
+               _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
+                               g_variant_new("(i)", BLUETOOTH_ERROR_NONE));
        }
 
+       if (eventsystem_unregister_event(status_reg_id) != ES_R_OK)
+               BT_ERR("Fail to unregister system event");
 }
 
 static gboolean __bt_enable_timeout_cb(gpointer user_data)
@@ -949,9 +1007,24 @@ static gboolean __bt_enable_timeout_cb(gpointer user_data)
        g_variant_unref(result);
        _bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
 
-#ifndef USB_BLUETOOTH
-       _bt_terminate_service(NULL);
-#endif
+       if (recovery_cnt > 0) {
+               BT_ERR("Try recovery again(remain:%d)", recovery_cnt);
+               if (recovery_timer > 0)
+                       g_source_remove(recovery_timer);
+
+               if (TIZEN_PROFILE_WEARABLE) {
+                       recovery_timer = g_timeout_add(BT_RECOVERY_TIME_W,
+                               (GSourceFunc)__bt_adapter_recovery_cb, NULL);
+               } else {
+                       recovery_timer = g_timeout_add(BT_RECOVERY_TIME,
+                               (GSourceFunc)__bt_adapter_recovery_cb, NULL);
+               }
+
+               return FALSE;
+       }
+
+       if (!TIZEN_FEATURE_BT_USB_DONGLE)
+               _bt_terminate_service(NULL);
 
        return FALSE;
 }
@@ -991,9 +1064,9 @@ static gboolean __bt_enable_le_timeout_cb(gpointer user_data)
        }
 
        g_variant_unref(result);
-       _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
 
-       _bt_set_le_disabled(BLUETOOTH_ERROR_TIMEOUT);
+       if (_bt_adapter_get_le_status() != BT_LE_DEACTIVATED)
+               _bt_set_le_disabled(BLUETOOTH_ERROR_TIMEOUT);
 
        if (_bt_adapter_get_status() == BT_DEACTIVATED)
                _bt_terminate_service(NULL);
@@ -1027,7 +1100,6 @@ void _bt_adapter_start_enable_timer(void)
        return;
 }
 
-#ifdef TIZEN_TV
 static gboolean __bt_adapter_enabled_cb(gpointer user_data)
 {
        BT_DBG("+");
@@ -1037,7 +1109,31 @@ static gboolean __bt_adapter_enabled_cb(gpointer user_data)
 
        return FALSE;
 }
-#endif
+
+int _bt_enable_adapter_check_status(void)
+{
+       bt_status_t status = _bt_adapter_get_status();
+       bt_le_status_t le_status = _bt_adapter_get_le_status();
+
+       BT_DBG("");
+
+       if (status == BT_ACTIVATING) {
+               BT_ERR("Enabling in progress");
+               return BLUETOOTH_ERROR_IN_PROGRESS;
+       }
+
+       if (status == BT_ACTIVATED) {
+               BT_ERR("Already enabled");
+               return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
+       }
+
+       if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
+               BT_ERR("Disabling in progress");
+               return BLUETOOTH_ERROR_DEVICE_BUSY;
+       }
+
+       return BLUETOOTH_ERROR_NONE;
+}
 
 int _bt_enable_adapter(void)
 {
@@ -1067,8 +1163,7 @@ int _bt_enable_adapter(void)
 
        _bt_adapter_set_status(BT_ACTIVATING);
 
-#ifdef TIZEN_TV
-{
+if (TIZEN_PROFILE_TV) {
        int adapter_status = BT_ADAPTER_DISABLED;
 
        if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
@@ -1081,7 +1176,6 @@ int _bt_enable_adapter(void)
                return BLUETOOTH_ERROR_NONE;
        }
 }
-#endif
 
        proxy = __bt_get_core_proxy();
        if (!proxy)
@@ -1090,11 +1184,10 @@ int _bt_enable_adapter(void)
        if (le_status == BT_LE_ACTIVATED) {
                BT_INFO("LE Already enabled. Just turn on PSCAN");
                ret = _bt_set_connectable(TRUE);
-               if (ret == BLUETOOTH_ERROR_NONE) {
+               if (ret == BLUETOOTH_ERROR_NONE)
                        _bt_adapter_set_status(BT_ACTIVATED);
-               } else {
+               else
                        return BLUETOOTH_ERROR_INTERNAL;
-               }
        }
 
        result = g_dbus_proxy_call_sync(proxy, "EnableAdapter",
@@ -1102,8 +1195,8 @@ int _bt_enable_adapter(void)
                                         G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
                                         NULL, &error);
         if (error) {
-                BT_ERR("EnableAdapterLe failed: %s", error->message);
-               _bt_adapter_set_status(BT_DEACTIVATED);
+               BT_ERR("EnableAdapterLe failed: %s", error->message);
+               _bt_adapter_set_status(BT_DEACTIVATED);
                g_clear_error(&error);
                error = NULL;
                result = g_dbus_proxy_call_sync(proxy,
@@ -1120,19 +1213,50 @@ int _bt_enable_adapter(void)
                }
                g_variant_unref(result);
                /* Terminate myself */
-               g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
+               if (!TIZEN_FEATURE_BT_USB_DONGLE)
+                       g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
                return BLUETOOTH_ERROR_INTERNAL;
        }
        g_variant_unref(result);
-       if (le_status == BT_LE_ACTIVATED) {
+       if (le_status == BT_LE_ACTIVATED)
                __bt_set_enabled();
-       } else {
+       else
                _bt_adapter_start_enable_timer();
-       }
 
        return BLUETOOTH_ERROR_NONE;
 }
 
+static gboolean __bt_set_powered(gboolean powered)
+{
+       GDBusProxy *proxy;
+       GError *error = NULL;
+       GVariant *result;
+
+       BT_DBG("");
+
+       proxy = _bt_get_adapter_properties_proxy();
+       retv_if(proxy == NULL, FALSE);
+
+       result = g_dbus_proxy_call_sync(proxy, "Set",
+               g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Powered",
+               g_variant_new("b", powered)),
+               G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
+       if (!result) {
+               if (error != NULL) {
+                       BT_ERR("Failed to set powered property (Error: %s)",
+                       error->message);
+                       g_clear_error(&error);
+               } else {
+                       BT_ERR("Failed to set powered property");
+               }
+               return FALSE;
+       }
+
+       BT_INFO("Set powered [%d]", powered);
+       g_variant_unref(result);
+       return TRUE;
+}
+
 static gboolean __bt_disconnect_all(void)
 {
        int i;
@@ -1150,7 +1274,7 @@ static gboolean __bt_disconnect_all(void)
 
        BT_DBG("");
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
 
        device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
 
@@ -1219,6 +1343,8 @@ static gboolean __bt_set_disabled_timeout_cb(gpointer user_data)
 {
        BT_DBG("");
        _bt_set_disabled(BLUETOOTH_ERROR_NONE);
+       _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
+               g_variant_new("(i)", BLUETOOTH_ERROR_NONE));
 
        return FALSE;
 }
@@ -1282,10 +1408,30 @@ int _bt_disable_cb(void)
        return BLUETOOTH_ERROR_NONE;
 }
 
+int _bt_disable_adapter_check_status(void)
+{
+       bt_status_t status = _bt_adapter_get_status();
+
+       BT_DBG("");
+
+       if (status == BT_DEACTIVATING) {
+               BT_DBG("Disabling in progress");
+               return BLUETOOTH_ERROR_IN_PROGRESS;
+       }
+
+       if (status == BT_DEACTIVATED) {
+               BT_DBG("Already disabled");
+               return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
+       }
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
 int _bt_disable_adapter(void)
 {
        BT_DBG("+");
        int ret;
+       bt_le_status_t le_status;
 
        if (_bt_adapter_get_status() == BT_DEACTIVATING) {
                BT_DBG("Disabling in progress");
@@ -1302,7 +1448,26 @@ int _bt_disable_adapter(void)
                timer_id = 0;
        }
 
-       __bt_disconnect_all();
+       /* unregister all the services/servers/profiles registered on bluez-adapter
+       once adapter is removed, reinitializing of the state-varaibles becomes
+       a problem */
+       if (_bt_unregister_obex_server() != BLUETOOTH_ERROR_NONE)
+               BT_ERR("Fail to unregister obex server");
+
+       if (_bt_unregister_media_player() != BLUETOOTH_ERROR_NONE)
+               BT_ERR("Fail to unregister media player");
+       /* Other unregister APIs should be placed here */
+
+       le_status = _bt_adapter_get_le_status();
+       if (le_status == BT_LE_ACTIVATED && is_le_intended == TRUE) {
+               __bt_disconnect_all();
+       } else {
+               if (le_status == BT_LE_ACTIVATED)
+                       _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
+
+               __bt_set_powered(FALSE);
+       }
+
        ret = _bt_disable_cb();
 
        BT_DBG("-");
@@ -1312,44 +1477,21 @@ int _bt_disable_adapter(void)
 int _bt_recover_adapter(void)
 {
        BT_DBG("+");
-       GDBusProxy *proxy;
-       GVariant *result;
-       GError *error = NULL;
 
        if (_bt_adapter_get_status() == BT_DEACTIVATING) {
-               BT_DBG("Disabling in progress");
+               BT_ERR("Disabling in progress");
                return BLUETOOTH_ERROR_IN_PROGRESS;
        }
 
        if (_bt_adapter_get_status() == BT_DEACTIVATED) {
-               BT_DBG("Already disabled");
+               BT_ERR("Already disabled");
                return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
        }
 
-       _bt_adapter_set_status(BT_DEACTIVATING);
+       recovery_cnt = BT_RECOVERY_MAX_COUNT;
 
-       proxy = __bt_get_core_proxy();
-       retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
-
-       result = g_dbus_proxy_call_sync(proxy,
-                               "RecoverAdapter",
-                               NULL,
-                               G_DBUS_CALL_FLAGS_NONE,
-                               -1,
-                               NULL,
-                               &error);
-
-       if (!result) {
-               if (error != NULL) {
-                       BT_ERR("Failed to RecoverAdapter (Error: %s)", error->message);
-                       g_clear_error(&error);
-               } else
-                       BT_ERR("Failed to RecoverAdapter");
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
-
-       g_variant_unref(result);
-       __bt_disconnect_all();
+       _bt_disable_adapter();
+       _bt_disable_adapter_le();
 
        BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;
@@ -1386,17 +1528,15 @@ int _bt_reset_adapter(void)
 
        g_variant_unref(result);
        /* Terminate myself */
-       if (_bt_adapter_get_status() == BT_DEACTIVATED) {
+       if (_bt_adapter_get_status() == BT_DEACTIVATED)
                g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
-       }
 
        return BLUETOOTH_ERROR_NONE;
 }
 
-#ifndef TIZEN_TV
 int _bt_check_adapter(int *status)
 {
-
+if (!TIZEN_PROFILE_TV) {
        char *adapter_path = NULL;
 
        BT_CHECK_PARAMETER(status, return);
@@ -1411,10 +1551,7 @@ int _bt_check_adapter(int *status)
 
        g_free(adapter_path);
        return BLUETOOTH_ERROR_NONE;
-}
-#else
-int _bt_check_adapter(int *status)
-{
+} else {
        GDBusProxy *proxy;
        GError *error = NULL;
        GVariant *result;
@@ -1457,7 +1594,7 @@ int _bt_check_adapter(int *status)
        g_variant_unref(temp);
        return BLUETOOTH_ERROR_NONE;
 }
-#endif
+}
 
 int _bt_enable_adapter_le(void)
 {
@@ -1480,6 +1617,7 @@ int _bt_enable_adapter_le(void)
 
        if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
                BT_ERR("Disabling in progress");
+               _bt_set_le_intended_status(FALSE);
                return BLUETOOTH_ERROR_DEVICE_BUSY;
        }
 
@@ -1620,11 +1758,10 @@ int _bt_get_local_address(bluetooth_device_address_t *local_address)
        address = g_variant_get_string(temp, NULL);
        BT_DBG("Address:%s", address);
 
-       if (address) {
+       if (address)
                _bt_convert_addr_string_to_type(local_address->addr, address);
-       } else {
+       else
                return BLUETOOTH_ERROR_INTERNAL;
-       }
 
        g_variant_unref(result);
        g_variant_unref(temp);
@@ -1898,20 +2035,29 @@ int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
        GError *error = NULL;
        GDBusProxy *proxy;
        GVariant *result;
+#ifdef TIZEN_FEATURE_BT_DPM
+       int discoverable_state = DPM_BT_ERROR;
+#endif
 
        proxy = _bt_get_adapter_properties_proxy();
 
        retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-#ifdef TIZEN_DPM_ENABLE
+#ifdef TIZEN_FEATURE_BT_DPM
+       _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
        if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
-               _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
-               _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
+           discoverable_state == DPM_RESTRICTED) {
+               if (headed_plugin_info->plugin_headed_enabled)
+                       headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
+
                return BLUETOOTH_ERROR_ACCESS_DENIED;
        }
-       if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
-                _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
-               _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
+
+       if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE &&
+           discoverable_state == DPM_RESTRICTED) {
+               if (headed_plugin_info->plugin_headed_enabled)
+                       headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT");
+
                return BLUETOOTH_ERROR_ACCESS_DENIED;
        }
 #endif
@@ -1992,10 +2138,17 @@ int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-       if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
-               timeout = -1;
-
-       ret = __bt_set_visible_time(timeout);
+       if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
+               ret = _bt_stop_visibility_timer();
+               if (!TIZEN_PROFILE_WEARABLE) {
+                       if (vconf_set_int(BT_FILE_VISIBLE_TIME, -1) != 0)
+                               BT_ERR("Set vconf failed");
+               }
+       } else if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE) {
+               ret = _bt_start_visibility_timer(timeout);
+       } else {
+               ret = _bt_stop_visibility_timer();
+       }
 
        g_variant_unref(result);
 
@@ -2036,7 +2189,7 @@ int _bt_start_discovery(void)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-       is_discovering = TRUE;
+       discovery_req = TRUE;
        cancel_by_user = FALSE;
        /* discovery status will be change in event */
        g_variant_unref(result);
@@ -2084,7 +2237,7 @@ int _bt_start_custom_discovery(bt_discovery_role_type_t role)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-       is_discovering = TRUE;
+       discovery_req = TRUE;
        cancel_by_user = FALSE;
        /* discovery status will be change in event */
        g_variant_unref(result);
@@ -2114,14 +2267,22 @@ int _bt_cancel_discovery(void)
                                &error);
 
        if (!result) {
+               int ret = BLUETOOTH_ERROR_INTERNAL;
                if (error != NULL) {
                        BT_ERR("StopDiscovery failed (Error: %s)", error->message);
+
+                       if (g_strrstr(error->message, "No discovery started"))
+                               ret = BLUETOOTH_ERROR_NOT_IN_OPERATION;
+
                        g_clear_error(&error);
-               } else
+               } else {
                        BT_ERR("StopDiscovery failed");
-               return BLUETOOTH_ERROR_INTERNAL;
+               }
+
+               return ret;
        }
 
+       discovery_req = FALSE;
        cancel_by_user = TRUE;
        /* discovery status will be change in event */
        g_variant_unref(result);
@@ -2130,7 +2291,7 @@ int _bt_cancel_discovery(void)
 
 gboolean _bt_is_discovering(void)
 {
-       return is_discovering;
+       return (is_discovering || discovery_req);
 }
 
 gboolean _bt_is_connectable(void)
@@ -2206,7 +2367,7 @@ int _bt_set_connectable(gboolean is_connectable)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-       BT_INFO("### Set connectable [%d]", is_connectable);
+       BT_INFO_C("### Set connectable [%d]", is_connectable);
        g_variant_unref(result);
        return BLUETOOTH_ERROR_NONE;
 }
@@ -2377,13 +2538,12 @@ static bluetooth_device_info_t *__bt_parse_device_info(GVariantIter *item_iter)
                } else if (strcasecmp(key, "ManufacturerData") == 0) {
                        manufacturer_data = g_byte_array_new();
                        g_variant_get(value, "ay", &char_value_iter);
-                       while (g_variant_iter_loop(char_value_iter, "y",  &char_value)) {
+                       while (g_variant_iter_loop(char_value_iter, "y",  &char_value))
                                g_byte_array_append(manufacturer_data, &char_value, 1);
-                       }
+
                        if (manufacturer_data) {
-                               if (manufacturer_data->len > 0) {
+                               if (manufacturer_data->len > 0)
                                        memcpy(dev_info->manufacturer_data.data, manufacturer_data->data, manufacturer_data->len);
-                               }
                        }
                        g_variant_iter_free(char_value_iter);
                        g_byte_array_free(manufacturer_data, TRUE);
@@ -2439,7 +2599,7 @@ int _bt_get_bonded_devices(GArray **dev_list)
        GVariantIter *iter = NULL;
        GError *error = NULL;
 
-       conn = _bt_get_system_conn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        manager_proxy = _bt_get_manager_proxy();
@@ -2487,7 +2647,7 @@ int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
        GDBusProxy *device_proxy = NULL;
        gboolean is_connected = FALSE;
 
-       conn = _bt_get_system_conn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        manager_proxy = _bt_get_manager_proxy();
@@ -2606,6 +2766,75 @@ int _bt_get_bonded_device_info(bluetooth_device_address_t *device_address,
        return ret;
 }
 
+int _bt_is_alias_set(bluetooth_device_address_t *device_address, gboolean *is_alias_set)
+{
+       char *object_path = NULL;
+       char address[BT_ADDRESS_STRING_SIZE] = { 0 };
+       gboolean alias_set = FALSE;
+
+       GDBusConnection *conn;
+       GDBusProxy *device_proxy;
+       GError *error = NULL;
+       GVariant *result = NULL;
+       GVariant *temp = NULL;
+
+
+       BT_CHECK_PARAMETER(device_address, return);
+       BT_CHECK_PARAMETER(is_alias_set, return);
+
+       _bt_convert_addr_type_to_string(address, device_address->addr);
+
+       object_path = _bt_get_device_object_path(address);
+       retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
+
+       conn = _bt_gdbus_get_system_gconn();
+       if (conn == NULL) {
+               g_free(object_path);
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+                       NULL,
+                       BT_BLUEZ_NAME,
+                       object_path,
+                       BT_PROPERTIES_INTERFACE,
+                       NULL, NULL);
+       if (device_proxy == NULL) {
+               g_free(object_path);
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       result = g_dbus_proxy_call_sync(device_proxy, "Get",
+                       g_variant_new("(ss)", BT_DEVICE_INTERFACE, "IsAliasSet"),
+                       G_DBUS_CALL_FLAGS_NONE,
+                       -1,
+                       NULL,
+                       &error);
+
+       if (!result) {
+               BT_ERR("Error occured in Proxy call");
+               if (error != NULL) {
+                       BT_ERR("Getting is_alias_set property failed: [%s]\n", error->message);
+                       g_error_free(error);
+               }
+               g_object_unref(device_proxy);
+               g_free(object_path);
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       g_variant_get(result, "(v)", &temp);
+       alias_set = g_variant_get_boolean(temp);
+       *is_alias_set = alias_set;
+       BT_DBG("address: [%s] | *is_alias_set: %s", address, *is_alias_set ? "TRUE" : "FALSE");
+       g_variant_unref(temp);
+       g_variant_unref(result);
+       g_object_unref(device_proxy);
+
+       g_free(object_path);
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
 int _bt_get_timeout_value(int *timeout)
 {
        time_t current_time;
@@ -2725,9 +2954,8 @@ int _bt_set_manufacturer_data(bluetooth_manufacturer_data_t *m_data)
 
        builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
 
-       for (i = 0; i < (m_data->data_len) + 2; i++) {
+       for (i = 0; i < (m_data->data_len) + 2; i++)
                g_variant_builder_add(builder, "y", m_data->data[i]);
-       }
 
        val = g_variant_new("(ay)", builder);
 
@@ -2750,9 +2978,8 @@ int _bt_set_manufacturer_data(bluetooth_manufacturer_data_t *m_data)
        }
        builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
 
-       for (i = 0; i < (m_data->data_len) + 2; i++) {
+       for (i = 0; i < (m_data->data_len) + 2; i++)
                g_variant_builder_add(builder, "y", m_data->data[i]);
-       }
 
        val = g_variant_new("(ay)", builder);
 
@@ -2774,19 +3001,19 @@ int _bt_service_set_alarm(int timeout, bt_set_alarm_cb call_back, void *user_dat
        int result = BLUETOOTH_ERROR_NONE;
        bt_service_alarm_t *alarm = NULL;
 
-       if(!call_back || !alarm_id)
+       if (!call_back || !alarm_id)
                return BLUETOOTH_ERROR_INVALID_PARAM;
 
        if (!alarm_mgr.is_alarm_initialized) {
                result = alarmmgr_init("bt-service");
                if (result != 0) {
-                       BT_ERR("Failed to initialize alarm = %d",result);
+                       BT_ERR("Failed to initialize alarm = %d", result);
                        result = BLUETOOTH_ERROR_INTERNAL;
                        goto finish;
                }
                result = alarmmgr_set_cb(alarm_cb, NULL);
                if (result != 0) {
-                       BT_ERR("Failed to set the callback = %d",result);
+                       BT_ERR("Failed to set the callback = %d", result);
                        result = BLUETOOTH_ERROR_INTERNAL;
                        goto finish;
                }
@@ -2794,13 +3021,13 @@ int _bt_service_set_alarm(int timeout, bt_set_alarm_cb call_back, void *user_dat
        }
 
        alarm = g_malloc0(sizeof(bt_service_alarm_t));
-       if(!alarm)
+       if (!alarm)
                return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
 
        result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, timeout,
                                                0, NULL, alarm_id);
        if (result != 0) {
-               BT_ERR("Failed to create alarm error = %d",result);
+               BT_ERR("Failed to create alarm error = %d", result);
                result = BLUETOOTH_ERROR_INTERNAL;
                g_free(alarm);
                goto finish;
@@ -2862,12 +3089,16 @@ int _bt_service_remove_alarm(alarm_id_t alarm_id)
 
 gint compare_alarm(gconstpointer list_data, gconstpointer data)
 {
+#ifdef ARCH64
+       alarm_id_t alarm_id = (alarm_id_t)(uintptr_t)data;
+#else
        alarm_id_t alarm_id = (alarm_id_t)data;
+#endif
        bt_service_alarm_t *p_data = (bt_service_alarm_t *)list_data;
 
-       if (p_data->alarm_id == alarm_id) {
+       if (p_data->alarm_id == alarm_id)
                return 0;
-       }
+
        return 1;
 }
 
@@ -2881,7 +3112,11 @@ static void alarm_data_free(void *data)
 static gboolean _bt_adapter_request_delayed_cb(gpointer user_data)
 {
        int result;
+#ifdef ARCH64
+       int function = (int)(uintptr_t)user_data;
+#else
        int function = (int)user_data;
+#endif
 
        switch (function) {
        case BT_ENABLE_ADAPTER:
@@ -2912,37 +3147,23 @@ static gboolean _bt_adapter_request_delayed_cb(gpointer user_data)
 
 int _bt_adapter_request_delayed(int function)
 {
-       bt_status_t status = _bt_adapter_get_status();
-       bt_le_status_t le_status = _bt_adapter_get_le_status();
+       int ret;
 
        switch (function) {
        case BT_ENABLE_ADAPTER:
-               if (status == BT_ACTIVATING) {
-                       BT_ERR("Enabling in progress");
-                       return BLUETOOTH_ERROR_IN_PROGRESS;
-               }
-
-               if (status == BT_ACTIVATED) {
-                       BT_ERR("Already enabled");
-                       return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
-               }
-
-               if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
-                       BT_ERR("Disabling in progress");
-                       return BLUETOOTH_ERROR_DEVICE_BUSY;
-               }
+               ret = _bt_enable_adapter_check_status();
+               if (ret == BLUETOOTH_ERROR_NONE)
+                       _bt_adapter_set_status(BT_ACTIVATING);
+               else
+                       return ret;
 
                break;
        case BT_DISABLE_ADAPTER:
-               if (status == BT_DEACTIVATING) {
-                       BT_DBG("Disabling in progress");
-                       return BLUETOOTH_ERROR_IN_PROGRESS;
-               }
-
-               if (status == BT_DEACTIVATED) {
-                       BT_DBG("Already disabled");
-                       return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
-               }
+               ret = _bt_disable_adapter_check_status();
+               if (ret == BLUETOOTH_ERROR_NONE)
+                       _bt_adapter_set_status(BT_DEACTIVATING);
+               else
+                       return ret;
 
                break;
        default:
@@ -2950,14 +3171,16 @@ int _bt_adapter_request_delayed(int function)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-       g_idle_add((GSourceFunc)_bt_adapter_request_delayed_cb, (void*)function);
+#ifdef ARCH64
+       g_idle_add((GSourceFunc)_bt_adapter_request_delayed_cb, (void *)(uintptr_t)function);
+#else
+       g_idle_add((GSourceFunc)_bt_adapter_request_delayed_cb, (void *)function);
+#endif
 
        return BLUETOOTH_ERROR_NONE;
 }
 
-#ifdef TIZEN_TV
 int _bt_get_enable_timer_id(void)
 {
        return timer_id;
 }
-#endif