Simplify the logic to manage invocation list
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / device / bt-service-core-device.c
index 9a1472a..cabb572 100644 (file)
@@ -56,7 +56,9 @@
 #include <oal-adapter-mgr.h>
 #include <oal-device-mgr.h>
 
+#if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
 #define MAX_BOND_RETRY_COUNT 3
+#endif
 #define BT_PASSKEY_MAX_LENGTH 4
 
 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT    6000    /* msec */
@@ -133,7 +135,9 @@ typedef enum {
 
 /* BT device bond state variable */
 static bt_bond_state_e bt_device_bond_state;
+#if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
 static int bond_retry_count;
+#endif
 
 static char *passkey_watcher;
 static GSList *pin_info_list = NULL;
@@ -210,6 +214,7 @@ static void __bt_handle_ongoing_device_service_search(bt_remote_dev_info_t *remo
 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info);
 
 static int __bt_get_device_pin_code(const char *address, char *pin_code);
+static gboolean __bt_ignore_auto_pairing_request(const char *address);
 
 gboolean _bt_is_device_creating(void)
 {
@@ -247,7 +252,7 @@ void __bt_device_handle_pending_requests(int result, int service_function,
                                BT_ERR("Unexpected: Info request pending for a different address!!");
                                return;
                        } else {
-                               BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
+                               BT_DBG("Found info request addr [%s]", (char*)req_info->user_data);
                                bt_sdp_info_t sdp_info;
 
                                memset(&sdp_info, 0x00, sizeof(bt_sdp_info_t));
@@ -257,7 +262,6 @@ void __bt_device_handle_pending_requests(int result, int service_function,
                                g_array_append_vals(out_param, &sdp_info, sizeof(bt_sdp_info_t));
                                _bt_service_method_return(req_info->context, out_param, result);
 
-                               g_free(req_info->user_data);
                                _bt_free_info_from_invocation_list(req_info);
                                g_array_free(out_param, TRUE);
                        }
@@ -269,7 +273,7 @@ void __bt_device_handle_pending_requests(int result, int service_function,
                                BT_ERR("Unexpected: Info request pending for a different address!!");
                                return;
                        } else {
-                               BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
+                               BT_DBG("Found info request addr [%s]", (char*)req_info->user_data);
                                bluetooth_device_info_t dev_info;
                                memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
                                _bt_convert_addr_string_to_type(dev_info.device_address.addr,
@@ -279,7 +283,6 @@ void __bt_device_handle_pending_requests(int result, int service_function,
                                                sizeof(bluetooth_device_info_t));
                                _bt_service_method_return(req_info->context, out_param, result);
 
-                               g_free(req_info->user_data);
                                _bt_free_info_from_invocation_list(req_info);
                                g_array_free(out_param, TRUE);
                        }
@@ -291,7 +294,7 @@ void __bt_device_handle_pending_requests(int result, int service_function,
                                BT_ERR("Unexpected: Info request pending for a different address!!");
                                return;
                        } else {
-                               BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
+                               BT_DBG("Found info request addr [%s]", (char*)req_info->user_data);
                                bluetooth_device_address_t dev_addr;
                                _bt_convert_addr_string_to_type(dev_addr.addr, address);
                                out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
@@ -299,7 +302,6 @@ void __bt_device_handle_pending_requests(int result, int service_function,
                                                sizeof(bluetooth_device_address_t));
                                _bt_service_method_return(req_info->context, out_param, result);
 
-                               g_free(req_info->user_data);
                                _bt_free_info_from_invocation_list(req_info);
                                g_array_free(out_param, TRUE);
                        }
@@ -310,7 +312,7 @@ void __bt_device_handle_pending_requests(int result, int service_function,
                        break;
                }
        }
-       BT_INFO("-");
+       BT_DBG("-");
 }
 
 /*
@@ -323,7 +325,6 @@ static void __bt_device_remote_properties_callback(event_dev_properties_t *oal_d
 {
        bt_remote_dev_info_t *rem_info = NULL;
 
-       BT_DBG("+");
        rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
        _bt_copy_remote_dev(rem_info, &(oal_dev_props->device_info));
 
@@ -348,13 +349,13 @@ static void __bt_device_remote_properties_callback(event_dev_properties_t *oal_d
 
        /* a. Check if bonding is on-going, if yes, we MUST update the bonding device properties */
        if (trigger_bond_info  && !strcmp(trigger_bond_info->addr, rem_info->address)) {
-               BT_INFO("Bonding is ongoing, try update properties");
+               BT_DBG("Bonding is ongoing, try update properties");
                if (!trigger_bond_info->dev_info ||
                                (!trigger_bond_info->dev_info->name &&
                                 !trigger_bond_info->dev_info->alias) ||
                                        !trigger_bond_info->dev_info->address ||
                                                trigger_bond_info->dev_info->uuid_count == 0) {
-                       BT_INFO("Complete data is not present, Assigning rem_info");
+                       BT_DBG("Complete data is not present, Assigning rem_info");
                        if (!trigger_bond_info->dev_info)
                                trigger_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
                        _bt_copy_remote_dev_info(trigger_bond_info->dev_info, rem_info);
@@ -363,13 +364,13 @@ static void __bt_device_remote_properties_callback(event_dev_properties_t *oal_d
                BT_DBG("Bonding dev addr has matched with remote dev properties address [%s]", rem_info->address);
                __bt_handle_ongoing_bond(trigger_bond_info->dev_info, FALSE);
        } else if (incoming_bond_info && !g_strcmp0(incoming_bond_info->addr, rem_info->address)) {
-               BT_INFO("Incoming Bond is ongoing, try update properties");
+               BT_DBG("Incoming Bond is ongoing, try update properties");
                if (!incoming_bond_info->dev_info ||
                                (!incoming_bond_info->dev_info->name &&
                                 !incoming_bond_info->dev_info->alias) ||
                                        !incoming_bond_info->dev_info->address ||
                                                incoming_bond_info->dev_info->uuid_count == 0) {
-                       BT_INFO("Complete data is not present, Assigning rem_info");
+                       BT_DBG("Complete data is not present, Assigning rem_info");
                        if (!incoming_bond_info->dev_info)
                                incoming_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
                        _bt_copy_remote_dev_info(incoming_bond_info->dev_info, rem_info);
@@ -402,7 +403,6 @@ static void __bt_device_remote_properties_callback(event_dev_properties_t *oal_d
        }
 
        _bt_free_remote_dev(rem_info);
-       BT_DBG("-");
 }
 
 static int __get_oal_trusted_profile(bluetooth_trusted_profile_t profile)
@@ -443,7 +443,7 @@ int _bt_set_trust_profile(bluetooth_device_address_t *addr,
        result = device_set_trust_profile(&bd_addr, oal_profile, trust);
        if (result != OAL_STATUS_SUCCESS) {
                BT_ERR("device_set_trust_profile error: [%d]", result);
-               return BLUETOOTH_ERROR_INTERNAL;
+               return _bt_convert_oal_status_to_bt_error(result);
        }
 
        BT_DBG("-");
@@ -468,7 +468,7 @@ int _bt_get_trust_profile(bluetooth_device_address_t *addr,
        result = device_get_trust_profile(&bd_addr, oal_profile, trust);
        if (result != OAL_STATUS_SUCCESS) {
                BT_ERR("device_set_trust_profile error: [%d]", result);
-               return BLUETOOTH_ERROR_INTERNAL;
+               return _bt_convert_oal_status_to_bt_error(result);
        }
 
        BT_DBG("-");
@@ -604,25 +604,25 @@ static void __bt_device_authorization_request_callback(event_dev_authorize_req_t
 
        _bt_convert_addr_type_to_string(address, auth_event->address.addr);
 
-       BT_INFO("service_d: %d", service_d);
+       BT_DBG("service_d: %d", service_d);
 
        switch (service_d) {
        case HID_SERVICE_ID:
-               BT_INFO("Incoming HID Profile conn Req from device addr [%s]", address);
+               BT_DBG("Incoming HID Profile conn Req from device addr [%s]", address);
                break;
        case A2DP_SERVICE_ID:
-               BT_INFO("Incoming A2DP(Remote Sink) profile conn Req from device addr [%s]", address);
+               BT_DBG("Incoming A2DP(Remote Sink) profile conn Req from device addr [%s]", address);
                _bt_a2dp_src_handle_incoming_authorization(address, service_d);
                return;
        case A2DP_SRC_SERVICE_ID:
-               BT_INFO("Incoming A2DP(Remote Source) Profile conn Req from device addr [%s]", address);
+               BT_DBG("Incoming A2DP(Remote Source) Profile conn Req from device addr [%s]", address);
                _bt_a2dp_sink_handle_incoming_authorization(address, service_d);
                break;
        case AVRCP_SERVICE_ID:
-               BT_INFO("Incoming AVRCP (Remote) Profile conn Req from device addr [%s]", address);
+               BT_DBG("Incoming AVRCP (Remote) Profile conn Req from device addr [%s]", address);
                break;
        case AVRCP_CT_SERVICE_ID:
-               BT_INFO("Incoming AVRCP (Controller) Profile conn Req from device addr [%s]", address);
+               BT_DBG("Incoming AVRCP (Controller) Profile conn Req from device addr [%s]", address);
                break;
 #ifdef TIZEN_FEATURE_BT_OBEX
        case OPP_SERVICE_ID: {
@@ -636,7 +636,7 @@ static void __bt_device_authorization_request_callback(event_dev_authorize_req_t
 
                name = g_strdup(address);
 
-               BT_INFO("Incoming OPP conn Req from device addr [%s]", address);
+               BT_DBG("Incoming OPP conn Req from device addr [%s]", address);
                _bt_obex_server_set_pending_conn_auth_device_addr(address);
                param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, name);
                _bt_send_event(BT_OPP_SERVER_EVENT,
@@ -646,28 +646,28 @@ static void __bt_device_authorization_request_callback(event_dev_authorize_req_t
        }
 #endif
        case HSP_SERVICE_ID:
-               BT_INFO("Incoming HSP_SERVICE_ID conn Req from device addr [%s]", address);
+               BT_DBG("Incoming HSP_SERVICE_ID conn Req from device addr [%s]", address);
                break;
        case HFP_SERVICE_ID:
-               BT_INFO("Incoming HFP_SERVICE_ID conn Req from device addr [%s]", address);
+               BT_DBG("Incoming HFP_SERVICE_ID conn Req from device addr [%s]", address);
                break;
        case SAP_SERVICE_ID:
-               BT_INFO("Incoming SAP_SERVICE_ID conn Req from device addr [%s]", address);
+               BT_DBG("Incoming SAP_SERVICE_ID conn Req from device addr [%s]", address);
                break;
        case HSP_HS_SERVICE_ID:
-               BT_INFO("Incoming HSP_HS_SERVICE_ID conn Req from device addr [%s]", address);
+               BT_DBG("Incoming HSP_HS_SERVICE_ID conn Req from device addr [%s]", address);
                break;
        case HFP_HS_SERVICE_ID:
-               BT_INFO("Incoming HFP_HS_SERVICE_ID conn Req from device addr [%s]", address);
+               BT_DBG("Incoming HFP_HS_SERVICE_ID conn Req from device addr [%s]", address);
                break;
 #ifdef TIZEN_BT_HAL
        case IOTIVITY_SERVICE_ID:
-               BT_INFO("Incoming IOTIVITY_SERVICE_ID conn Req from device addr [%s]", address);
+               BT_DBG("Incoming IOTIVITY_SERVICE_ID conn Req from device addr [%s]", address);
                break;
 #endif
        default:
                /* For now, reject authorization for any service apart from above switch cases */
-               BT_INFO("Incoming Profile conn req with service ID [%d] from device addr [%s]", service_d, address);
+               BT_DBG("Incoming Profile conn req with service ID [%d] from device addr [%s]", service_d, address);
                res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, FALSE, FALSE);
                if (res != OAL_STATUS_SUCCESS)
                        BT_ERR("authorize_response: %d", res);
@@ -679,18 +679,15 @@ static void __bt_device_authorization_request_callback(event_dev_authorize_req_t
        res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, TRUE, FALSE);
        if (res != OAL_STATUS_SUCCESS)
                BT_ERR("authorize_response: %d", res);
-       BT_INFO("-");
 }
 
 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond)
 {
        GVariant *param = NULL;
-       BT_DBG("+");
 
        if ((remote_dev_info->name || remote_dev_info->alias)
                        && remote_dev_info->address
                        && remote_dev_info->uuids) {
-               BT_INFO("All properties updated,  time to send bonding finished event");
                GVariant *uuids = NULL;
                GVariantBuilder *builder = NULL;
                GVariant *manufacturer_data;
@@ -730,6 +727,7 @@ static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboo
                                BLUETOOTH_EVENT_BONDING_FINISHED,
                                param);
                BT_PERMANENT_LOG("Paired %s", remote_dev_info->address + 12);
+               BT_INFO_C("### Paired %s", remote_dev_info->address + 12);
                if (incoming_bond) {
                        __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
                } else {
@@ -746,8 +744,6 @@ static void __handle_incoming_bond_created_event(bt_address_t *bd_addr)
        char address[BT_ADDRESS_STRING_SIZE];
        bluetooth_device_address_t dev_addr;
 
-       BT_INFO("+");
-
        /*
         * BlueZ sends paired signal for each paired device, during activation,
         * We should ignore this, otherwise application thinks that a new device
@@ -782,7 +778,6 @@ static void __handle_incoming_bond_created_event(bt_address_t *bd_addr)
                __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
        }
 
-       BT_INFO("-");
 }
 
 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
@@ -790,8 +785,6 @@ static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
        gchar address[BT_ADDRESS_STR_LEN];
        bluetooth_device_address_t dev_addr;
 
-       BT_INFO("+");
-
        if (trigger_bond_info == NULL) {
                /* Send reply */
                BT_DBG("trigger_bond_info == NULL, Handle incomming bond event");
@@ -807,7 +800,6 @@ static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
                return;
        }
 
-       BT_INFO("Bonding successfully completed");
        /* Bonding state will be cleaned up & BONDING FINISHED EVENT
           will be sent only when Properties are fetched from stack
           Till that time lets not free trigger_bond_info.
@@ -819,7 +811,7 @@ static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
                        trigger_bond_info->addr);
 
        if (_bt_device_get_bonded_device_info(&dev_addr) == BLUETOOTH_ERROR_NONE) {
-               BT_DBG("BOnded device info query posted to stack successfully");
+               BT_DBG("Bonded device info query posted to stack successfully");
                __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_BOND_DEVICE,
                                trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
        } else {
@@ -832,8 +824,6 @@ static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
                __bt_free_bond_info(BT_DEVICE_BOND_INFO);
                __bt_free_pairing_info(&trigger_pairing_info);
        }
-
-       BT_INFO("-");
 }
 
 /**********************************************************************************************
@@ -847,13 +837,11 @@ static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr)
 {
        char address[BT_ADDRESS_STRING_SIZE];
 
-       BT_INFO("+");
-
        _bt_convert_addr_type_to_string(address, bd_addr->addr);
        _bt_service_remove_device_from_bonded_list(address);
 
        if (trigger_unbond_info) {
-               BT_INFO("Bond removal request successfully handled, return DBUS and send event");
+               BT_DBG("Bond removal request successfully handled, return DBUS and send event");
                GVariant *param = NULL;
                __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_UNBOND_DEVICE,
                                trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
@@ -868,7 +856,6 @@ static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr)
                BT_ERR("Bonding was removed");
                __bt_device_handle_bond_state();
        }
-       BT_INFO("-");
 }
 
 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event)
@@ -881,6 +868,7 @@ static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_f
        case OAL_STATUS_RMT_DEVICE_DOWN:
        {
                if (trigger_bond_info) {
+#if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
                        BT_INFO("OAL_STATUS_RMT_DEVICE_DOWN:Lets retry bonding!! retry count [%d]",
                                        bond_retry_count);
                        int ret = OAL_STATUS_SUCCESS;
@@ -896,13 +884,16 @@ static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_f
                                } else
                                        bond_retry_count++;
                        } else {
+#endif
                                BT_ERR("Create Bond failed MAX_BOND_RETRY_COUNT TIMES!!");
                                __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
                                                trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
                                __bt_free_bond_info(BT_DEVICE_BOND_INFO);
                                __bt_free_pairing_info(&trigger_pairing_info);
+#if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
                                bond_retry_count = 0;
                        }
+#endif
                }
                break;
        }
@@ -912,7 +903,12 @@ static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_f
                  To be considered later*/
                int result = BLUETOOTH_ERROR_INTERNAL;
                BT_INFO("BT_OPERATION_STATUS_AUTH_FAILED");
+
+               BT_INFO("add device in pairing black list");
+               _bt_set_autopair_status_in_bonding_info(FALSE);
+
                if (trigger_bond_info) {
+                       __bt_ignore_auto_pairing_request(trigger_bond_info->addr);
                        BT_ERR("Create Bond procedure could not suceed, check if cancelled by User");
                        if (trigger_bond_info->is_cancelled_by_user) {
                                BT_ERR("Bonding is cancelled by user");
@@ -926,6 +922,18 @@ static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_f
                __bt_free_pairing_info(&trigger_pairing_info);
                break;
        }
+       case OAL_STATUS_ALREADY_CONNECT:
+       {
+               int result = BLUETOOTH_ERROR_ALREADY_CONNECT;
+               BT_INFO("OAL_STATUS_ALREADY_CONNECT");
+
+               if (trigger_bond_info) {
+                       __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
+                               trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
+                       __bt_free_bond_info(BT_DEVICE_BOND_INFO);
+               }
+               break;
+       }
        case OAL_STATUS_INTERNAL_ERROR:
        {
                BT_INFO("OAL_STATUS_INTERNAL_ERROR");
@@ -1030,7 +1038,7 @@ static void __bt_device_dbfw_plus_info_callback(event_dev_dbfw_plus_info_t *dbfw
 static void __bt_device_event_handler(int event_type, gpointer event_data)
 {
        int eventcheck = OAL_EVENT_DEVICE_PROPERTIES;
-       BT_INFO("event [%d] Event check = [%d]", event_type, eventcheck);
+       BT_DBG("event [%d] Event check = [%d]", event_type, eventcheck);
 
        switch (event_type) {
        case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY: {
@@ -1064,24 +1072,22 @@ static void __bt_device_event_handler(int event_type, gpointer event_data)
                break;
        }
        case OAL_EVENT_DEVICE_ACL_CONNECTED: {
-               BT_INFO("ACL Connected event Received");
-               event_dev_conn_status_t* param = event_data;
-               __bt_device_acl_state_changed_callback(param, TRUE, 0);
+               BT_DBG("ACL Connected event Received");
+               __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, TRUE, 0);
                break;
        }
        case OAL_EVENT_DEVICE_ACL_DISCONNECTED: {
-               BT_INFO("ACL Disconnected event Received");
+               BT_DBG("ACL Disconnected event Received");
                __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 0);
                break;
        }
        case OAL_EVENT_DEVICE_LE_CONNECTED: {
-               BT_INFO("LE Connected event Received");
-               event_dev_conn_status_t* param = event_data;
-               __bt_device_acl_state_changed_callback(param, TRUE, 1);
+               BT_DBG("LE Connected event Received");
+               __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, TRUE, 1);
                break;
        }
        case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
-               BT_INFO("LE Disconnected event Received");
+               BT_DBG("LE Disconnected event Received");
                __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 1);
                break;
        }
@@ -1470,66 +1476,33 @@ static void __bt_device_ssp_consent_callback(remote_device_t* dev_info)
        BT_DBG("-");
 }
 
-static int __bt_oal_status_to_bt_error(int oal_status)
-{
-        int ret = 0;
-
-       switch (oal_status) {
-       case OAL_STATUS_SUCCESS:
-               ret = BLUETOOTH_ERROR_NONE;
-               break;
-       case OAL_STATUS_CONN_TIMEOUT:
-       case OAL_STATUS_LINK_LOSS:
-               BT_INFO("Connection Timeout");
-               ret = BLUETOOTH_ERROR_CONNECTION_TIMEOUT;
-               break;
-#ifdef TIZEN_BT_HAL
-       case OAL_STATUS_CONN_TERM_LOCAL_HOST:
-               ret = BLUETOOTH_ERROR_LOCAL_HOST_TERM;
-               break;
-       case OAL_STATUS_CONN_TERM_RMT_HOST:
-               ret = BLUETOOTH_ERROR_REMOTE_USER_TERM;
-               break;
-#endif
-       case OAL_STATUS_INTERNAL_ERROR:
-               ret = BLUETOOTH_ERROR_INTERNAL;
-               break;
-       default:
-               ret = BLUETOOTH_ERROR_INTERNAL;
-               break;
-       }
-       return ret;
-}
-
 static void __bt_device_acl_state_changed_callback(event_dev_conn_status_t *acl_event,
                gboolean connected, unsigned char type)
 {
        gchar address[BT_ADDRESS_STR_LEN];
-       int result = BLUETOOTH_ERROR_NONE;
+       int disc_reason = 0;
        GVariant *param = NULL;
        bt_device_conn_info_t conn_info;
 
-       BT_DBG("+");
        _bt_convert_addr_type_to_string(address, acl_event->address.addr);
 
        _bt_logging_connection(connected, type);
 
-       result = __bt_oal_status_to_bt_error(acl_event->status);
-       BT_INFO("Result [0x%x]", result);
+       disc_reason = acl_event->status;
 
        if (connected) {
-               param = g_variant_new("(isy)", result, address, type);
+               BT_PERMANENT_LOG("Connected %s %s", !type ? "EDR" : "LE", address + 12);
+               param = g_variant_new("(isy)", BLUETOOTH_ERROR_NONE, address, type);
                _bt_send_event(BT_DEVICE_EVENT,
                                BLUETOOTH_EVENT_DEVICE_CONNECTED,
                                param);
        } else {
-               param = g_variant_new("(isyi)", result, address, type, dbfw_rssi);
+               BT_PERMANENT_LOG("Disconnected %s(%d) %s", !type ? "EDR" : "LE",  disc_reason, address + 12);
+               param = g_variant_new("(isyi)", disc_reason, address, type, dbfw_rssi);
                _bt_send_event(BT_DEVICE_EVENT,
                                BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
                                param);
        }
-       BT_PERMANENT_LOG("%sonnected %s %s",
-               connected ? "C" : "Disc", !type ? "EDR" : "LE", address + 12);
 
        conn_info.connected = connected;
        conn_info.type = type;
@@ -1538,13 +1511,10 @@ static void __bt_device_acl_state_changed_callback(event_dev_conn_status_t *acl_
 
 #ifdef TIZEN_GATT_CLIENT
        /*handle LE connected device info*/
-       if (type) {
-               BT_DBG("handle LE connected device info");
+       if (type)
                _bt_handle_le_connected_dev_info(address, connected);
-       }
 #endif
 
-       BT_DBG("-");
 }
 
 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
@@ -1557,7 +1527,6 @@ static void __bt_device_remote_device_found_callback(gpointer event_data, gboole
        GVariantBuilder *builder = NULL;
        unsigned int i;
 
-       BT_INFO("+");
        ret_if(_bt_is_discovering() == FALSE);
        ret_if(event_data == NULL);
 
@@ -1616,7 +1585,6 @@ static void __bt_device_remote_device_found_callback(gpointer event_data, gboole
                        param);
 
        _bt_free_remote_dev(dev_info);
-       BT_DBG("-");
 }
 
 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
@@ -1625,7 +1593,6 @@ static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* in
        int result = BLUETOOTH_ERROR_NONE;
        GVariant *param = NULL;
        int event;
-       BT_DBG("+");
 
        _bt_convert_addr_type_to_string(address, info->address.addr);
 
@@ -1640,12 +1607,10 @@ static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* in
                        event,
                        param);
 
-       BT_DBG("-");
 }
 
 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
 {
-       BT_DBG("+");
        bt_pairing_data_t * info = *p_info;
        if (info) {
                if (info->addr)
@@ -1654,13 +1619,10 @@ static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
                g_free(info);
        }
        *p_info = NULL;
-       BT_DBG("-");
 }
 
 static void __bt_free_bond_info(uint8_t type)
 {
-       BT_INFO("+");
-
        switch (type) {
        case BT_DEVICE_BOND_INFO:
                if (!trigger_bond_info)
@@ -1698,7 +1660,6 @@ static void __bt_free_bond_info(uint8_t type)
                trigger_unbond_info = NULL;
                break;
        }
-       BT_INFO("-");
 }
 
 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
@@ -1727,7 +1688,7 @@ static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info
 
 static int __bt_device_handle_bond_state(void)
 {
-       BT_INFO("Current Bond state: %d", bt_device_bond_state);
+       BT_DBG("Current Bond state: %d", bt_device_bond_state);
        int ret = OAL_STATUS_INTERNAL_ERROR;
 
        switch (bt_device_bond_state) {
@@ -1773,7 +1734,7 @@ static int __bt_device_handle_bond_state(void)
        }
 
        if (ret != OAL_STATUS_SUCCESS)
-               return BLUETOOTH_ERROR_INTERNAL;
+               return _bt_convert_oal_status_to_bt_error(ret);
        else
                return BLUETOOTH_ERROR_NONE;
 }
@@ -1783,18 +1744,15 @@ int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
        int result;
        bt_address_t bd_addr;
 
-       BT_DBG("+");
-
        retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
 
        memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
        result = device_query_attributes(&bd_addr);
        if (result != OAL_STATUS_SUCCESS) {
                BT_ERR("device_query_attributes error: [%d]", result);
-               return BLUETOOTH_ERROR_INTERNAL;
+               return _bt_convert_oal_status_to_bt_error(result);
        }
 
-       BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;
 }
 
@@ -1803,20 +1761,18 @@ int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
        int ret;
        char address[BT_ADDRESS_STRING_SIZE];
 
-       BT_DBG("+");
        BT_CHECK_PARAMETER(alias, return);
 
        ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
        if (ret != OAL_STATUS_SUCCESS) {
                BT_ERR("device_set_alias: %d", ret);
-               return BLUETOOTH_ERROR_INTERNAL;
+               return _bt_convert_oal_status_to_bt_error(ret);
        }
 
        /* Update local cache */
        _bt_convert_addr_type_to_string(address, device_address->addr);
        _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
 
-       BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;
 }
 
@@ -1826,7 +1782,7 @@ int _bt_bond_device(bluetooth_device_address_t *device_address,
        int result = BLUETOOTH_ERROR_NONE;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
        bluetooth_device_info_t dev_info;
-       BT_DBG("+");
+       const char *stack_name = NULL;
 
        retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
 
@@ -1849,21 +1805,29 @@ int _bt_bond_device(bluetooth_device_address_t *device_address,
 
        /* Ready to initiate bonding */
 
-       /* In Tizen, we will first remove bond and then attempt to create bond to keep
-          consistency with bluedroid. Even if remove bond fails due to device not already
-          bonded, then straight away create bond is triggered. This is because, remove bond
-          is handled differently in bluedroid and bluez. In Bluez, if device is
-          already removed, remove bond call fails.
-          However in bluedroid, remove bond on already removed device returns success. So we will
-          handle the cases transparently*/
-       bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
+       stack_name = oal_get_stack_name();
+       if (stack_name && !g_strcmp0(stack_name, "bluez")) {
+               BT_DBG("[bluez] Create bond by type %d", conn_type);
+               bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
+       } else {
+               /* In Tizen, we will first remove bond and then attempt to create bond to keep
+                  consistency with bluedroid. Even if remove bond fails due to device not already
+                  bonded, then straight away create bond is triggered. This is because, remove bond
+                  is handled differently in bluedroid and bluez. In Bluez, if device is
+                  already removed, remove bond call fails.
+                  However in bluedroid, remove bond on already removed device returns success. So we will
+                  handle the cases transparently */
+               bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
+       }
+
+#if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
        bond_retry_count = 0;
+#endif
        result = __bt_device_handle_bond_state();
 
        if (result != BLUETOOTH_ERROR_NONE)
                goto fail;
 
-       BT_DBG("-");
        return result;
 
 fail:
@@ -1875,7 +1839,6 @@ fail:
                        sizeof(bluetooth_device_info_t));
        __bt_free_bond_info(BT_DEVICE_BOND_INFO);
 
-       BT_DBG("-");
        return result;
 }
 
@@ -1885,7 +1848,6 @@ int _bt_unbond_device(bluetooth_device_address_t *device_address,
        int result = OAL_STATUS_SUCCESS;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
        bluetooth_device_info_t dev_info;
-       BT_INFO("+");
 
        retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
 
@@ -1907,7 +1869,7 @@ int _bt_unbond_device(bluetooth_device_address_t *device_address,
        if (result != OAL_STATUS_SUCCESS)
                goto fail;
 
-       return result;
+       return BLUETOOTH_ERROR_NONE;
 
 fail:
        memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
@@ -1918,7 +1880,7 @@ fail:
                        sizeof(bluetooth_device_info_t));
        __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
 
-       return result;
+       return _bt_convert_oal_status_to_bt_error(result);
 }
 
 int _bt_cancel_bonding(void)
@@ -1964,7 +1926,7 @@ int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
 
        if (ret != OAL_STATUS_SUCCESS) {
                BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
-               return BLUETOOTH_ERROR_INTERNAL;
+               return _bt_convert_oal_status_to_bt_error(ret);
        }
 
        BT_INFO("-");
@@ -2055,7 +2017,7 @@ int _bt_search_device(bluetooth_device_address_t *device_address)
        if (result != OAL_STATUS_SUCCESS) {
                BT_ERR("Device Service Search Failed..: %d", result);
                __bt_free_service_search_info(&service_search_info);
-               return BLUETOOTH_ERROR_INTERNAL;
+               return _bt_convert_oal_status_to_bt_error(result);
        }
        return BLUETOOTH_ERROR_NONE;
 }
@@ -2069,7 +2031,7 @@ int _bt_cancel_search_device(void)
 
        if (ret != OAL_STATUS_SUCCESS) {
                BT_ERR("SDP Cancel request failed [%d]", ret);
-               return BLUETOOTH_ERROR_INTERNAL;
+               return _bt_convert_oal_status_to_bt_error(ret);
        }
 
        __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
@@ -2093,7 +2055,7 @@ int _bt_set_authorization(bluetooth_device_address_t *device_address,
        ret = device_set_authorized((bt_address_t*)device_address, authorize);
        if (ret != OAL_STATUS_SUCCESS) {
                BT_ERR("device_set_authorized: %d", ret);
-               return BLUETOOTH_ERROR_INTERNAL;
+               return _bt_convert_oal_status_to_bt_error(ret);
        }
 
        return BLUETOOTH_ERROR_NONE;
@@ -2123,6 +2085,9 @@ gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int
        case BT_PROFILE_CONN_HSP:
                svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
                break;
+       case BT_PROFILE_CONN_HFG:
+               svc_id = HFP_SERVICE_ID; /* Remote is HFP AG Unit */
+               break;
 #ifdef TIZEN_GATT_CLIENT
        case BT_PROFILE_CONN_GATT:
                return _bt_is_remote_gatt_device_connected(device_address); /* Remote is GATT client or Server */
@@ -2152,7 +2117,7 @@ int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
        res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
        if (res != OAL_STATUS_SUCCESS) {
                BT_ERR("authorize_response: %d", res);
-               return BLUETOOTH_ERROR_INTERNAL;
+               return _bt_convert_oal_status_to_bt_error(res);
        }
 
        BT_DBG("-");
@@ -2174,7 +2139,7 @@ int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
                        low_threshold, in_range_threshold, high_threshold);
        if (result != OAL_STATUS_SUCCESS) {
                BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
-               return BLUETOOTH_ERROR_INTERNAL;
+               return _bt_convert_oal_status_to_bt_error(result);
        }
 
        BT_DBG("-");
@@ -2194,7 +2159,7 @@ int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
        result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
        if (result != OAL_STATUS_SUCCESS) {
                BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
-               return BLUETOOTH_ERROR_INTERNAL;
+               return _bt_convert_oal_status_to_bt_error(result);
        }
 
        BT_DBG("-");
@@ -2211,7 +2176,7 @@ int _bt_set_passkey_notification(const char *sender, gboolean enable)
        result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY, enable);
        if (OAL_STATUS_SUCCESS != result) {
                BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
-               return BLUETOOTH_ERROR_INTERNAL;
+               return _bt_convert_oal_status_to_bt_error(result);
        }
 
        g_free(passkey_watcher);
@@ -2312,6 +2277,25 @@ int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
        return BLUETOOTH_ERROR_NONE;
 }
 
+int _bt_device_get_ida(bluetooth_device_address_t *device_address, bluetooth_device_address_t *id_address)
+{
+       int result = BLUETOOTH_ERROR_NONE;
+       char address[BT_ADDRESS_STRING_SIZE] = { 0 };
+
+       _bt_convert_addr_type_to_string(address, device_address->addr);
+
+       BT_DBG("getting IDA for remote device: [%s]", address);
+
+       result = device_get_ida((bt_address_t*)device_address, (bt_address_t*)id_address);
+
+       if (result != OAL_STATUS_SUCCESS) {
+               BT_ERR("device_get_ida Failed %d", result);
+               return _bt_convert_oal_status_to_bt_error(result);
+       }
+
+       return result;
+}
+
 #ifdef TIZEN_GATT_CLIENT
 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
 {
@@ -2373,10 +2357,9 @@ static void _bt_remove_le_connected_dev_info(const char *address)
        return;
 }
 
-
 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected)
 {
-       BT_DBG("+");
+       BT_DBG("update le_connected_dev_list");
 
        if (connected)
                _bt_add_le_connected_dev_info(address);
@@ -2417,8 +2400,6 @@ int _bt_add_le_conn_param_info(const char *address, const char *sender,
        bt_le_conn_param_t *param = NULL;
        bt_le_conn_param_t *data = NULL;
 
-       BT_DBG("+");
-
        if (!address || !sender)
                return BLUETOOTH_ERROR_INVALID_PARAM;
 
@@ -2469,7 +2450,7 @@ static int __bt_le_set_conn_parameter(const char *address,
        max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
        to = time_out / BT_LE_CONN_TO_SPLIT;
 
-       BT_INFO("updating: Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
+       BT_DBG("updating: Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
                        min, max, latency, to);
 
        _bt_convert_addr_string_to_type(dev_addr.addr, address);
@@ -2551,7 +2532,7 @@ int _bt_le_connection_update(const char *sender,
 
        BT_CHECK_PARAMETER(device_address, return);
 
-       BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
+       BT_DBG("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
                        sender, interval_min, interval_max, latency, time_out);
 
        if (interval_min > interval_max ||
@@ -2591,7 +2572,7 @@ int _bt_le_connection_update(const char *sender,
 
        dev = __bt_get_le_connected_dev_info(address);
        if (dev == NULL) {
-               BT_DBG("device not found in the list");
+               BT_ERR("device not found in the list");
                ret = BLUETOOTH_ERROR_NOT_CONNECTED;
                goto fail;
        }
@@ -2618,11 +2599,10 @@ update:
        if (ret != OAL_STATUS_SUCCESS) {
                _bt_remove_le_conn_param_info(address, sender);
                BT_DBG("fail to update the LE connection parameter");
-               ret = BLUETOOTH_ERROR_INTERNAL;
+               ret = _bt_convert_oal_status_to_bt_error(ret);
                goto fail;
        }
 
-       BT_DBG("updated LE connection parameter");
        dev->interval_min = interval_min;
        dev->interval_max = interval_max;
 
@@ -2642,11 +2622,95 @@ int _bt_disconnect_device(bluetooth_device_address_t *device_address)
        result = device_disconnect((bt_address_t *)device_address);
        if (result != OAL_STATUS_SUCCESS) {
                BT_DBG("Failed to disconnect device");
-               return BLUETOOTH_ERROR_INTERNAL;
+               return _bt_convert_oal_status_to_bt_error(result);
        }
 
        BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;
 }
 
+static gboolean __bt_ignore_auto_pairing_request(const char *address)
+{
+       gchar *buffer;
+       char **lines;
+       int i;
+       char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
+       char *temp_buffer;
+       FILE *fp;
+       long size;
+       size_t result;
+
+       BT_DBG("+\n");
+
+       if (address == NULL)
+               return FALSE;
+
+       /* Get the LAP(Lower Address part) */
+       /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
+          a "," */
+       snprintf(lap_address, sizeof(lap_address), ",%s", address);
+
+       fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
+
+       if (fp == NULL) {
+               BT_ERR("fopen failed \n");
+               return FALSE;
+       }
+
+       fseek(fp, 0, SEEK_END);
+       size = ftell(fp);
+       rewind(fp);
+
+       if (size < 0) {
+               BT_ERR("Get file size failed \n");
+               fclose(fp);
+               return FALSE;
+       }
+
+       buffer = g_malloc0(sizeof(char) * size);
+       result = fread((char *)buffer, 1, size, fp);
+       fclose(fp);
+       if (result != size) {
+               BT_ERR("Read Error\n");
+               g_free(buffer);
+               return FALSE;
+       }
+
+       lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
+       g_free(buffer);
+
+       if (lines == NULL)
+               return FALSE;
+
+       /* Write the data and insert new device data */
+       for (i = 0; lines[i] != NULL; i++) {
+               if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
+                       temp_buffer = g_strconcat(lines[i], lap_address, NULL);
+                       g_free(lines[i]);
+                       lines[i] = temp_buffer;
+               }
+       }
+       buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
+       g_strfreev(lines);
+       /* Fix : NULL_RETURNS */
+       retv_if(buffer == NULL, FALSE);
+
+       fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
+
+       if (fp == NULL) {
+               BT_ERR("fopen failed \n");
+               g_free(buffer);
+               return FALSE;
+       }
+
+       BT_DBG("Buffer = %s\n", buffer);
+       fwrite(buffer, 1, strlen(buffer), fp);
+       fclose(fp);
+
+       g_free(buffer);
+
+       BT_DBG("-\n");
+
+       return FALSE;
+}
 #endif