Merge "Fix ASAN issue multiple definition of variable" into tizen
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / adapter / bt-service-core-adapter.c
index b82c333..581c1b6 100644 (file)
@@ -78,7 +78,6 @@ static guint timer_id = 0;
 
 static gboolean a2dp_init_pending = FALSE;
 
-
 /* Adapter default states */
 static bt_status_t adapter_state = BT_DEACTIVATED;
 static bt_adapter_discovery_state_t adapter_discovery_state = ADAPTER_DISCOVERY_STOPPED;
@@ -548,15 +547,21 @@ int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
 
        if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
                ret = _bt_stop_visibility_timer();
+               if (BLUETOOTH_ERROR_NONE != ret)
+                       BT_ERR("_bt_stop_visibility_timer failed");
                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);
+               if (BLUETOOTH_ERROR_NONE != ret)
+                       BT_ERR("_bt_start_visibility_timer failed");
 
        } else {
                ret = _bt_stop_visibility_timer();
+               if (BLUETOOTH_ERROR_NONE != ret)
+                       BT_ERR("_bt_stop_visibility_timer failed");
        }
 
        BT_DBG("-");
@@ -957,56 +962,50 @@ static void __bt_adapter_event_handler(int event_type, gpointer event_data)
 int _bt_init_profiles()
 {
        int ret;
+       BT_INFO("+");
 
        /*TODO: Init bluetooth profiles */
        ret = _bt_hidhost_initialize();
-       if (ret != BLUETOOTH_ERROR_NONE) {
+       if (ret != BLUETOOTH_ERROR_NONE)
                BT_ERR("_bt_hidhost_initialize Failed");
-               return ret;
-       }
 
        ret = _bt_socket_init();
-       if (ret != BLUETOOTH_ERROR_NONE) {
+       if (ret != BLUETOOTH_ERROR_NONE)
                BT_ERR("_bt_socket_init Failed");
-               return ret;
-       }
 
        /*
         * Query local adapter services and based on a2dp service uuids initialized
         * in bluetooth stack, enable A2DP sourec or A2DP sink role.
         */
        ret =  adapter_get_service_uuids();
-       if (ret != OAL_STATUS_SUCCESS) {
+       if (ret != OAL_STATUS_SUCCESS)
                BT_ERR("adapter_get_service_uuids failed: %d", ret);
-               return BLUETOOTH_ERROR_INTERNAL;
-       } else {
+       else
                a2dp_init_pending = TRUE;
-       }
+
 
        /* Initialize HFP Audio Gateway */
        ret = _bt_audio_initialize(BT_AG_MODULE);
-       if (ret != BLUETOOTH_ERROR_NONE) {
+       if (ret != BLUETOOTH_ERROR_NONE)
                BT_ERR("_bt_audio_initialize(BT_AG_MODULE) Failed");
-               return ret;
-       }
+
        /* Registering callback for receiving audio services searched */
        ret = _bt_audio_initialize(BT_AUDIO_ALL_MODULE);
-       if (ret != BLUETOOTH_ERROR_NONE) {
+       if (ret != BLUETOOTH_ERROR_NONE)
                BT_ERR("_bt_audio_initialize(BT_AUDIO_ALL_MODULE) Failed");
-               return ret;
-       }
 
        ret = _bt_hdp_init();
-       if (ret != BLUETOOTH_ERROR_NONE) {
+       if (ret != BLUETOOTH_ERROR_NONE)
                BT_ERR("_bt_hdp_init Failed");
-               return ret;
-       }
 
        ret = _bt_gatt_init();
-       if (ret != BLUETOOTH_ERROR_NONE) {
+       if (ret != BLUETOOTH_ERROR_NONE)
                BT_ERR("_bt_gatt_init Failed");
-               return ret;
-       }
+
+       /* Initialize HF Client */
+       ret = _bt_audio_initialize(BT_HFP_MODULE);
+       if (ret != BLUETOOTH_ERROR_NONE)
+               BT_ERR("_bt_audio_initialize(BT_HFP_MODULE) Failed");
 
        return BLUETOOTH_ERROR_NONE;
 }
@@ -1100,8 +1099,9 @@ static void __bt_adapter_handle_pending_requests(int service_function, void *use
        BT_INFO("+");
 
        /* Get method invocation context */
-       for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
+       for (l = _bt_get_invocation_list(); l != NULL; ) {
                req_info = l->data;
+               l = g_slist_next(l);
                if (req_info == NULL || req_info->service_function != service_function)
                        continue;
 
@@ -1180,21 +1180,21 @@ static gboolean __bt_adapter_post_set_enabled(gpointer user_data)
 {
        BT_INFO("__bt_adapter_post_set_enabled>>");
 
-       if (!TIZEN_PROFILE_TV) {
-               __bt_set_visible_mode();
-
-               /* add the vconf noti handler */
-               if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
-                               (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
-                       BT_ERR("DEVICE_NAME key changed notification registration failed");
-
-               __bt_set_local_name();
-       } else {
+       if (TIZEN_PROFILE_TV || !headed_plugin_info->plugin_headed_enabled) {
                if (BLUETOOTH_ERROR_NONE != _bt_set_discoverable_mode(
                                BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0))
                        BT_ERR("Fail to set discoverable mode");
+       } else {
+               __bt_set_visible_mode();
        }
 
+       /* add the vconf noti handler */
+       if (0 != vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
+                       (vconf_callback_fn)__bt_phone_name_changed_cb, NULL))
+               BT_ERR("DEVICE_NAME key changed notification registration failed");
+
+       __bt_set_local_name();
+
        /* Get All properties */
        if (OAL_STATUS_SUCCESS != adapter_get_properties())
                BT_ERR("adapter_get_properties failed");
@@ -1214,6 +1214,12 @@ static gboolean __bt_adapter_post_set_disabled(gpointer user_data)
                        BT_ERR("vconf_ignore_key_changed failed");
        }
 
+       /* bt-service should be terminated when BT is off */
+       if (!TIZEN_FEATURE_BT_USB_DONGLE) {
+               /* TODO: Implement to check if it is the recovery mode or not */
+               _bt_reliable_terminate_service(NULL);
+       }
+
        return FALSE;
 }
 
@@ -1231,6 +1237,9 @@ static void __bt_adapter_update_bt_enabled(void)
        if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
                BT_ERR("Set vconf failed\n");
 
+       if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
+               BT_ERR("Set vconf failed\n");
+
        /* TODO:Add timer function to handle any further post processing */
        g_idle_add((GSourceFunc)__bt_adapter_post_set_enabled, NULL);
 
@@ -1260,6 +1269,9 @@ static void __bt_adapter_update_bt_disabled(void)
        if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
                BT_ERR("Set vconf failed");
 
+       if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
+               BT_ERR("Set vconf failed\n");
+
        /* TODO:Add timer function to handle any further post processing */
        g_idle_add((GSourceFunc)__bt_adapter_post_set_disabled, NULL);
 
@@ -1314,6 +1326,16 @@ static void __bt_adapter_state_change_callback(int bt_status)
        }
 }
 
+int _bt_set_adapter_request_state(int enable)
+{
+       return oal_set_adapter_request_state(enable);
+}
+
+int _bt_set_le_request_state(int enable)
+{
+       return oal_set_le_request_state(enable);
+}
+
 static int __bt_adapter_state_handle_request(gboolean enable)
 {
        int result = BLUETOOTH_ERROR_NONE;
@@ -1329,8 +1351,6 @@ static int __bt_adapter_state_handle_request(gboolean enable)
                        if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
                                        adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
                                /*TODO Stop Discovery*/
-                               if (result != OAL_STATUS_SUCCESS)
-                                       BT_ERR("Discover stop failed: %d", result);
                                __bt_adapter_update_discovery_status(FALSE);
                        }
                        result = adapter_disable();
@@ -1355,8 +1375,6 @@ static int __bt_adapter_state_handle_request(gboolean enable)
                        if (adapter_discovery_state == ADAPTER_DISCOVERY_STARTED ||
                                        adapter_discovery_state == ADAPTER_DISCOVERY_STARTING) {
                                /*TODO Stop Discovery*/
-                               if (result != OAL_STATUS_SUCCESS)
-                                       BT_ERR("Discover stop failed: %d", result);
                                __bt_adapter_update_discovery_status(FALSE);
                        }
                        result = adapter_disable();
@@ -1457,19 +1475,10 @@ static int __bt_adapter_state_discovery_request(gboolean enable,
        case ADAPTER_DISCOVERY_STARTING: {
                BT_INFO("Adapter is currently in discovery starting state, state [%d]",
                                adapter_discovery_state);
-               if (enable) {
-                       return BLUETOOTH_ERROR_IN_PROGRESS;
-               } else {
-                       result = adapter_stop_inquiry();
-                       if (result != OAL_STATUS_SUCCESS) {
-                               BT_ERR("Discover stop failed: %d", result);
-                               result = BLUETOOTH_ERROR_INTERNAL;
-                       } else {
-                               BT_ERR("Stop Discovery Triggered successfully");
-                               __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPING);
-                               result = BLUETOOTH_ERROR_NONE;
-                       }
-               }
+
+               result = enable ?  BLUETOOTH_ERROR_IN_PROGRESS :
+                                       BLUETOOTH_ERROR_DEVICE_BUSY;
+
                break;
        }
        case ADAPTER_DISCOVERY_STOPPED: {
@@ -1499,23 +1508,10 @@ static int __bt_adapter_state_discovery_request(gboolean enable,
        case ADAPTER_DISCOVERY_STOPPING: {
                BT_INFO("Adapter is currently in discovery stopping state, state [%d]",
                                adapter_discovery_state);
-               if (!enable)
-                       return BLUETOOTH_ERROR_NOT_IN_OPERATION;
-               else {
-                       BT_DBG("max_resp: %u, duration: %u, cod: 0x%X", max_response, duration, mask);
-                       if (!is_custom)
-                               result = adapter_start_inquiry(duration);
-                       else
-                               result = adapter_start_custom_inquiry(role);
-                       if (result != OAL_STATUS_SUCCESS) {
-                               BT_ERR("Start Discovery failed: %d", result);
-                               result = BLUETOOTH_ERROR_INTERNAL;
-                       } else {
-                               BT_ERR("Start Discovery Triggered successfully");
-                       __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STARTING);
-                               result = BLUETOOTH_ERROR_NONE;
-                       }
-               }
+
+               result = enable ?  BLUETOOTH_ERROR_DEVICE_BUSY :
+                                       BLUETOOTH_ERROR_NOT_IN_OPERATION;
+
                break;
        }
        default:
@@ -1665,7 +1661,6 @@ void _bt_set_disabled(int result)
        if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
                BT_ERR("Set vconf failed\n");
 
-       _bt_cancel_queued_transfers();
        _bt_adapter_set_status(BT_DEACTIVATED);
        __bt_adapter_update_discovery_status(ADAPTER_DISCOVERY_STOPPED);