Fix : LE Connect d-bus method is not returned
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-device.c
index 9a8b1e3..6b17704 100644 (file)
 #include "bt-service-network.h"
 #include "bt-service-adapter.h"
 #include "bt-service-gap-agent.h"
+#include "bt-service-pbap.h"
+
+#ifdef TIZEN_FEATURE_BT_DPM
+#include "bt-service-dpm.h"
+#endif
 
 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
@@ -84,12 +89,37 @@ typedef struct {
        char *pin_code;
 } bt_pin_code_info_t;
 
+typedef struct {
+       int req_id;
+       bluetooth_device_address_t bd_addr;
+       gboolean auto_connect;
+} bt_pending_le_conn_info_s;
+
+typedef struct {
+       char *address;
+       float interval_min;
+       float interval_max;
+       GSList *senders;
+} bt_connected_le_dev_t;
+
+typedef struct {
+       char *sender;
+       float interval_min;
+       float interval_max;
+       guint16 latency;
+       guint16 time_out;
+       float key;
+} bt_le_conn_param_t;
+
 gboolean is_device_creating;
 bt_funcion_data_t *bonding_info;
 bt_funcion_data_t *searching_info;
 bt_funcion_data_t *att_mtu_req_info;
 
+static GSList *le_connected_dev_list = NULL;
 static GSList *pin_info_list = NULL;
+static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
+static guint pending_le_conn_timer_id = 0;
 
 /* This HID Mouse does not support pairing precedure. need to skip it. */
 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
@@ -209,7 +239,7 @@ static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
        bundle *b = NULL;
        GDBusConnection *conn;
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        if (conn == NULL)
                return FALSE;
 
@@ -308,8 +338,12 @@ static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
        ret_if(value == NULL);
        ret_if(info == NULL);
 
-       info->uuid_count = g_variant_get_size(value);
-       info->uuids = g_variant_dup_strv(value, &info->uuid_count);
+       gsize uuid_count = 0;
+
+       info->uuids = g_variant_dup_strv(value, &uuid_count);
+       info->uuid_count = (unsigned int)uuid_count;
+
+       BT_DBG("uuid count : %d", uuid_count);
 }
 
 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
@@ -350,7 +384,7 @@ bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
 
        retv_if(object_path == NULL, NULL);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        if (conn == NULL) {
                BT_ERR("conn == NULL");
                return NULL;
@@ -389,7 +423,7 @@ bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
                        g_variant_unref(tmp_value);
                }
 
-               tmp_value = g_variant_lookup_value (value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
+               tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
                if (tmp_value) {
                        dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
                        g_variant_unref(tmp_value);
@@ -550,12 +584,6 @@ static gboolean __ignore_auto_pairing_request(const char *address)
        }
 
        buffer = g_malloc0(sizeof(char) * size);
-       /* Fix : NULL_RETURNS */
-       if (buffer == NULL) {
-               BT_ERR("Memory allocation error\n");
-               fclose(fp);
-               return FALSE;
-       }
        result = fread((char *)buffer, 1, size, fp);
        fclose(fp);
        if (result != size) {
@@ -628,6 +656,8 @@ static int __bt_retry_bond(void)
        BT_CHECK_PARAMETER(bonding_info, return);
        BT_CHECK_PARAMETER(bonding_info->addr, return);
 
+       is_device_creating = TRUE;
+
        g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
                                g_variant_new("(y)", bonding_info->conn_type),
                                G_DBUS_CALL_FLAGS_NONE,
@@ -701,6 +731,7 @@ static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
 {
        int result = BLUETOOTH_ERROR_NONE;
        GError *err = NULL;
+       GVariant *reply;
        GVariant *out_param1;
        request_info_t *req_info;
        bluetooth_device_info_t dev_info;
@@ -711,7 +742,9 @@ static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
        /* Terminate ALL system popup */
        syspopup_destroy_all();
 
-       g_dbus_proxy_call_finish(proxy, res, &err);
+       reply = g_dbus_proxy_call_finish(proxy, res, &err);
+       if (reply)
+               g_variant_unref(reply);
 
        is_device_creating = FALSE;
 
@@ -767,7 +800,7 @@ static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
                        result = BLUETOOTH_ERROR_HOST_DOWN;
                } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
                        BT_INFO("Cancel already running bonding");
-                       if(__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
+                       if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
                                BT_INFO("Error while Cancelling bonding");
                                /* we need to unref proxy so continue */
                        }
@@ -846,7 +879,7 @@ static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
        }
 
        remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
-       if(!remote_dev_info)
+       if (!remote_dev_info)
                goto dbus_return;
 
        GVariant *uuids = NULL;
@@ -932,6 +965,16 @@ int _bt_bond_device(int request_id,
 
        BT_CHECK_PARAMETER(device_address, return);
 
+#ifdef TIZEN_FEATURE_BT_DPM
+       int pairing_state = DPM_STATUS_ERROR;
+
+       _bt_dpm_get_bluetooth_pairing_state(&pairing_state);
+       if (pairing_state == DPM_RESTRICTED) {
+               BT_ERR("Not allow to pair the device");
+               return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
+       }
+#endif
+
        if (bonding_info) {
                BT_ERR("Bonding in progress");
 
@@ -945,7 +988,7 @@ int _bt_bond_device(int request_id,
                return BLUETOOTH_ERROR_DEVICE_BUSY;
        }
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(address, device_address->addr);
@@ -1053,13 +1096,16 @@ static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
                                        gpointer user_data)
 {
        GError *err = NULL;
+       GVariant *reply;
        GVariant *out_param1;
        int result = BLUETOOTH_ERROR_NONE;
        bt_funcion_data_t *unbonding_info;
        bluetooth_device_info_t dev_info;
        request_info_t *req_info;
 
-       g_dbus_proxy_call_finish(proxy, res, &err);
+       reply = g_dbus_proxy_call_finish(proxy, res, &err);
+       if (reply)
+               g_variant_unref(reply);
 
        unbonding_info = user_data;
 
@@ -1127,12 +1173,6 @@ int _bt_unbond_device(int request_id,
 
        /* allocate user data so that it can be retrieved in callback */
        unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
-       /* Fix : NULL_RETURNS */
-       if (unbonding_info == NULL) {
-               BT_ERR("Memory not allocated !");
-               return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
-       }
-
        unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
        unbonding_info->req_id = request_id;
 
@@ -1147,7 +1187,7 @@ int _bt_unbond_device(int request_id,
                goto fail;
        }
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        if (conn == NULL) {
                BT_ERR("conn is NULL");
                result = BLUETOOTH_ERROR_INTERNAL;
@@ -1211,6 +1251,7 @@ static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
                                        gpointer user_data)
 {
        GError *err = NULL;
+       GVariant *reply;
        GVariant *out_param1;
        int result = BLUETOOTH_ERROR_NONE;
        bluetooth_device_info_t dev_info;
@@ -1221,7 +1262,9 @@ static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
        GVariantBuilder *builder = NULL;
        int i = 0;
 
-       g_dbus_proxy_call_finish(proxy, res, &err);
+       reply = g_dbus_proxy_call_finish(proxy, res, &err);
+       if (reply)
+               g_variant_unref(reply);
 
        g_object_unref(proxy);
 
@@ -1241,15 +1284,14 @@ static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
                g_dbus_error_strip_remote_error(err);
                BT_ERR("Error occured in Proxy call [%s]\n", err->message);
 
-               if (g_strrstr("Operation canceled", err->message)) {
+               if (g_strrstr("Operation canceled", err->message))
                        result = BLUETOOTH_ERROR_CANCEL_BY_USER;
-               } else if (g_strrstr("In Progress", err->message)) {
+               else if (g_strrstr("In Progress", err->message))
                        result = BLUETOOTH_ERROR_IN_PROGRESS;
-               } else if (g_strrstr("Host is down", err->message)) {
+               else if (g_strrstr("Host is down", err->message))
                        result = BLUETOOTH_ERROR_HOST_DOWN;
-               } else {
+               else
                        result = BLUETOOTH_ERROR_CONNECTION_ERROR;
-               }
 
                if (result == BLUETOOTH_ERROR_HOST_DOWN ||
                     result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
@@ -1265,7 +1307,7 @@ static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
        }
 
        remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
-       if(!remote_dev_info)
+       if (!remote_dev_info)
                goto dbus_return;
 
 event:
@@ -1360,7 +1402,7 @@ int _bt_search_device(int request_id,
        _bt_convert_addr_type_to_string(searching_info->addr,
                                        device_address->addr);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
 
@@ -1439,7 +1481,7 @@ int _bt_set_alias(bluetooth_device_address_t *device_address,
        adapter_proxy = _bt_get_adapter_proxy();
        retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(address, device_address->addr);
@@ -1493,7 +1535,7 @@ int _bt_set_authorization(bluetooth_device_address_t *device_address,
 
        BT_CHECK_PARAMETER(device_address, return);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(address, device_address->addr);
@@ -1538,7 +1580,7 @@ int _bt_set_authorization(bluetooth_device_address_t *device_address,
        }
 
        result = g_dbus_proxy_call_sync(device_proxy, "Set",
-                                g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b",authorize)),
+                                g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
                                 G_DBUS_CALL_FLAGS_NONE,
                                 -1,
                                 NULL,
@@ -1573,7 +1615,7 @@ int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
 
        BT_CHECK_PARAMETER(device_address, return);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(address, device_address->addr);
@@ -1628,6 +1670,7 @@ int _bt_is_device_connected(bluetooth_device_address_t *device_address,
 {
        char *object_path = NULL;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
+       char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
        GDBusProxy *device_proxy = NULL;
        GDBusProxy *adapter_proxy = NULL;
        GDBusConnection *conn;
@@ -1640,18 +1683,24 @@ int _bt_is_device_connected(bluetooth_device_address_t *device_address,
        retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
        retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
 
+       _bt_convert_addr_type_to_string(address, device_address->addr);
+       _bt_convert_addr_string_to_secure_string(secure_address, address);
+
        *is_connected = FALSE;
-       BT_DBG("connection_type: %d", connection_type);
+       BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
+
        if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
                return _bt_rfcomm_is_device_connected(device_address,
                                                is_connected);
        else if (connection_type == BLUETOOTH_GATT_SERVICE)
                return _bt_is_gatt_connected(device_address, is_connected);
+       else if (connection_type == BLUETOOTH_PBAP_SERVICE)
+               return _bt_pbap_is_connected(device_address, is_connected);
 
        adapter_proxy = _bt_get_adapter_proxy();
        retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(address, device_address->addr);
@@ -1680,8 +1729,9 @@ int _bt_is_device_connected(bluetooth_device_address_t *device_address,
                                BT_ERR("%s", error->message);
                                g_error_free(error);
                        }
+                       *is_connected = FALSE;
                        g_object_unref(device_proxy);
-                       return BLUETOOTH_ERROR_INTERNAL;
+                       return BLUETOOTH_ERROR_NONE;
                }
                g_variant_get(result , "(@a{sv})", &value);
                g_variant_unref(result);
@@ -1702,15 +1752,18 @@ int _bt_is_device_connected(bluetooth_device_address_t *device_address,
        } else {
                uuid = _bt_get_profile_uuid128(connection_type);
                if (uuid == NULL) {
-                       BT_ERR("uuid is NULL");
+                       BT_ERR("connection_type: %d, uuid is NULL", connection_type);
                        return BLUETOOTH_ERROR_INTERNAL;
                }
-
-               BT_DBG("uuid: %s", uuid);
+               BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
 
                object_path = _bt_get_device_object_path(address);
-               retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
-               BT_DBG("object_path: %s", object_path);
+               if (!object_path) {
+                       BT_ERR("object_path is NULL");
+                       g_free(uuid);
+                       return BLUETOOTH_ERROR_NOT_PAIRED;
+               }
+
                device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
                                                                        NULL, BT_BLUEZ_NAME,
                                                                        object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
@@ -1729,16 +1782,17 @@ int _bt_is_device_connected(bluetooth_device_address_t *device_address,
                                        &error);
 
                if (result == NULL) {
-                       int ret = BLUETOOTH_ERROR_INTERNAL;
                        BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
                        if (error) {
                                BT_ERR("%s", error->message);
                                if (g_strrstr(error->message, "Not Connected"))
-                                       ret = BLUETOOTH_ERROR_NOT_CONNECTED;
+                                       BT_DBG("Not connected");
                                g_error_free(error);
                        }
+                       *is_connected = FALSE;
                        g_object_unref(device_proxy);
-                       return ret;
+                       g_free(uuid);
+                       return BLUETOOTH_ERROR_NONE;
                }
                g_variant_get(result, "(b)", is_connected);
                g_free(uuid);
@@ -1764,7 +1818,7 @@ int _bt_get_connected_link(bluetooth_device_address_t *device_address,
 
        BT_CHECK_PARAMETER(device_address, return);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(address, device_address->addr);
@@ -1871,10 +1925,11 @@ done:
        }
 }
 
-int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
-               gboolean auto_connect)
+static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
+       gboolean auto_connect)
 {
        char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
+       char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
        gchar *device_path = NULL;
        GDBusProxy *device_proxy = NULL;
        GDBusConnection *conn;
@@ -1883,7 +1938,7 @@ int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
 
        BT_CHECK_PARAMETER(bd_addr, return);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(device_address,
@@ -1909,12 +1964,10 @@ int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
                goto fail;
        }
 
-       func_data->req_id = req_id;
+       _bt_convert_addr_string_to_secure_string(secure_address, device_address);
+       BT_INFO("Connect LE [%s]", secure_address);
 
-       ret = _bt_hold_current_advertising();
-       if (ret != BLUETOOTH_ERROR_NONE) {
-               BT_ERR("Unable to hold advertising");
-       }
+       func_data->req_id = req_id;
 
        g_dbus_proxy_call(device_proxy, "ConnectLE",
                        g_variant_new("(b)", auto_connect),
@@ -1928,17 +1981,96 @@ int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
 fail:
        if (device_proxy)
                g_object_unref(device_proxy);
-       if (func_data) {
-               g_free(func_data->address);
-               g_free(func_data);
-       }
+
+       g_free(func_data->address);
+       g_free(func_data);
+
        return ret;
 }
 
+static gboolean __bt_connect_le_timer_cb(gpointer user_data)
+{
+       int ret;
+
+       BT_INFO("Try to initiate pending LE connection");
+
+       pending_le_conn_timer_id = 0;
+
+       ret = __bt_connect_le_device_internal(pending_le_conn_info->req_id,
+                       &pending_le_conn_info->bd_addr,
+                       pending_le_conn_info->auto_connect);
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               request_info_t *req_info = NULL;
+               GVariant *out_param1;
+
+               req_info = _bt_get_request_info(pending_le_conn_info->req_id);
+               if (req_info == NULL) {
+                       BT_ERR("req_info is NULL");
+                       goto done;
+               }
+
+               if (req_info->context == NULL) {
+                       BT_ERR("req_info->context is NULL");
+                       _bt_delete_request_list(req_info->req_id);
+                       goto done;
+               }
+
+               out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
+                               &pending_le_conn_info->bd_addr,
+                               sizeof(bluetooth_device_address_t), TRUE,
+                               NULL, NULL);
+
+               g_dbus_method_invocation_return_value(req_info->context,
+                               g_variant_new("(iv)", ret, out_param1));
+       }
+
+done:
+       g_free(pending_le_conn_info);
+       pending_le_conn_info = NULL;
+
+       return FALSE;
+}
+
+void _bt_pending_connect_le_device(void)
+{
+       if (pending_le_conn_timer_id > 0) {
+               g_source_remove(pending_le_conn_timer_id);
+               __bt_connect_le_timer_cb(NULL);
+       }
+}
+
+int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
+               gboolean auto_connect)
+{
+       int ret = BLUETOOTH_ERROR_NONE;
+
+       BT_CHECK_PARAMETER(bd_addr, return);
+
+       ret = _bt_hold_current_advertising();
+       if (ret == BLUETOOTH_ERROR_NONE) {
+               BT_INFO("Current advertising is held");
+               pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
+               pending_le_conn_info->req_id = req_id;
+               memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
+                               BLUETOOTH_ADDRESS_LENGTH);
+               pending_le_conn_info->auto_connect = auto_connect;
+
+               pending_le_conn_timer_id =
+                       g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
+
+               return BLUETOOTH_ERROR_NONE;
+       } else if (ret != BLUETOOTH_ERROR_NOT_IN_OPERATION) {
+               BT_ERR("Unable to hold advertising");
+       }
+
+       return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
+}
+
 int _bt_disconnect_le_device(int req_id,
                const bluetooth_device_address_t *bd_addr)
 {
        char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
+       char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
        gchar *device_path;
        GDBusProxy *device_proxy;
        GDBusConnection *conn;
@@ -1947,7 +2079,7 @@ int _bt_disconnect_le_device(int req_id,
 
        BT_CHECK_PARAMETER(bd_addr, return);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(device_address,
@@ -1975,6 +2107,9 @@ int _bt_disconnect_le_device(int req_id,
                goto fail;
        }
 
+       _bt_convert_addr_string_to_secure_string(secure_address, device_address);
+       BT_INFO("Disconnect LE [%s]", secure_address);
+
        func_data->req_id = req_id;
 
        g_dbus_proxy_call(device_proxy, "DisconnectLE",
@@ -1988,10 +2123,10 @@ int _bt_disconnect_le_device(int req_id,
 fail:
        if (device_proxy)
                g_object_unref(device_proxy);
-       if (func_data) {
-               g_free(func_data->address);
-               g_free(func_data);
-       }
+
+       g_free(func_data->address);
+       g_free(func_data);
+
        return ret;
 }
 
@@ -2010,7 +2145,7 @@ int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
        _bt_convert_addr_type_to_string(device_address,
                        (unsigned char *)bd_addr->addr);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        adapter_proxy = _bt_get_adapter_proxy();
@@ -2043,9 +2178,18 @@ int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
                g_object_unref(device_proxy);
                return BLUETOOTH_ERROR_INTERNAL;
        }
-
        g_object_unref(device_proxy);
 
+       /* IPSP daemon launch */
+       GDBusProxy *ipsp_proxy;
+
+       ipsp_proxy = _bt_get_ipsp_proxy();
+       retv_if(ipsp_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+       g_dbus_proxy_call(ipsp_proxy, "EnableIpsp",
+                       NULL, G_DBUS_CALL_FLAGS_NONE,
+                       -1, NULL, NULL, NULL);
+
        return ret;
 }
 
@@ -2064,7 +2208,7 @@ int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
        _bt_convert_addr_type_to_string(device_address,
                        (unsigned char *)bd_addr->addr);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        adapter_proxy = _bt_get_adapter_proxy();
@@ -2113,7 +2257,7 @@ int _bt_connect_profile(char *address, char *uuid,
        GVariant *result = NULL;
        GError *error = NULL;
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        object_path = _bt_get_device_object_path(address);
@@ -2158,6 +2302,47 @@ int _bt_connect_profile(char *address, char *uuid,
        return BLUETOOTH_ERROR_NONE;
 }
 
+int _bt_disconnect_all(char *address)
+{
+       int ret = BLUETOOTH_ERROR_NONE;
+       char *object_path;
+       GDBusProxy *proxy;
+       GDBusConnection *conn;
+       GVariant *result = NULL;
+       GError *err = NULL;
+
+       BT_DBG("");
+       conn = _bt_gdbus_get_system_gconn();
+       retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+       object_path = _bt_get_device_object_path(address);
+       retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+       proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+                                       NULL, BT_BLUEZ_NAME,
+                                       object_path, BT_DEVICE_INTERFACE,  NULL, NULL);
+       g_free(object_path);
+       retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
+
+       result = g_dbus_proxy_call_sync(proxy, "Disconnect",
+               NULL,
+               G_DBUS_CALL_FLAGS_NONE,
+               -1, NULL,
+               &err);
+
+       if (err != NULL) {
+               BT_ERR("Dbus Call Error:[%s]", err->message);
+               g_error_free(err);
+               ret = BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       g_object_unref(proxy);
+       if (result)
+               g_variant_unref(result);
+
+       return ret;
+}
+
 int _bt_disconnect_profile(char *address, char *uuid,
                                                void *cb, gpointer func_data)
 {
@@ -2165,7 +2350,7 @@ int _bt_disconnect_profile(char *address, char *uuid,
        GDBusProxy *proxy;
        GDBusConnection *conn;
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        object_path = _bt_get_device_object_path(address);
@@ -2269,59 +2454,24 @@ int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
        return ret;
 }
 
-int _bt_le_conn_update(unsigned char *device_address,
+static int __bt_le_set_conn_parameter(const char *address,
                                float interval_min, float interval_max,
                                guint16 latency, guint16 time_out)
 {
-       char address[BT_ADDRESS_STRING_SIZE] = { 0 };
        gchar *device_path = NULL;
        GError *error = NULL;
        GDBusProxy *device_proxy = NULL;
        GDBusConnection *conn;
        GVariant *reply;
        guint32 min, max, to;
-       guint32 min_supervision_to;
        int ret = BLUETOOTH_ERROR_NONE;
 
-       BT_CHECK_PARAMETER(device_address, return);
+       BT_CHECK_PARAMETER(address, return);
 
        BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
                        interval_min, interval_max, latency, time_out);
 
-       if (interval_min > interval_max ||
-                       interval_min < BT_LE_CONN_INTERVAL_MIN ||
-                       interval_max > BT_LE_CONN_INTERVAL_MAX) {
-               ret = BLUETOOTH_ERROR_INVALID_PARAM;
-               goto fail;
-       }
-
-       if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
-                       time_out > BT_LE_CONN_SUPER_TO_MAX) {
-               ret = BLUETOOTH_ERROR_INVALID_PARAM;
-               goto fail;
-       }
-
-       if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
-               ret = BLUETOOTH_ERROR_INVALID_PARAM;
-               goto fail;
-       }
-
-       /*
-        * The Supervision_Timeout in milliseconds shall be larger than
-        * (1 + Conn_Latency) * Conn_Interval_Max * 2,
-        * where Conn_Interval_Max is given in milliseconds.
-        */
-       min_supervision_to = (1 + latency) * interval_max * 2;
-       if (time_out <= min_supervision_to) {
-               ret = BLUETOOTH_ERROR_INVALID_PARAM;
-               goto fail;
-       }
-
-       _bt_convert_addr_type_to_string(address, device_address);
-
-       BT_DBG("Remote device address: %s", address);
-
-       device_path = _bt_get_device_object_path(address);
+       device_path = _bt_get_device_object_path((char *)address);
 
        if (device_path == NULL) {
                BT_ERR("device_path NULL");
@@ -2329,7 +2479,7 @@ int _bt_le_conn_update(unsigned char *device_address,
                goto fail;
        }
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        if (conn == NULL) {
                BT_ERR("conn NULL");
                ret = BLUETOOTH_ERROR_INTERNAL;
@@ -2372,6 +2522,306 @@ fail:
        return ret;
 }
 
+static void __bt_le_conn_param_free(void *data)
+{
+       bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
+
+       BT_DBG("%s", param->sender);
+       g_free(param->sender);
+       g_free(param);
+}
+
+static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
+{
+       bt_le_conn_param_t *parama= (bt_le_conn_param_t *)a;
+       bt_le_conn_param_t *paramb= (bt_le_conn_param_t *)b;
+
+       return parama->key > paramb->key;
+}
+
+static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
+{
+       GSList *l = NULL;
+       bt_connected_le_dev_t *dev;
+
+       if (!address)
+               return NULL;
+
+       for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
+               dev = l->data;
+
+               if (g_strcmp0(dev->address, address) == 0)
+                       return dev;
+       }
+       return NULL;
+}
+
+static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
+{
+       GSList *l = NULL;
+       bt_le_conn_param_t *param = NULL;
+
+       if (!dev || !sender)
+               return NULL;
+
+       for (l = dev->senders; l; l = g_slist_next(l)) {
+               param = l->data;
+               if (g_strcmp0(param->sender, sender) == 0)
+                       return param;
+       }
+
+       return NULL;
+}
+
+int _bt_add_le_conn_param_info(const char *address, const char *sender,
+                       float interval_min, float interval_max, guint16 latency, guint16 time_out)
+{
+       bt_connected_le_dev_t *dev = NULL;
+       bt_le_conn_param_t *param = NULL;
+       bt_le_conn_param_t *data = NULL;
+
+       if (!address || !sender)
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+
+       dev = __bt_get_le_connected_dev_info(address);
+       if (!dev)
+               return BLUETOOTH_ERROR_INTERNAL;
+
+       param = __bt_get_le_conn_param_info(dev, sender);
+
+       data = g_malloc0(sizeof(bt_le_conn_param_t));
+       data->sender = g_strdup(sender);
+       data->interval_min = interval_min;
+       data->interval_max = interval_max;
+       data->latency = latency;
+       data->time_out = time_out;
+       data->key = interval_min + (interval_max - interval_min)/2;
+
+       if (param == NULL) {
+               BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
+               dev->senders = g_slist_append(dev->senders, data);
+       } else {
+               BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
+               dev->senders = g_slist_remove(dev->senders, param);
+               g_free(param->sender);
+               g_free(param);
+               dev->senders = g_slist_append(dev->senders, data);
+       }
+
+       /* Sorting. First element have the minimum interval */
+       dev->senders = g_slist_sort(dev->senders,
+                                       (GCompareFunc)__bt_compare_le_conn_param_key);
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_remove_le_conn_param_info(const char *address, const char *sender, gboolean *is_removed)
+{
+       bt_connected_le_dev_t *dev = NULL;
+       bt_le_conn_param_t *param = NULL;
+
+       if (!address || !sender)
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+
+       dev = __bt_get_le_connected_dev_info(address);
+       if (!dev)
+               return BLUETOOTH_ERROR_INTERNAL;
+
+       param = __bt_get_le_conn_param_info(dev, sender);
+       if (param) {
+               BT_DBG("Remove param %s %s ", address, sender);
+               dev->senders = g_slist_remove(dev->senders, param);
+               g_free(param->sender);
+               g_free(param);
+               *is_removed = TRUE;
+       } else
+               *is_removed = FALSE;
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_remove_all_le_conn_param_info(const char *sender)
+{
+       GSList *l = NULL;
+       bt_connected_le_dev_t *dev = NULL;
+       bt_le_conn_param_t *param = NULL;
+       gboolean is_removed = FALSE;
+       char *sender_new = NULL;
+       unsigned char addr[BLUETOOTH_ADDRESS_LENGTH];
+       int ret = BLUETOOTH_ERROR_NONE;
+
+       if (!sender)
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+
+       for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
+               dev = l->data;
+               _bt_remove_le_conn_param_info(dev->address, sender, &is_removed);
+
+               if (is_removed) {
+                       BT_INFO("Sender terminated. Update le conn interval [senders %d]",
+                                               g_slist_length(dev->senders));
+                       if (g_slist_length(dev->senders) > 0) {
+                               param = dev->senders->data;
+                               BT_DBG("dev %f %f, param %f %f", dev->interval_min, dev->interval_max,
+                                               param->interval_min, param->interval_max);
+
+                               if (dev->interval_min != param->interval_min ||
+                                       dev->interval_max != param->interval_max) {
+                                       sender_new = g_strdup(param->sender);
+
+                                       _bt_convert_addr_string_to_type(addr, dev->address);
+                                       ret = _bt_le_conn_update(sender_new, addr,
+                                                               param->interval_min,  param->interval_max,
+                                                               param->latency, param->time_out);
+                                       g_free(sender_new);
+
+                                       if (ret != BLUETOOTH_ERROR_NONE)
+                                               BT_ERR("Unable to set le connection parameter");
+                               }
+                       } else {
+                               BT_INFO("Set the default interval");
+
+                               bluetooth_le_connection_param_t param = { 0 };
+                                _bt_get_le_connection_parameter(
+                                                       BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER,
+                                                       &param);
+
+                               ret = __bt_le_set_conn_parameter(dev->address,
+                                                               param.interval_min, param.interval_max,
+                                                               param.latency, param.timeout);
+                               if (ret == BLUETOOTH_ERROR_NONE) {
+                                       dev->interval_min = param.interval_min;
+                                       dev->interval_max = param.interval_max;
+                               }
+                       }
+               }
+       }
+
+       return ret;
+}
+
+void _bt_add_le_connected_dev_info(const char *address)
+{
+       bt_connected_le_dev_t *dev = NULL;
+
+       if (!address)
+               return;
+
+       dev = g_malloc0(sizeof(bt_connected_le_dev_t));
+       dev->address = g_strdup(address);
+
+       le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
+
+       return;
+}
+
+void _bt_remove_le_connected_dev_info(const char *address)
+{
+       bt_connected_le_dev_t *dev = NULL;
+
+       if (!address)
+               return;
+
+       dev = __bt_get_le_connected_dev_info(address);
+       if (!dev)
+               return;
+
+       g_slist_free_full(dev->senders, __bt_le_conn_param_free);
+       le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
+       g_free(dev->address);
+       g_free(dev);
+
+       return;
+}
+
+int _bt_le_conn_update(const char *sender,
+                               unsigned char *device_address,
+                               float interval_min, float interval_max,
+                               guint16 latency, guint16 time_out)
+{
+       char address[BT_ADDRESS_STRING_SIZE] = { 0 };
+       guint32 min_supervision_to;
+       bt_connected_le_dev_t *dev = NULL;
+       bt_le_conn_param_t *param = NULL;
+       gboolean is_removed = FALSE;
+       int ret = BLUETOOTH_ERROR_NONE;
+
+       BT_CHECK_PARAMETER(device_address, return);
+
+       BT_INFO("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 ||
+                       interval_min < BT_LE_CONN_INTERVAL_MIN ||
+                       interval_max > BT_LE_CONN_INTERVAL_MAX) {
+               ret = BLUETOOTH_ERROR_INVALID_PARAM;
+               goto fail;
+       }
+
+       if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
+                       time_out > BT_LE_CONN_SUPER_TO_MAX) {
+               ret = BLUETOOTH_ERROR_INVALID_PARAM;
+               goto fail;
+       }
+
+       if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
+               ret = BLUETOOTH_ERROR_INVALID_PARAM;
+               goto fail;
+       }
+
+       /*
+        * The Supervision_Timeout in milliseconds shall be larger than
+        * (1 + Conn_Latency) * Conn_Interval_Max * 2,
+        * where Conn_Interval_Max is given in milliseconds.
+        */
+       min_supervision_to = (1 + latency) * interval_max * 2;
+       if (time_out <= min_supervision_to) {
+               ret = BLUETOOTH_ERROR_INVALID_PARAM;
+               goto fail;
+       }
+
+       _bt_convert_addr_type_to_string(address, device_address);
+       BT_DBG("Remote device address: %s", address);
+
+       _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
+
+       dev = __bt_get_le_connected_dev_info(address);
+       if (dev == NULL) {
+               ret = BLUETOOTH_ERROR_NOT_CONNECTED;
+               goto fail;
+       }
+
+       if (g_slist_length(dev->senders) == 1)
+               goto update;
+       else {
+               param = dev->senders->data;
+
+               BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
+
+               if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
+                       BT_DBG("Skip due to same interval");
+                       return ret;
+               }
+
+               interval_min = param->interval_min;
+               interval_max = param->interval_max;
+       }
+
+update:
+       ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
+
+       if (ret != BLUETOOTH_ERROR_NONE) {
+               _bt_remove_le_conn_param_info(address, sender, &is_removed);
+               return ret;
+       }
+
+       dev->interval_min = interval_min;
+       dev->interval_max = interval_max;
+
+fail:
+       return ret;
+}
+
 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
                                bluetooth_device_pin_code_t *pin_code)
 {
@@ -2589,13 +3039,13 @@ char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
 
 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
 {
-       if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0) {
+       if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
                return TRUSTED_PROFILE_PBAP;
-       } else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0) {
+       else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
                return TRUSTED_PROFILE_MAP;
-       } else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0) {
+       else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
                return TRUSTED_PROFILE_SAP;
-       }
+
        return 0; /* 0 - Unknown Profile */
 }
 
@@ -2618,7 +3068,7 @@ int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
                        bd_addr->addr[4], bd_addr->addr[5],
                        profile, trust);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(address, bd_addr->addr);
@@ -2680,7 +3130,7 @@ int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
                        bd_addr->addr[4], bd_addr->addr[5],
                        profile, *trust);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(address, bd_addr->addr);
@@ -2744,7 +3194,7 @@ int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
                        bd_addr->addr[4], bd_addr->addr[5],
                        profile, restricted);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(address, bd_addr->addr);
@@ -2806,7 +3256,7 @@ int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
                        bd_addr->addr[4], bd_addr->addr[5],
                        profile, *restricted);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(address, bd_addr->addr);
@@ -2900,8 +3350,8 @@ static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
                goto dbus_return;
 
        if (val) {
-                g_variant_get (val, "(qy)", &mtu, &status);
-                g_variant_unref (val);
+                g_variant_get(val, "(qy)", &mtu, &status);
+                g_variant_unref(val);
        }
 
        BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
@@ -2958,7 +3408,7 @@ int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_addre
                return BLUETOOTH_ERROR_DEVICE_BUSY;
        }
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        adapter_proxy = _bt_get_adapter_proxy();
@@ -2978,12 +3428,6 @@ int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_addre
        g_free(device_path);
        retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       if (device_path == NULL) {
-               BT_DBG("device_path NULL");
-               ret = BLUETOOTH_ERROR_INTERNAL;
-               goto fail;
-       }
-
        att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
        att_mtu_req_info->addr = (char *)g_strdup(address);
        att_mtu_req_info->req_id = request_id;
@@ -2996,7 +3440,7 @@ int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_addre
                                NULL,
                                (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
                                NULL);
-fail:
+
        return ret;
 }
 
@@ -3016,7 +3460,7 @@ int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
 
        BT_CHECK_PARAMETER(device_address, return);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(address, device_address->addr);
@@ -3080,7 +3524,7 @@ int _bt_get_device_ida(bluetooth_device_address_t *device_address,
 
        BT_CHECK_PARAMETER(device_address, return);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        _bt_convert_addr_type_to_string(address, device_address->addr);
@@ -3125,11 +3569,10 @@ int _bt_get_device_ida(bluetooth_device_address_t *device_address,
 
        BT_DBG("ID Address:%s", idaddress);
 
-       if (idaddress) {
+       if (idaddress)
                _bt_convert_addr_string_to_type(id_address->addr, idaddress);
-       } else {
+       else
                ret = BLUETOOTH_ERROR_INTERNAL;
-       }
 
        g_object_unref(device_proxy);