Remove After tag to remove cyclic dependency
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-device.c
index eca0775..e7002e9 100644 (file)
@@ -122,9 +122,6 @@ static guint pending_le_conn_timer_id = 0;
 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
                                        gpointer user_data);
 
-static int __bt_retry_bond(void);
-
-
 /*static void __bt_decline_pair_request()
 {
        GVariant *out_param1;
@@ -519,20 +516,70 @@ bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
        return dev_info;
 }
 
-char *_bt_get_bonded_device_name(char *address)
+char *_bt_get_device_name(const char *bdaddress)
 {
-       bluetooth_device_address_t device_address = { {0} };
-       bluetooth_device_info_t dev_info;
+       char *device_path = NULL;
+       const gchar *name = NULL;
+       gchar *dev_name = NULL;
+       gsize name_len = 0;
+       GVariant *result = NULL;
+       GError *err = NULL;
+       GDBusProxy *device_proxy;
+       GDBusConnection *conn;
 
-       retv_if(address == NULL, strdup(""));
+       retv_if(bdaddress == NULL, NULL);
 
-       _bt_convert_addr_string_to_type(device_address.addr, address);
+       device_path = _bt_get_device_object_path((char *)bdaddress);
+       retv_if(device_path == NULL, NULL);
 
-       memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
+       conn = _bt_gdbus_get_system_gconn();
+       retv_if(conn == NULL, NULL);
+       INFO_SECURE("Device_path %s", device_path);
+       device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+                               NULL, BT_BLUEZ_NAME, device_path,
+                               BT_PROPERTIES_INTERFACE, NULL, &err);
+
+       g_free(device_path);
+       retv_if(device_proxy == NULL, NULL);
+
+       result = g_dbus_proxy_call_sync(device_proxy, "Get",
+                       g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Alias"),
+                       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+       if (err) {
+               BT_ERR("DBus Error : %s", err->message);
+               g_clear_error(&err);
+       } else {
+               GVariant *value;
+               g_variant_get(result, "(v)", &value);
+               name = g_variant_get_string(value, &name_len);
+               INFO_SECURE("Alias Name [%s]", name);
+               if (name_len)
+                       dev_name = g_strdup(name);
+               g_variant_unref(value);
+               g_variant_unref(result);
+       }
 
-       _bt_get_bonded_device_info(&device_address, &dev_info);
+       if (name_len == 0) {
+               GVariant *value;
+               result = g_dbus_proxy_call_sync(device_proxy, "Get",
+                       g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Name"),
+                       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+               if (err) {
+                       ERR("DBus Error : %s", err->message);
+                       g_clear_error(&err);
+               } else {
+                       g_variant_get(result, "(v)", &value);
+                       name = g_variant_get_string(value, &name_len);
+                       INFO_SECURE("Name = %s", name);
+                       if (name_len)
+                               dev_name = g_strdup(name);
+                       g_variant_unref(value);
+                       g_variant_unref(result);
+               }
+       }
 
-       return g_strdup(dev_info.device_name.name);
+       g_object_unref(device_proxy);
+       return dev_name;
 }
 
 static gboolean __ignore_auto_pairing_request(const char *address)
@@ -641,81 +688,6 @@ static int __bt_cancel_bonding(void)
        return BLUETOOTH_ERROR_NONE;
 }
 
-static int __bt_retry_bond(void)
-{
-       BT_CHECK_PARAMETER(bonding_info, return);
-       BT_CHECK_PARAMETER(bonding_info->addr, return);
-
-       g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
-                               g_variant_new("(y)", bonding_info->conn_type),
-                               G_DBUS_CALL_FLAGS_NONE,
-                               BT_MAX_DBUS_TIMEOUT,
-                               NULL,
-                               (GAsyncReadyCallback)__bt_bond_device_cb,
-                               NULL);
-
-       return BLUETOOTH_ERROR_NONE;
-}
-
-
-static int __bt_remove_and_bond(void)
-{
-       GDBusProxy *adapter_proxy;
-       GVariant *result = NULL;
-       GError *err = NULL;
-       char *device_path = NULL;
-
-       BT_CHECK_PARAMETER(bonding_info, return);
-       BT_CHECK_PARAMETER(bonding_info->addr, return);
-
-       adapter_proxy = _bt_get_adapter_proxy();
-       retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
-                               g_variant_new("(s)", bonding_info->addr),
-                               G_DBUS_CALL_FLAGS_NONE,
-                               -1,
-                               NULL,
-                               NULL);
-       if (result == NULL)
-               return BLUETOOTH_ERROR_INTERNAL;
-
-       g_variant_get(result , "(o)", &device_path);
-       g_variant_unref(result);
-
-       retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
-                               g_variant_new("(o)", device_path),
-                               G_DBUS_CALL_FLAGS_NONE,
-                               -1,
-                               NULL,
-                               &err);
-       g_free(device_path);
-       if (result == NULL) {
-               if (err != NULL) {
-                       BT_ERR("UnpairDevice Fail: %s", err->message);
-                       g_error_free(err);
-               }
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
-
-       return __bt_retry_bond();
-}
-
-static int __bt_cancel_and_bond(void)
-{
-       int ret = BLUETOOTH_ERROR_NONE;
-
-       ret = _bt_agent_reply_cancellation();
-       if (ret != BLUETOOTH_ERROR_NONE) {
-               BT_ERR("Fail to call reply cancellation");
-               return ret;
-       }
-       return __bt_retry_bond();
-}
-
-
 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
                                        gpointer user_data)
 {
@@ -757,10 +729,8 @@ static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
                BT_ERR("Error occured in CreateBonding [%s]", err->message);
 
                if (g_strrstr(err->message, "Already Exists")) {
-                       BT_INFO("Existing Bond, remove and retry");
-                       ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
-
-                       result = BLUETOOTH_ERROR_PARING_FAILED;
+                       BT_INFO("Existing Bond");
+                       result = BLUETOOTH_ERROR_ALREADY_CONNECT;
                } else if (_bt_agent_is_canceled() ||
                        g_strrstr(err->message, "Authentication Canceled")) {
                        BT_INFO("Cancelled by USER");
@@ -769,10 +739,8 @@ static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
                        BT_INFO("REJECTED");
                        result = BLUETOOTH_ERROR_ACCESS_DENIED;
                } else if (g_strrstr(err->message, "In Progress")) {
-                       BT_INFO("Bond in progress, cancel and retry");
-                       ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
-
-                       result = BLUETOOTH_ERROR_PARING_FAILED;
+                       BT_INFO("Bond in progress");
+                       result = BLUETOOTH_ERROR_IN_PROGRESS;
                } else if (g_strrstr(err->message, "Authentication Failed")) {
                        BT_INFO("Authentication Failed");
                        if (bonding_info->is_autopair == TRUE) {
@@ -2223,6 +2191,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)
 {
@@ -3407,22 +3416,23 @@ int _bt_get_device_ida(bluetooth_device_address_t *device_address,
                        G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
        if (result == NULL) {
                BT_ERR("Failed to get device ID address");
+
+               ret = BLUETOOTH_ERROR_INTERNAL;
                if (error != NULL) {
+                       g_dbus_error_strip_remote_error(error);
                        BT_ERR("Error occured in Proxy call [%s]", error->message);
+                       if (g_strcmp0(error->message, "Does Not Exist") == 0)
+                               ret = BLUETOOTH_ERROR_NOT_PAIRED;
                        g_error_free(error);
                }
                g_object_unref(device_proxy);
-               return BLUETOOTH_ERROR_INTERNAL;
+               return ret;
        }
 
        g_variant_get(result , "(&s)", &idaddress);
-       if (idaddress == NULL) {
-               BT_ERR("No paired device");
-               ret = BLUETOOTH_ERROR_NOT_PAIRED;
-       } else {
-               DBG_SECURE("ID Address : %s", idaddress);
-               _bt_convert_addr_string_to_type(id_address->addr, idaddress);
-       }
+
+       DBG_SECURE("ID Address : %s", idaddress);
+       _bt_convert_addr_string_to_type(id_address->addr, idaddress);
 
        g_variant_unref(result);
        g_object_unref(device_proxy);