Fix _bt_dpm_get_bluetooth_limited_discoverable_state condition
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-adapter.c
index f9a15b6..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>
@@ -39,6 +36,7 @@
 #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"
@@ -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,7 +79,8 @@ static void *adapter_agent = NULL;
 static GDBusProxy *core_proxy = NULL;
 static guint timer_id = 0;
 static guint le_timer_id = 0;
-static gboolean is_recovery_mode;
+static int recovery_cnt = BT_RECOVERY_MAX_COUNT;
+static guint recovery_timer = 0;
 
 static uint status_reg_id;
 
@@ -88,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);
 
@@ -157,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;
        }
 
@@ -184,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;
 }
 
@@ -208,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();
-
-       visible_timer.timeout = timeout;
-
-#ifndef TIZEN_PROFILE_WEARABLE
+       alarm_id_t alarm_id = 0;
 #ifdef TIZEN_FEATURE_BT_DPM
-       if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
+       int discoverable_state = DPM_BT_ERROR;
 #endif
-       if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
-               BT_ERR("Set vconf failed");
+
+       if (timeout <= 0) {
+               BT_ERR("Invalid timeout : %d", timeout);
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+       }
+
 #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;
 }
@@ -283,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;
@@ -341,10 +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);
+                       g_variant_get(value, "s", &name);
                } else if (!g_strcmp0(key, "Class")) {
                        cod = g_variant_get_uint32(value);
                } else if (!g_strcmp0(key, "Connected")) {
@@ -382,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;
        }
@@ -391,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;
@@ -400,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;
@@ -408,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)
@@ -453,8 +484,7 @@ 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);
@@ -464,8 +494,13 @@ static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
                                                        (const char **)&ptr))
                                        *ptr = '\0';
 
+                       BT_INFO("device_name is changed to %s", phone_name);
                        _bt_set_local_name(phone_name);
+               } else {
+                       BT_ERR("phone_name is NOT valid");
                }
+       } else {
+               BT_ERR("vconf type is NOT string");
        }
 }
 
@@ -511,18 +546,16 @@ static int __bt_set_enabled(void)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-#if defined(TIZEN_PROFILE_MOBILE) || defined(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
-#ifdef TIZEN_PROFILE_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)
@@ -587,16 +620,6 @@ void _bt_set_disabled(int result)
        _bt_adapter_set_status(BT_DEACTIVATED);
        _bt_set_discovery_status(FALSE);
 
-#ifndef TIZEN_FEATURE_BT_USB_DONGLE
-       if (_bt_adapter_get_le_status() != BT_LE_DEACTIVATED) {
-#endif
-               /* Send disabled event */
-               _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
-                               g_variant_new("(i)", result));
-#ifndef TIZEN_FEATURE_BT_USB_DONGLE
-       }
-#endif
-
        BT_INFO("Adapter disabled");
 }
 
@@ -660,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("(i)", result));
+                                               g_variant_new("(i)", result));
 }
 
 void *_bt_get_adapter_agent(void)
@@ -699,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;
@@ -727,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)
@@ -776,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("+");
@@ -790,26 +839,27 @@ 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);
-
-#ifndef 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) {
+       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;
+                       }
+               }
        }
-#endif
 
        if (_bt_register_media_player() != BLUETOOTH_ERROR_NONE)
                BT_ERR("Fail to register media player");
@@ -870,23 +920,50 @@ void _bt_handle_adapter_removed(void)
        if (0 != ret)
                ERR("vconf_ignore_key_changed failed\n");
 
-#ifndef TIZEN_FEATURE_BT_USB_DONGLE
-       _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");
 
-       if (is_recovery_mode == TRUE) {
-               /* Send disabled event */
-               _bt_set_disabled(BLUETOOTH_ERROR_NONE);
+       if (_bt_unregister_media_player() != BLUETOOTH_ERROR_NONE)
+               BT_ERR("Fail to unregister media player");
+
+       /* 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));
 
-               /* Will recover BT by bt-core, so set the mode as activating */
-               _bt_adapter_set_status(BT_ACTIVATING);
-               is_recovery_mode = FALSE;
+                       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_reliable_terminate_service(NULL);
+               _bt_set_disabled(BLUETOOTH_ERROR_NONE);
+               _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
+                               g_variant_new("(i)", BLUETOOTH_ERROR_NONE));
        }
-#else
-       _bt_set_disabled(BLUETOOTH_ERROR_NONE);
-#endif
 
        if (eventsystem_unregister_event(status_reg_id) != ES_R_OK)
                BT_ERR("Fail to unregister system event");
@@ -930,9 +1007,24 @@ static gboolean __bt_enable_timeout_cb(gpointer user_data)
        g_variant_unref(result);
        _bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
 
-#ifndef TIZEN_FEATURE_BT_USB_DONGLE
-       _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;
 }
@@ -972,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);
@@ -1008,7 +1100,6 @@ void _bt_adapter_start_enable_timer(void)
        return;
 }
 
-#ifdef TIZEN_PROFILE_TV
 static gboolean __bt_adapter_enabled_cb(gpointer user_data)
 {
        BT_DBG("+");
@@ -1018,7 +1109,6 @@ static gboolean __bt_adapter_enabled_cb(gpointer user_data)
 
        return FALSE;
 }
-#endif
 
 int _bt_enable_adapter_check_status(void)
 {
@@ -1073,8 +1163,7 @@ int _bt_enable_adapter(void)
 
        _bt_adapter_set_status(BT_ACTIVATING);
 
-#ifdef TIZEN_PROFILE_TV
-{
+if (TIZEN_PROFILE_TV) {
        int adapter_status = BT_ADAPTER_DISABLED;
 
        if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
@@ -1087,7 +1176,6 @@ int _bt_enable_adapter(void)
                return BLUETOOTH_ERROR_NONE;
        }
 }
-#endif
 
        proxy = __bt_get_core_proxy();
        if (!proxy)
@@ -1124,10 +1212,9 @@ int _bt_enable_adapter(void)
                                g_clear_error(&error);
                }
                g_variant_unref(result);
-#ifndef TIZEN_FEATURE_BT_USB_DONGLE
                /* Terminate myself */
-               g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
-#endif
+               if (!TIZEN_FEATURE_BT_USB_DONGLE)
+                       g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
                return BLUETOOTH_ERROR_INTERNAL;
        }
        g_variant_unref(result);
@@ -1139,6 +1226,37 @@ int _bt_enable_adapter(void)
        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;
@@ -1225,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;
 }
@@ -1311,6 +1431,7 @@ 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");
@@ -1327,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("-");
@@ -1337,46 +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);
-
-       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;
-       }
-
-       is_recovery_mode = TRUE;
+       recovery_cnt = BT_RECOVERY_MAX_COUNT;
 
-       g_variant_unref(result);
-       __bt_disconnect_all();
+       _bt_disable_adapter();
+       _bt_disable_adapter_le();
 
        BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;
@@ -1419,10 +1534,9 @@ int _bt_reset_adapter(void)
        return BLUETOOTH_ERROR_NONE;
 }
 
-#ifndef TIZEN_PROFILE_TV
 int _bt_check_adapter(int *status)
 {
-
+if (!TIZEN_PROFILE_TV) {
        char *adapter_path = NULL;
 
        BT_CHECK_PARAMETER(status, return);
@@ -1437,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;
@@ -1483,7 +1594,7 @@ int _bt_check_adapter(int *status)
        g_variant_unref(temp);
        return BLUETOOTH_ERROR_NONE;
 }
-#endif
+}
 
 int _bt_enable_adapter_le(void)
 {
@@ -1506,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;
        }
 
@@ -1923,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_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
@@ -2017,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);
 
@@ -2061,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);
@@ -2109,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);
@@ -2154,6 +2282,7 @@ int _bt_cancel_discovery(void)
                return ret;
        }
 
+       discovery_req = FALSE;
        cancel_by_user = TRUE;
        /* discovery status will be change in event */
        g_variant_unref(result);
@@ -2162,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)
@@ -3051,9 +3180,7 @@ int _bt_adapter_request_delayed(int function)
        return BLUETOOTH_ERROR_NONE;
 }
 
-#ifdef TIZEN_PROFILE_TV
 int _bt_get_enable_timer_id(void)
 {
        return timer_id;
 }
-#endif