Code cleanup
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-pbap.c
index 46f5254..d69b66a 100644 (file)
@@ -20,9 +20,7 @@
 #include <string.h>
 #include <malloc.h>
 #include <stacktrim.h>
-#if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
 #include <syspopup_caller.h>
-#endif
 #include <vconf.h>
 #include <storage.h>
 
@@ -33,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"
@@ -45,7 +42,7 @@
 #define        PBAP_NUM_OF_FIELDS_ENTRY 29
 #define        PBAP_FIELD_ALL (0xFFFFFFFFFFFFFFFFULL)
 
-#define PBAP_DEFAULT_DOWNLAOD_PATH "/opt/usr/media/Downloads/"
+#define PBAP_DEFAULT_DOWNLAOD_PATH "/opt/usr/home/owner/media/Downloads"
 #define PBAP_DEFAULT_FILE_NAME "pb.vcf"
 
 char *FIELDS[] = {
@@ -112,7 +109,6 @@ 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 struct {
@@ -286,6 +282,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);
@@ -301,6 +299,7 @@ void __bt_pbap_connect_cb(GDBusProxy *proxy,
 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;
@@ -320,13 +319,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");
+       g_conn = _bt_gdbus_get_session_gconn();
+       if (g_conn == NULL) {
+                       BT_ERR("Couldn't connect to session bus");
                        return EXIT_FAILURE;
        }
-
-       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);
@@ -364,6 +362,7 @@ void __bt_pbap_disconnect_cb(GDBusProxy *proxy,
        char *address_string = user_data;
        GError *error = NULL;
        GVariant *value;
+       int result = BLUETOOTH_ERROR_INTERNAL ;
 
        BT_DBG("Address = %s", address_string);
 
@@ -387,11 +386,22 @@ void __bt_pbap_disconnect_cb(GDBusProxy *proxy,
                g_free(g_pbap_server_address);
                g_pbap_server_address = NULL;
 
+               result = BLUETOOTH_ERROR_NONE;
                selected_path.folder = -1;
                selected_path.type = -1;
+
+               g_variant_unref(value);
        }
 
-       /* PBAP disconnected event will be sent in event reciever */
+       /* 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("-");
@@ -565,6 +575,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);
@@ -630,7 +642,7 @@ void __bt_pbap_get_vcard_list_cb(GDBusProxy *proxy,
        }
 
        BT_DBG("Address = %s", address_string);
-       GVariant *temp = g_variant_new_strv((const gchar*)vcard_list, length);
+       GVariant *temp = g_variant_new_strv((const gchar * const *)vcard_list, length);
        signal = g_variant_new("(isv)", result, address_string, temp);
 
        _bt_send_event(BT_PBAP_CLIENT_EVENT,
@@ -747,7 +759,7 @@ void __bt_pbap_search_phonebook_cb(GDBusProxy *proxy,
        BT_DBG("Address = %s", address_string);
 
        signal = g_variant_new("(is@as)", result, address_string,
-                       g_variant_new_strv((const gchar *)vcard_list, length));
+                       g_variant_new_strv((const gchar * const *)vcard_list, length));
 
        _bt_send_event(BT_PBAP_CLIENT_EVENT,
                                BLUETOOTH_PBAP_PHONEBOOK_SEARCH,
@@ -872,6 +884,7 @@ int __bt_pbap_call_get_phonebook(GDBusProxy *proxy, bt_pbap_data_t *pbap_data)
 
        g_free(format_str);
        g_free(order_str);
+       g_free(target_file);
 
        BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;
@@ -940,7 +953,7 @@ int __bt_pbap_call_get_vcard(GDBusProxy *proxy, bt_pbap_data_t *pbap_data)
        char *target_file = NULL;
        char *download_path = NULL;
        char *vcard_handle = NULL;
-       char vcard[10] = { 0, };
+       char vcard[20] = { 0, };
        GVariantBuilder builder;
        GVariantBuilder inner_builder;
        GVariant *filters;
@@ -1020,6 +1033,7 @@ int __bt_pbap_call_get_vcard(GDBusProxy *proxy, bt_pbap_data_t *pbap_data)
 
        g_free(format_str);
        g_free(vcard_handle);
+       g_free(target_file);
 
        BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;
@@ -1074,10 +1088,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;
@@ -1106,7 +1147,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);
@@ -1154,6 +1203,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;
@@ -1186,7 +1236,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);
@@ -1210,6 +1268,8 @@ int _bt_pbap_get_phonebook(const bluetooth_device_address_t *address,
        pbap_data->app_param = param;
 
        if (source ==  selected_path.folder && type == selected_path.type) {
+               g_free(source_string);
+               g_free(type_string);
                return __bt_pbap_call_get_phonebook(g_pbap_session_proxy, pbap_data);
        }
 
@@ -1231,6 +1291,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;
@@ -1263,7 +1324,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);
@@ -1312,6 +1381,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;
@@ -1343,7 +1413,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);
@@ -1354,6 +1432,8 @@ int _bt_pbap_pull_vcard(const bluetooth_device_address_t *address,
                        ERR("Unable to create proxy: %s", err->message);
                        g_clear_error(&err);
                }
+               g_free(source_string);
+               g_free(type_string);
                return -1;
        }
 
@@ -1365,6 +1445,8 @@ int _bt_pbap_pull_vcard(const bluetooth_device_address_t *address,
        pbap_data->app_param = param;
 
        if (source ==  selected_path.folder && type == selected_path.type) {
+               g_free(source_string);
+               g_free(type_string);
                return __bt_pbap_call_get_vcard(g_pbap_session_proxy, pbap_data);
        }
 
@@ -1386,6 +1468,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;
@@ -1418,7 +1501,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);