Add missed code for the discovery state logic
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-adapter.c
index 93dee47..569afe2 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
 
@@ -70,6 +68,7 @@ typedef struct {
 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;
@@ -157,10 +156,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,10 +183,10 @@ 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);
@@ -212,21 +211,25 @@ 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;
 
-#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
-       }
+       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
+               }
 #endif
+       }
 
 
        if (timeout <= 0)
@@ -408,6 +411,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)
@@ -511,18 +515,16 @@ static int __bt_set_enabled(void)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
-#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
+       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,15 +589,12 @@ 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
+       if (TIZEN_FEATURE_BT_USB_DONGLE ||
+           _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));
-#ifndef USB_BLUETOOTH
        }
-#endif
 
        BT_INFO("Adapter disabled");
 }
@@ -699,7 +698,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 +725,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)
@@ -795,21 +792,21 @@ void _bt_handle_adapter_added(void)
        BT_DBG("status : %d", status);
        BT_DBG("le_status : %d", le_status);
 
-#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) {
+       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");
@@ -839,8 +836,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) {
@@ -872,23 +867,34 @@ void _bt_handle_adapter_removed(void)
        if (0 != ret)
                ERR("vconf_ignore_key_changed failed\n");
 
-#ifndef USB_BLUETOOTH
-       _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 (is_recovery_mode == TRUE) {
+                       /* Send disabled event */
+                       _bt_set_disabled(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;
+                       /* Will recover BT by bt-core, so set the mode as activating */
+                       _bt_adapter_set_status(BT_ACTIVATING);
+                       is_recovery_mode = FALSE;
+               } else {
+                       _bt_reliable_terminate_service(NULL);
+               }
        } else {
-               _bt_reliable_terminate_service(NULL);
+               _bt_set_disabled(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");
@@ -932,9 +938,8 @@ 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 (!TIZEN_FEATURE_BT_USB_DONGLE)
+               _bt_terminate_service(NULL);
 
        return FALSE;
 }
@@ -1010,7 +1015,6 @@ void _bt_adapter_start_enable_timer(void)
        return;
 }
 
-#ifdef TIZEN_TV
 static gboolean __bt_adapter_enabled_cb(gpointer user_data)
 {
        BT_DBG("+");
@@ -1020,7 +1024,6 @@ static gboolean __bt_adapter_enabled_cb(gpointer user_data)
 
        return FALSE;
 }
-#endif
 
 int _bt_enable_adapter_check_status(void)
 {
@@ -1075,8 +1078,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)
@@ -1089,7 +1091,6 @@ int _bt_enable_adapter(void)
                return BLUETOOTH_ERROR_NONE;
        }
 }
-#endif
 
        proxy = __bt_get_core_proxy();
        if (!proxy)
@@ -1126,10 +1127,9 @@ int _bt_enable_adapter(void)
                                g_clear_error(&error);
                }
                g_variant_unref(result);
-#ifndef USB_BLUETOOTH
                /* 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);
@@ -1328,7 +1328,16 @@ int _bt_disable_adapter(void)
                g_source_remove(timer_id);
                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 */
        __bt_disconnect_all();
        ret = _bt_disable_cb();
 
@@ -1421,10 +1430,9 @@ 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);
@@ -1439,10 +1447,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;
@@ -1485,7 +1490,7 @@ int _bt_check_adapter(int *status)
        g_variant_unref(temp);
        return BLUETOOTH_ERROR_NONE;
 }
-#endif
+}
 
 int _bt_enable_adapter_le(void)
 {
@@ -1925,20 +1930,28 @@ 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");
+               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
@@ -2063,7 +2076,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);
@@ -2111,7 +2124,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);
@@ -2156,6 +2169,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);
@@ -2164,7 +2178,7 @@ int _bt_cancel_discovery(void)
 
 gboolean _bt_is_discovering(void)
 {
-       return is_discovering;
+       return (is_discovering || discovery_req);
 }
 
 gboolean _bt_is_connectable(void)
@@ -2240,7 +2254,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;
 }
@@ -2962,7 +2976,11 @@ 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)
@@ -2981,7 +2999,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:
@@ -3036,14 +3058,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