Update the RFCOMM server connection information
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-pbap.c
index 9cbf004..9132225 100644 (file)
@@ -110,6 +110,7 @@ char *SEARCH_FIELD[] = {
 static char *g_pbap_session_path = NULL;
 static char *g_pbap_server_address = NULL;
 static GDBusProxy *g_pbap_proxy = NULL;
+static gboolean g_is_pbap_connecting;
 
 static struct {
        int type;
@@ -253,6 +254,26 @@ void _bt_obex_pbap_client_disconnect(char *path)
        BT_DBG("-");
 }
 
+static int __bt_pbap_get_error(const char *error_message)
+{
+       if (error_message == NULL) {
+               BT_ERR("Error message NULL");
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       BT_ERR("Error message = %s", error_message);
+       if (g_strcmp0(error_message, "Unable to find service record") == 0)
+               return BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
+       else if (g_strcmp0(error_message, "Transport got disconnected") == 0)
+               return BLUETOOTH_ERROR_AUTHORIZATION_REJECTED;
+       else if (g_str_has_prefix(error_message, "Connection refused") == 0)
+               return BLUETOOTH_ERROR_AUTHENTICATION_REJECTED;
+       else if (g_strcmp0(error_message, "Timed out waiting for response") == 0)
+               return BLUETOOTH_ERROR_TIMEOUT;
+       else
+               return BLUETOOTH_ERROR_INTERNAL;
+}
+
 void __bt_pbap_connect_cb(GDBusProxy *proxy,
                GAsyncResult *res, gpointer user_data)
 {
@@ -269,9 +290,11 @@ void __bt_pbap_connect_cb(GDBusProxy *proxy,
        if (value == NULL) {
                BT_ERR("g_dbus_proxy_call_finish failed");
                if (error) {
-                       BT_ERR("errCode[%x], message[%s]\n",
-                                       error->code, error->message);
-                       g_clear_error(&error);
+                       g_dbus_error_strip_remote_error(error);
+                       result = __bt_pbap_get_error(error->message);
+                       BT_ERR("Failed to coonect with error[0x%x][%s]",
+                                       result, error->message);
+                       g_error_free(error);
                }
                g_object_unref(g_pbap_proxy);
                g_pbap_proxy = NULL;
@@ -294,6 +317,8 @@ void __bt_pbap_connect_cb(GDBusProxy *proxy,
 
        g_free(address_string);
        BT_DBG("-");
+
+       g_is_pbap_connecting = FALSE;
 }
 
 int _bt_pbap_connect(const bluetooth_device_address_t *address)
@@ -312,6 +337,9 @@ int _bt_pbap_connect(const bluetooth_device_address_t *address)
        if (g_pbap_session_path)
                return BLUETOOTH_ERROR_ALREADY_CONNECT;
 
+       if (g_is_pbap_connecting == TRUE)
+               return BLUETOOTH_ERROR_IN_PROGRESS;
+
        BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X]",
                        address->addr[0], address->addr[1],
                        address->addr[2], address->addr[3],
@@ -322,7 +350,7 @@ int _bt_pbap_connect(const bluetooth_device_address_t *address)
        g_conn = _bt_gdbus_get_session_gconn();
        if (g_conn == NULL) {
                        BT_ERR("Couldn't connect to session bus");
-                       return EXIT_FAILURE;
+                       return BLUETOOTH_ERROR_INTERNAL;
        }
        g_pbap_proxy =  g_dbus_proxy_new_sync(g_conn,
                        G_DBUS_PROXY_FLAGS_NONE, NULL,
@@ -334,7 +362,7 @@ int _bt_pbap_connect(const bluetooth_device_address_t *address)
                        ERR("Unable to create proxy: %s", error->message);
                        g_clear_error(&error);
                }
-               return -1;
+               return BLUETOOTH_ERROR_INTERNAL;
        }
 
        /* Create Hash*/
@@ -347,13 +375,14 @@ int _bt_pbap_connect(const bluetooth_device_address_t *address)
 
        GVariant *temp = g_variant_new("(s@a{sv})", ptr, args);
 
+       g_is_pbap_connecting = TRUE;
        g_dbus_proxy_call(g_pbap_proxy, "CreateSession",
                        temp,
                        G_DBUS_CALL_FLAGS_NONE, -1, NULL,
                        (GAsyncReadyCallback)__bt_pbap_connect_cb, ptr);
 
        BT_DBG("-");
-       return 0;
+       return BLUETOOTH_ERROR_NONE;
 }
 
 void __bt_pbap_disconnect_cb(GDBusProxy *proxy,
@@ -434,7 +463,7 @@ int _bt_pbap_disconnect(const bluetooth_device_address_t *address)
                        G_DBUS_CALL_FLAGS_NONE, -1, NULL,
                        (GAsyncReadyCallback)__bt_pbap_disconnect_cb, ptr);
 
-       return 0;
+       return BLUETOOTH_ERROR_NONE;
 }
 
 void __bt_pbap_select_cb(GDBusProxy *proxy,
@@ -1151,6 +1180,8 @@ int _bt_pbap_get_phonebook_size(const bluetooth_device_address_t *address,
        g_conn = _bt_gdbus_get_session_gconn();
        if (g_conn == NULL) {
                BT_ERR("Couldn't connect to session bus");
+               g_free(source_string);
+               g_free(type_string);
                return 0;
        }
        g_pbap_session_proxy =  g_dbus_proxy_new_sync(g_conn,
@@ -1238,6 +1269,8 @@ int _bt_pbap_get_phonebook(const bluetooth_device_address_t *address,
        g_conn = _bt_gdbus_get_session_gconn();
        if (g_conn == NULL) {
                BT_ERR("Couldn't connect to session bus");
+               g_free(source_string);
+               g_free(type_string);
                return 0;
        }
        g_pbap_session_proxy =  g_dbus_proxy_new_sync(g_conn,
@@ -1324,6 +1357,8 @@ int _bt_pbap_get_list(const bluetooth_device_address_t *address, int source,
        g_conn = _bt_gdbus_get_session_gconn();
        if (g_conn == NULL) {
                BT_ERR("Couldn't connect to session bus");
+               g_free(source_string);
+               g_free(type_string);
                return 0;
        }
        g_pbap_session_proxy =  g_dbus_proxy_new_sync(g_conn,
@@ -1411,6 +1446,8 @@ int _bt_pbap_pull_vcard(const bluetooth_device_address_t *address,
        g_conn = _bt_gdbus_get_session_gconn();
        if (g_conn == NULL) {
                BT_ERR("Couldn't connect to session bus");
+               g_free(source_string);
+               g_free(type_string);
                return 0;
        }
        g_pbap_session_proxy =  g_dbus_proxy_new_sync(g_conn,
@@ -1496,6 +1533,8 @@ int _bt_pbap_phonebook_search(const bluetooth_device_address_t *address,
        g_conn = _bt_gdbus_get_session_gconn();
        if (g_conn == NULL) {
                BT_ERR("Couldn't connect to session bus");
+               g_free(source_string);
+               g_free(type_string);
                return 0;
        }
        g_pbap_session_proxy =  g_dbus_proxy_new_sync(g_conn,