Fixed AVRCP controller reply pending request
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-adapter.c
index d06b753..078bca7 100644 (file)
@@ -22,6 +22,9 @@
 #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_FEATURE_BT_DPM
+#include "bt-service-adapter-le.h"
+
+#ifdef TIZEN_DPM_ENABLE
 #include "bt-service-dpm.h"
 #endif
 
@@ -65,12 +69,9 @@ 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;
@@ -79,9 +80,10 @@ 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 gboolean is_recovery_mode;
+static gboolean in_power_off;
 
+static guint poweroff_subscribe_id;
 static uint status_reg_id;
 
 #define BT_CORE_NAME "org.projectx.bt_core"
@@ -90,9 +92,6 @@ 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);
 
@@ -162,10 +161,10 @@ static gboolean __bt_timeout_handler(gpointer user_data)
                visible_timer.event_id = 0;
                visible_timer.timeout = 0;
 
-               if (!TIZEN_PROFILE_WEARABLE) {
-                       if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
-                               BT_ERR("Set vconf failed\n");
-               }
+#ifndef TIZEN_PROFILE_WEARABLE
+               if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
+                       BT_ERR("Set vconf failed\n");
+#endif
                return FALSE;
        }
 
@@ -189,10 +188,10 @@ static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
                visible_timer.event_id = 0;
                visible_timer.timeout = 0;
 
-               if (!TIZEN_PROFILE_WEARABLE) {
-                       if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
-                               BT_ERR("Set vconf failed\n");
-               }
+#ifndef TIZEN_PROFILE_WEARABLE
+               if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
+                       BT_ERR("Set vconf failed\n");
+#endif
        }
        /* Switch Off visibility in Bluez */
        _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
@@ -217,25 +216,21 @@ int __bt_set_visible_time(int timeout)
 {
        int result;
        alarm_id_t alarm_id;
-#ifdef TIZEN_FEATURE_BT_DPM
-       int discoverable_state = DPM_BT_ERROR;
-#endif
 
        __bt_visibility_alarm_remove();
 
        visible_timer.timeout = timeout;
 
-       if (!TIZEN_PROFILE_WEARABLE) {
-#ifdef TIZEN_FEATURE_BT_DPM
-               _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
-               if (discoverable_state != DPM_RESTRICTED) {
-#endif
-               if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
-                       BT_ERR("Set vconf failed");
-#ifdef TIZEN_FEATURE_BT_DPM
-               }
+#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
        }
+#endif
+#endif
 
 
        if (timeout <= 0)
@@ -364,9 +359,9 @@ static int __bt_get_bonded_device_info(gchar *device_path,
                        rssi = g_variant_get_int16(value);
                } else if (!g_strcmp0(key, "UUIDs")) {
                        __bt_get_service_list(value, dev_info);
-               } else if (!g_strcmp0(key, "ManufacturerDataLen")) {
+               } else if (!g_strcmp0(key, "LegacyManufacturerDataLen")) {
                        dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
-               } else if (!g_strcmp0(key, "ManufacturerData")) {
+               } else if (!g_strcmp0(key, "LegacyManufacturerData")) {
                        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))
@@ -419,7 +414,6 @@ 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)
@@ -460,12 +454,24 @@ void _bt_set_le_intended_status(gboolean value)
        is_le_intended = value;
 }
 
+static void __bt_set_in_poweroff(void)
+{
+       BT_INFO("Set in_power_off to TRUE");
+       in_power_off = TRUE;
+}
+
+static gboolean __bt_is_in_poweroff(void)
+{
+       return in_power_off;
+}
+
 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
 {
        char *phone_name = NULL;
        char *ptr = NULL;
 
-       ret_if(node == NULL);
+       if (node == NULL)
+               return;
 
        if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
                phone_name = vconf_keynode_get_str(node);
@@ -527,16 +533,18 @@ static int __bt_set_enabled(void)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-       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");
-       }
+#ifdef 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_TV
+       if (_bt_set_discoverable_mode(
+               BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0) != BLUETOOTH_ERROR_NONE)
+                       BT_ERR("Fail to set discoverable mode");
+#endif
+#endif
 
        /* Update Bluetooth Status to notify other modules */
        if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
@@ -601,6 +609,16 @@ void _bt_set_disabled(int result)
        _bt_adapter_set_status(BT_DEACTIVATED);
        _bt_set_discovery_status(FALSE);
 
+#ifndef USB_BLUETOOTH
+       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 USB_BLUETOOTH
+       }
+#endif
+
        BT_INFO("Adapter disabled");
 }
 
@@ -612,7 +630,11 @@ static int __bt_set_le_enabled(void)
 
        /* Update Bluetooth Status to notify other modules */
        if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_ON) != 0)
-               BT_ERR("Set vconf failed\n");
+               BT_ERR("Set vconf failed");
+
+       /* set packet length to max size to enable packet length extension */
+       if (BLUETOOTH_ERROR_NONE != _bt_le_set_max_packet_len())
+               BT_ERR("Fail to set max packet length");
 
        if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
                                                EVT_VAL_BT_LE_ON) != ES_R_OK)
@@ -664,7 +686,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)
@@ -703,6 +725,7 @@ 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;
@@ -730,39 +753,87 @@ static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
 
        _bt_enable_core();
 }
+#endif
 
-void _bt_service_register_vconf_handler(void)
+static void __bt_poweroff_event_filter(GDBusConnection *connection,
+               const gchar *sender_name, const gchar *object_path,
+               const gchar *interface_name, const gchar *signal_name,
+               GVariant *parameters, gpointer user_data)
 {
-       BT_DBG("+");
+       int state = 0;
 
-       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");
+       g_variant_get(parameters, "(i)", &state);
+
+       if (state != BT_DEVICED_POWEROFF_SIGNAL_POWEROFF &&
+           state != BT_DEVICED_POWEROFF_SIGNAL_REBOOT) {
+               BT_DBG("Not interested event : %d", state);
+               return;
        }
 
-       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");
+       if (_bt_adapter_get_status() == BT_ACTIVATING) {
+               BT_INFO("Just update VCONFKEY_BT_STATUS in Power off");
+               if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON))
+                       BT_ERR("Set VCONFKEY_BT_STATUS failed");
+       } else {
+               __bt_set_in_poweroff();
        }
 }
 
+void _bt_service_register_poweroff_event(void)
+{
+       if (poweroff_subscribe_id)
+               return;
+
+       poweroff_subscribe_id = g_dbus_connection_signal_subscribe(
+                       _bt_gdbus_get_system_gconn(), NULL,
+                       BT_DEVICED_POWEROFF_INTERFACE,
+                       BT_DEVICED_POWEROFF_SIGNAL,
+                       BT_DEVICED_POWEROFF_OBJECT_PATH,
+                       NULL, 0, __bt_poweroff_event_filter, NULL, NULL);
+}
+
+void _bt_service_unregister_poweroff_event(void)
+{
+       if (poweroff_subscribe_id == 0)
+               return;
+
+       g_dbus_connection_signal_unsubscribe(_bt_gdbus_get_system_gconn(),
+                                            poweroff_subscribe_id);
+       poweroff_subscribe_id = 0;
+}
+
+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
+
+#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
+}
+
 void _bt_service_unregister_vconf_handler(void)
 {
        BT_DBG("+");
 
-       if (TIZEN_FEATURE_FLIGHTMODE_ENABLED) {
-               vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
-                               (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
-       }
+#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_PROFILE_WEARABLE) {
-               vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
-                               (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
 }
 
 static void __bt_state_event_handler(const char *event_name, bundle *data, void *user_data)
@@ -778,34 +849,6 @@ 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("+");
@@ -820,27 +863,26 @@ void _bt_handle_adapter_added(void)
        }
 */
 
-       recovery_cnt = -1;
-
        status = _bt_adapter_get_status();
        le_status = _bt_adapter_get_le_status();
        BT_INFO("status : %d", status);
        BT_INFO("le_status : %d", le_status);
-       if (!TIZEN_FEATURE_BT_USB_DONGLE) {
+
+#ifndef USB_BLUETOOTH
+       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;
                }
-       } 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");
@@ -901,50 +943,23 @@ void _bt_handle_adapter_removed(void)
        if (0 != ret)
                ERR("vconf_ignore_key_changed failed\n");
 
-       /* 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 */
-
-       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);
-                       }
+#ifndef USB_BLUETOOTH
+       _bt_destroy_agent(adapter_agent);
+       adapter_agent = NULL;
 
-                       if (eventsystem_unregister_event(status_reg_id) != ES_R_OK)
-                               BT_ERR("Fail to unregister system event");
-                       return;
-               }
+       if (is_recovery_mode == TRUE) {
+               /* Send disabled event */
+               _bt_set_disabled(BLUETOOTH_ERROR_NONE);
 
-               if (recovery_timer == 0)
-                       _bt_reliable_terminate_service(NULL);
+               /* Will recover BT by bt-core, so set the mode as activating */
+               _bt_adapter_set_status(BT_ACTIVATING);
+               is_recovery_mode = FALSE;
        } else {
-               _bt_set_disabled(BLUETOOTH_ERROR_NONE);
-               _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
-                               g_variant_new("(i)", BLUETOOTH_ERROR_NONE));
+               _bt_reliable_terminate_service(NULL);
        }
+#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");
@@ -988,24 +1003,9 @@ static gboolean __bt_enable_timeout_cb(gpointer user_data)
        g_variant_unref(result);
        _bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
 
-       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);
+#ifndef USB_BLUETOOTH
+       _bt_terminate_service(NULL);
+#endif
 
        return FALSE;
 }
@@ -1045,9 +1045,9 @@ static gboolean __bt_enable_le_timeout_cb(gpointer user_data)
        }
 
        g_variant_unref(result);
+       _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
 
-       if (_bt_adapter_get_le_status() != BT_LE_DEACTIVATED)
-               _bt_set_le_disabled(BLUETOOTH_ERROR_TIMEOUT);
+       _bt_set_le_disabled(BLUETOOTH_ERROR_TIMEOUT);
 
        if (_bt_adapter_get_status() == BT_DEACTIVATED)
                _bt_terminate_service(NULL);
@@ -1081,6 +1081,7 @@ void _bt_adapter_start_enable_timer(void)
        return;
 }
 
+#ifdef TIZEN_TV
 static gboolean __bt_adapter_enabled_cb(gpointer user_data)
 {
        BT_DBG("+");
@@ -1090,6 +1091,7 @@ static gboolean __bt_adapter_enabled_cb(gpointer user_data)
 
        return FALSE;
 }
+#endif
 
 int _bt_enable_adapter_check_status(void)
 {
@@ -1144,7 +1146,8 @@ int _bt_enable_adapter(void)
 
        _bt_adapter_set_status(BT_ACTIVATING);
 
-if (TIZEN_PROFILE_TV) {
+#ifdef TIZEN_TV
+{
        int adapter_status = BT_ADAPTER_DISABLED;
 
        if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
@@ -1157,6 +1160,14 @@ if (TIZEN_PROFILE_TV) {
                return BLUETOOTH_ERROR_NONE;
        }
 }
+#endif
+
+       if (__bt_is_in_poweroff() == TRUE) {
+               BT_INFO("Just update VCONFKEY_BT_STATUS in Power off");
+               if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
+                       BT_ERR("Set VCONFKEY_BT_STATUS failed");
+               return BLUETOOTH_ERROR_NONE;
+       }
 
        proxy = __bt_get_core_proxy();
        if (!proxy)
@@ -1193,9 +1204,10 @@ if (TIZEN_PROFILE_TV) {
                                g_clear_error(&error);
                }
                g_variant_unref(result);
+#ifndef USB_BLUETOOTH
                /* Terminate myself */
-               if (!TIZEN_FEATURE_BT_USB_DONGLE)
-                       g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
+               g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
+#endif
                return BLUETOOTH_ERROR_INTERNAL;
        }
        g_variant_unref(result);
@@ -1207,37 +1219,6 @@ if (TIZEN_PROFILE_TV) {
        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;
@@ -1247,7 +1228,7 @@ static gboolean __bt_disconnect_all(void)
        GVariant *result;
        GError *error = NULL;
        GArray *device_list;
-       bluetooth_device_info_t info;
+       bluetooth_device_info_t *info;
        guint size;
        char *device_path = NULL;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
@@ -1269,12 +1250,12 @@ static gboolean __bt_disconnect_all(void)
 
        for (i = 0; i < size; i++) {
 
-               info = g_array_index(device_list,
+               info = &g_array_index(device_list,
                                bluetooth_device_info_t, i);
 
-               if (info.connected != BLUETOOTH_CONNECTED_LINK_NONE) {
+               if (info->connected != BLUETOOTH_CONNECTED_LINK_NONE) {
                        BT_DBG("Found Connected device");
-                       _bt_convert_addr_type_to_string(address, info.device_address.addr);
+                       _bt_convert_addr_type_to_string(address, info->device_address.addr);
                        device_path = _bt_get_device_object_path(address);
                        if (device_path == NULL)
                                continue;
@@ -1324,8 +1305,6 @@ 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;
 }
@@ -1412,7 +1391,6 @@ 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");
@@ -1429,26 +1407,7 @@ int _bt_disable_adapter(void)
                timer_id = 0;
        }
 
-       /* 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);
-       }
-
+       __bt_disconnect_all();
        ret = _bt_disable_cb();
 
        BT_DBG("-");
@@ -1458,6 +1417,9 @@ 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_ERR("Disabling in progress");
@@ -1469,10 +1431,32 @@ int _bt_recover_adapter(void)
                return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
        }
 
-       recovery_cnt = BT_RECOVERY_MAX_COUNT;
+       _bt_adapter_set_status(BT_DEACTIVATING);
 
-       _bt_disable_adapter();
-       _bt_disable_adapter_le();
+       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;
+
+       g_variant_unref(result);
+       __bt_disconnect_all();
 
        BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;
@@ -1515,9 +1499,10 @@ int _bt_reset_adapter(void)
        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);
@@ -1532,7 +1517,10 @@ if (!TIZEN_PROFILE_TV) {
 
        g_free(adapter_path);
        return BLUETOOTH_ERROR_NONE;
-} else {
+}
+#else
+int _bt_check_adapter(int *status)
+{
        GDBusProxy *proxy;
        GError *error = NULL;
        GVariant *result;
@@ -1575,7 +1563,7 @@ if (!TIZEN_PROFILE_TV) {
        g_variant_unref(temp);
        return BLUETOOTH_ERROR_NONE;
 }
-}
+#endif
 
 int _bt_enable_adapter_le(void)
 {
@@ -1598,7 +1586,6 @@ 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;
        }
 
@@ -2016,28 +2003,20 @@ 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);
+#ifdef TIZEN_DPM_ENABLE
        if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
-                discoverable_state == DPM_RESTRICTED) {
-               if (headed_plugin_info->plugin_headed_enabled)
-                       headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT_HANDSFREE");
-
+               _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
+               _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
                return BLUETOOTH_ERROR_ACCESS_DENIED;
        }
        if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
-               discoverable_state == DPM_RESTRICTED) {
-               if (headed_plugin_info->plugin_headed_enabled)
-                       headed_plugin_info->headed_plugin->bt_launch_dpmpopup("DPM_POLICY_DISABLE_BT");
-
+                _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
+               _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
                return BLUETOOTH_ERROR_ACCESS_DENIED;
        }
 #endif
@@ -2162,7 +2141,7 @@ int _bt_start_discovery(void)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-       discovery_req = TRUE;
+       is_discovering = TRUE;
        cancel_by_user = FALSE;
        /* discovery status will be change in event */
        g_variant_unref(result);
@@ -2210,7 +2189,7 @@ int _bt_start_custom_discovery(bt_discovery_role_type_t role)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-       discovery_req = TRUE;
+       is_discovering = TRUE;
        cancel_by_user = FALSE;
        /* discovery status will be change in event */
        g_variant_unref(result);
@@ -2255,7 +2234,6 @@ 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);
@@ -2264,7 +2242,7 @@ int _bt_cancel_discovery(void)
 
 gboolean _bt_is_discovering(void)
 {
-       return (is_discovering || discovery_req);
+       return is_discovering;
 }
 
 gboolean _bt_is_connectable(void)
@@ -2506,9 +2484,9 @@ static bluetooth_device_info_t *__bt_parse_device_info(GVariantIter *item_iter)
                        }
                        dev_info->service_index = i;
                        g_variant_iter_free(iter);
-               } else if (strcasecmp(key, "ManufacturerDataLen") == 0) {
+               } else if (strcasecmp(key, "LegacyManufacturerDataLen") == 0) {
                        dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
-               } else if (strcasecmp(key, "ManufacturerData") == 0) {
+               } else if (strcasecmp(key, "LegacyManufacturerData") == 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))
@@ -3062,11 +3040,7 @@ 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)
@@ -3085,11 +3059,7 @@ 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:
@@ -3144,16 +3114,14 @@ int _bt_adapter_request_delayed(int function)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-#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
+       g_idle_add((GSourceFunc)_bt_adapter_request_delayed_cb, (void*)function);
 
        return BLUETOOTH_ERROR_NONE;
 }
 
+
 int _bt_get_enable_timer_id(void)
 {
        return timer_id;
 }
+