Replace the deprecatd soup API
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-pbap.c
index fa9e27b..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],
@@ -319,10 +347,10 @@ int _bt_pbap_connect(const bluetooth_device_address_t *address)
 
        _bt_convert_addr_type_to_string(address_string, (unsigned char *)address->addr);
        BT_DBG("Address String: %s", address_string);
-       g_conn = _bt_gdbus_get_system_gconn();
+       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,
@@ -998,7 +1027,7 @@ int __bt_pbap_call_get_vcard(GDBusProxy *proxy, bt_pbap_data_t *pbap_data)
 //
 //****************************
 
-       snprintf(vcard, 20, "%d.vcf", app_param->index);
+       sprintf(vcard, "%d.vcf", app_param->index);
        BT_DBG("Handle: %s", vcard);
        vcard_handle = g_strdup(vcard);
        BT_DBG("vcard_handle: %s", vcard_handle);
@@ -1148,7 +1177,7 @@ int _bt_pbap_get_phonebook_size(const bluetooth_device_address_t *address,
                        address_string, source_string, type_string);
        BT_DBG("Session Path = %s\n", g_pbap_session_path);
 
-       g_conn = _bt_gdbus_get_system_gconn();
+       g_conn = _bt_gdbus_get_session_gconn();
        if (g_conn == NULL) {
                BT_ERR("Couldn't connect to session bus");
                g_free(source_string);
@@ -1237,7 +1266,7 @@ int _bt_pbap_get_phonebook(const bluetooth_device_address_t *address,
 
        BT_DBG("Session Path = %s\n", g_pbap_session_path);
 
-       g_conn = _bt_gdbus_get_system_gconn();
+       g_conn = _bt_gdbus_get_session_gconn();
        if (g_conn == NULL) {
                BT_ERR("Couldn't connect to session bus");
                g_free(source_string);
@@ -1325,7 +1354,7 @@ int _bt_pbap_get_list(const bluetooth_device_address_t *address, int source,
 
        BT_DBG("Session Path = %s\n", g_pbap_session_path);
 
-       g_conn = _bt_gdbus_get_system_gconn();
+       g_conn = _bt_gdbus_get_session_gconn();
        if (g_conn == NULL) {
                BT_ERR("Couldn't connect to session bus");
                g_free(source_string);
@@ -1414,7 +1443,7 @@ int _bt_pbap_pull_vcard(const bluetooth_device_address_t *address,
 
        BT_DBG("Session Path = %s\n", g_pbap_session_path);
 
-       g_conn = _bt_gdbus_get_system_gconn();
+       g_conn = _bt_gdbus_get_session_gconn();
        if (g_conn == NULL) {
                BT_ERR("Couldn't connect to session bus");
                g_free(source_string);
@@ -1501,7 +1530,7 @@ int _bt_pbap_phonebook_search(const bluetooth_device_address_t *address,
 
        BT_DBG("Session Path = %s\n", g_pbap_session_path);
 
-       g_conn = _bt_gdbus_get_system_gconn();
+       g_conn = _bt_gdbus_get_session_gconn();
        if (g_conn == NULL) {
                BT_ERR("Couldn't connect to session bus");
                g_free(source_string);