Ignore vconfkey value change for AVC mode on
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-pbap.c
index a088a0a..9132225 100644 (file)
@@ -31,7 +31,6 @@
 #include <glib.h>
 #include <gio/gio.h>
 
-#define         PBAP_UUID "0000112f-0000-1000-8000-00805f9b34fb"
 #define         PBAP_OBEX_CLIENT_SERVICE "org.bluez.obex"
 #define         PBAP_OBEX_CLIENT_PATH "/org/bluez/obex"
 #define         PBAP_OBEX_CLIENT_INTERFACE "org.bluez.obex.Client1"
@@ -110,8 +109,8 @@ char *SEARCH_FIELD[] = {
 
 static char *g_pbap_session_path = NULL;
 static char *g_pbap_server_address = NULL;
-static GDBusConnection *dbus_connection = NULL;
 static GDBusProxy *g_pbap_proxy = NULL;
+static gboolean g_is_pbap_connecting;
 
 static struct {
        int type;
@@ -255,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)
 {
@@ -271,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;
@@ -284,6 +305,8 @@ void __bt_pbap_connect_cb(GDBusProxy *proxy,
                BT_DBG("Session Path = %s\n", g_pbap_session_path);
                result = BLUETOOTH_ERROR_NONE;
                g_pbap_server_address = g_strdup(address_string);
+
+               g_variant_unref(value);
        }
 
        signal = g_variant_new("(is)", result, address_string);
@@ -294,11 +317,14 @@ 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)
 {
        BT_DBG("+");
+       GDBusConnection *g_conn;
        GError *error = NULL;
        char address_string[18] = { 0, };
        char *ptr = NULL;
@@ -311,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],
@@ -318,13 +347,12 @@ 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);
-       dbus_connection = _bt_get_session_gconn();
-       if (dbus_connection == NULL) {
-                       BT_ERR("Couldn't connect to system bus");
-                       return EXIT_FAILURE;
+       g_conn = _bt_gdbus_get_session_gconn();
+       if (g_conn == NULL) {
+                       BT_ERR("Couldn't connect to session bus");
+                       return BLUETOOTH_ERROR_INTERNAL;
        }
-
-       g_pbap_proxy =  g_dbus_proxy_new_sync(dbus_connection,
+       g_pbap_proxy =  g_dbus_proxy_new_sync(g_conn,
                        G_DBUS_PROXY_FLAGS_NONE, NULL,
                        PBAP_OBEX_CLIENT_SERVICE, PBAP_OBEX_CLIENT_PATH,
                        PBAP_OBEX_CLIENT_INTERFACE, NULL, &error);
@@ -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,
@@ -362,7 +391,6 @@ void __bt_pbap_disconnect_cb(GDBusProxy *proxy,
        char *address_string = user_data;
        GError *error = NULL;
        GVariant *value;
-       GVariant *signal = NULL;
        int result = BLUETOOTH_ERROR_INTERNAL ;
 
        BT_DBG("Address = %s", address_string);
@@ -390,12 +418,19 @@ void __bt_pbap_disconnect_cb(GDBusProxy *proxy,
                result = BLUETOOTH_ERROR_NONE;
                selected_path.folder = -1;
                selected_path.type = -1;
+
+               g_variant_unref(value);
        }
 
-       signal = g_variant_new("(is)", result, address_string);
-       _bt_send_event(BT_PBAP_CLIENT_EVENT,
-                       BLUETOOTH_PBAP_DISCONNECTED,
-                       signal);
+       /* If the result is success, the event reciever will send the disconnect event */
+       if (result != BLUETOOTH_ERROR_NONE) {
+               GVariant *signal = NULL;
+
+               signal = g_variant_new("(is)", result, address_string);
+               _bt_send_event(BT_PBAP_CLIENT_EVENT,
+                               BLUETOOTH_PBAP_DISCONNECTED,
+                               signal);
+       }
 
        g_free(address_string);
        BT_DBG("-");
@@ -428,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,
@@ -569,6 +604,8 @@ void __bt_pbap_get_phonebook_cb(GDBusProxy *proxy,
                transfer_info->filename = (char *)filename;
                transfer_info->operation = PULL_ALL;
                transfers = g_slist_append(transfers, transfer_info);
+
+               g_variant_unref(value);
        }
 
        g_object_unref(proxy);
@@ -1080,10 +1117,37 @@ int __bt_pbap_call_search_phonebook(GDBusProxy *proxy, bt_pbap_data_t *pbap_data
        return BLUETOOTH_ERROR_NONE;
 }
 
+int _bt_pbap_is_connected(bluetooth_device_address_t *device_address,
+                                       gboolean *connected)
+{
+       char address_string[18] = { 0, };
+
+       BT_CHECK_PARAMETER(device_address, return);
+       BT_CHECK_PARAMETER(connected, return);
+
+       /* In now, only 1 pbap connection is allowed */
+       if (g_pbap_server_address == NULL) {
+               *connected = FALSE;
+               return 0;
+       }
+
+       _bt_convert_addr_type_to_string(address_string,
+                                                       (unsigned char *)device_address->addr);
+       BT_DBG("Address String: %s", address_string);
+
+       if (g_strcmp0(address_string, g_pbap_server_address) == 0)
+               *connected = TRUE;
+       else
+               *connected = FALSE;
+
+       return 0;
+}
+
 int _bt_pbap_get_phonebook_size(const bluetooth_device_address_t *address,
                int source, int type)
 {
        BT_DBG("+");
+       GDBusConnection *g_conn;
        GDBusProxy *g_pbap_session_proxy = NULL;
        char address_string[18] = { 0, };
        char *source_string = NULL;
@@ -1112,7 +1176,15 @@ int _bt_pbap_get_phonebook_size(const bluetooth_device_address_t *address,
        BT_DBG("Address[%s] Source[%s] Type[%s]",
                        address_string, source_string, type_string);
        BT_DBG("Session Path = %s\n", g_pbap_session_path);
-       g_pbap_session_proxy =  g_dbus_proxy_new_sync(dbus_connection,
+
+       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,
                        G_DBUS_PROXY_FLAGS_NONE, NULL,
                        PBAP_SESSION_SERVICE, g_pbap_session_path,
                        PBAP_SESSION_INTERFACE, NULL, &err);
@@ -1160,6 +1232,7 @@ int _bt_pbap_get_phonebook(const bluetooth_device_address_t *address,
                int source, int type, bt_pbap_pull_parameters_t *app_param)
 {
        BT_DBG("+");
+       GDBusConnection *g_conn;
        GDBusProxy *g_pbap_session_proxy = NULL;
        char address_string[18] = { 0, };
        char *source_string = NULL;
@@ -1192,7 +1265,15 @@ int _bt_pbap_get_phonebook(const bluetooth_device_address_t *address,
                        address_string, source_string, type_string);
 
        BT_DBG("Session Path = %s\n", g_pbap_session_path);
-       g_pbap_session_proxy =  g_dbus_proxy_new_sync(dbus_connection,
+
+       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,
                        G_DBUS_PROXY_FLAGS_NONE, NULL,
                        PBAP_SESSION_SERVICE, g_pbap_session_path,
                        PBAP_SESSION_INTERFACE, NULL, &err);
@@ -1239,6 +1320,7 @@ int _bt_pbap_get_list(const bluetooth_device_address_t *address, int source,
                int type,  bt_pbap_list_parameters_t *app_param)
 {
        BT_DBG("+");
+       GDBusConnection *g_conn;
        GDBusProxy *g_pbap_session_proxy = NULL;
        char address_string[18] = { 0, };
        char *source_string = NULL;
@@ -1271,7 +1353,15 @@ int _bt_pbap_get_list(const bluetooth_device_address_t *address, int source,
                        address_string, source_string, type_string);
 
        BT_DBG("Session Path = %s\n", g_pbap_session_path);
-       g_pbap_session_proxy =  g_dbus_proxy_new_sync(dbus_connection,
+
+       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,
                        G_DBUS_PROXY_FLAGS_NONE, NULL,
                        PBAP_SESSION_SERVICE, g_pbap_session_path,
                        PBAP_SESSION_INTERFACE, NULL, &err);
@@ -1320,6 +1410,7 @@ int _bt_pbap_pull_vcard(const bluetooth_device_address_t *address,
                int source, int type, bt_pbap_pull_vcard_parameters_t *app_param)
 {
        BT_DBG("+");
+       GDBusConnection *g_conn;
        GDBusProxy *g_pbap_session_proxy = NULL;
        char address_string[18] = { 0, };
        char *source_string = NULL;
@@ -1351,7 +1442,15 @@ int _bt_pbap_pull_vcard(const bluetooth_device_address_t *address,
                        address_string, source_string, type_string);
 
        BT_DBG("Session Path = %s\n", g_pbap_session_path);
-       g_pbap_session_proxy =  g_dbus_proxy_new_sync(dbus_connection,
+
+       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,
                        G_DBUS_PROXY_FLAGS_NONE, NULL,
                        PBAP_SESSION_SERVICE, g_pbap_session_path,
                        PBAP_SESSION_INTERFACE, NULL, &err);
@@ -1398,6 +1497,7 @@ int _bt_pbap_phonebook_search(const bluetooth_device_address_t *address,
                int source, int type, bt_pbap_search_parameters_t *app_param)
 {
        BT_DBG("+");
+       GDBusConnection *g_conn;
        GDBusProxy *g_pbap_session_proxy = NULL;
        char address_string[18] = { 0, };
        char *source_string = NULL;
@@ -1430,7 +1530,14 @@ int _bt_pbap_phonebook_search(const bluetooth_device_address_t *address,
 
        BT_DBG("Session Path = %s\n", g_pbap_session_path);
 
-       g_pbap_session_proxy =  g_dbus_proxy_new_sync(dbus_connection,
+       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,
                        G_DBUS_PROXY_FLAGS_NONE, NULL,
                        PBAP_SESSION_SERVICE, g_pbap_session_path,
                        PBAP_SESSION_INTERFACE, NULL, &err);