Use FindDevice to get device object path
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-network.c
index e175573..00140da 100644 (file)
@@ -148,8 +148,9 @@ static void __bt_network_connect_cb(GDBusProxy *proxy, GAsyncResult *res,
                        g_clear_error(&g_error);
                }
                result = BLUETOOTH_ERROR_INTERNAL;
+       } else {
+               g_variant_unref(reply);
        }
-       g_variant_unref(reply);
 
        func_data = user_data;
        if (func_data == NULL) {
@@ -207,8 +208,9 @@ static void __bt_network_disconnect_cb(GDBusProxy *proxy, GAsyncResult *res,
                        g_clear_error(&g_error);
                }
                result = BLUETOOTH_ERROR_INTERNAL;
+       } else {
+               g_variant_unref(reply);
        }
-       g_variant_unref(reply);
 
        func_data = user_data;
        if (func_data == NULL) {
@@ -255,7 +257,7 @@ int _bt_network_activate(void)
        GDBusConnection *conn;
        GDBusProxy *server_proxy;
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        adapter_path = _bt_get_adapter_path();
@@ -281,11 +283,11 @@ int _bt_network_activate(void)
                if (err != NULL) {
                        g_dbus_error_strip_remote_error(err);
                        BT_ERR("Network server register Error: %s\n", err->message);
-                       if (g_strcmp0(err->message, "Already Exists") == 0) {
+                       if (g_strcmp0(err->message, "Already Exists") == 0)
                                ret = BLUETOOTH_ERROR_ALREADY_INITIALIZED;
-                       } else {
+                       else
                                ret = BLUETOOTH_ERROR_INTERNAL;
-                       }
+
                        g_error_free(err);
                }
        } else {
@@ -306,7 +308,7 @@ int _bt_network_deactivate(void)
        GDBusProxy *server_proxy;
        int ret = BLUETOOTH_ERROR_NONE;
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        adapter_path = _bt_get_adapter_path();
@@ -352,15 +354,13 @@ int _bt_network_deactivate(void)
 int _bt_network_connect(int request_id, int role,
                bluetooth_device_address_t *device_address)
 {
-       const gchar *device_path = NULL;
+       gchar *device_path = NULL;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
        char remote_role[BLUETOOTH_UUID_STRING_MAX] = { 0 };
        bt_function_data_t *func_data;
        GDBusProxy *adapter_proxy;
        GDBusProxy *profile_proxy;
        GDBusConnection *conn;
-       GVariant *result = NULL;
-       GError*err = NULL;
 
        BT_CHECK_PARAMETER(device_address, return);
 
@@ -384,28 +384,14 @@ int _bt_network_connect(int request_id, int role,
        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);
 
-       result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
-                       g_variant_new("(s)", address),
-                       G_DBUS_CALL_FLAGS_NONE,
-                       -1, NULL, &err);
-       if (result == NULL) {
-               BT_ERR("Error occurred in call to FindDevice");
-               if (err) {
-                       BT_ERR("Error: %s", err->message);
-                       g_clear_error(&err);
-               }
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
-
-       device_path =  g_variant_get_string(result, NULL);
+       device_path = _bt_get_device_object_path(address);
        if (device_path == NULL) {
                BT_ERR("No paired device");
-               g_variant_unref(result);
                return BLUETOOTH_ERROR_NOT_PAIRED;
        }
 
@@ -413,11 +399,9 @@ int _bt_network_connect(int request_id, int role,
                        NULL, BT_BLUEZ_NAME,
                        device_path, BT_NETWORK_CLIENT_INTERFACE,  NULL, NULL);
 
-       g_variant_unref(result);
+       g_free(device_path);
        retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
        func_data = g_malloc0(sizeof(bt_function_data_t));
-       retv_if(func_data == NULL, BLUETOOTH_ERROR_MEMORY_ALLOCATION);
-
        func_data->address = g_strdup(address);
        func_data->req_id = request_id;
 
@@ -435,42 +419,26 @@ int _bt_network_connect(int request_id, int role,
 int _bt_network_disconnect(int request_id,
                bluetooth_device_address_t *device_address)
 {
-       const gchar *device_path = NULL;
+       gchar *device_path = NULL;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
        bt_function_data_t *func_data;
        GDBusProxy *adapter_proxy;
        GDBusProxy *profile_proxy;
        GDBusConnection *conn;
-       GVariant *result = NULL;
-       GError*err = NULL;
 
        BT_CHECK_PARAMETER(device_address, return);
 
        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);
 
-       result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
-                                g_variant_new("(s)", address),
-                                G_DBUS_CALL_FLAGS_NONE,
-                                -1, NULL, &err);
-       if (result == NULL) {
-               BT_ERR("Error occurred in call to FindDevice");
-               if (err) {
-                       BT_ERR("Error: %s", err->message);
-                       g_clear_error(&err);
-               }
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
-
-       device_path =  g_variant_get_string(result, NULL);
+       device_path = _bt_get_device_object_path(address);
        if (device_path == NULL) {
                BT_ERR("No paired device");
-               g_variant_unref(result);
                return BLUETOOTH_ERROR_NOT_PAIRED;
        }
 
@@ -478,11 +446,9 @@ int _bt_network_disconnect(int request_id,
                        NULL, BT_BLUEZ_NAME,
                        device_path, BT_NETWORK_CLIENT_INTERFACE,  NULL, NULL);
 
-       g_variant_unref(result);
+       g_free(device_path);
        retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
        func_data = g_malloc0(sizeof(bt_function_data_t));
-       retv_if(func_data == NULL, BLUETOOTH_ERROR_MEMORY_ALLOCATION);
-
        func_data->address = g_strdup(address);
        func_data->req_id = request_id;
 
@@ -508,7 +474,7 @@ int _bt_network_server_disconnect(int request_id,
 
        BT_CHECK_PARAMETER(device_address, return);
 
-       conn = _bt_get_system_gconn();
+       conn = _bt_gdbus_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        adapter_path = _bt_get_adapter_path();