When compare address, ignoring the case of the characters
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-telephony.c
old mode 100755 (executable)
new mode 100644 (file)
index 9f6bca4..8ae305d
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <dbus/dbus.h>
 #include <unistd.h>
 #include <vconf.h>
 #include <vconf-keys.h>
 
 #include "bt-common.h"
 #include "bluetooth-telephony-api.h"
-#include "marshal.h"
+#include "bt-internal-types.h"
 
 #define BT_SCO_TIMEOUT 3000
 
@@ -50,8 +49,8 @@ typedef struct {
 } bt_telephony_info_t;
 
 
-char *src_addr = NULL;
-
+static char *src_addr = NULL;
+static guint owner_id = 0;
 
 #define BLUETOOTH_TELEPHONY_ERROR (__bluetooth_telephony_error_quark())
 
@@ -68,7 +67,6 @@ char *src_addr = NULL;
 #define HFP_AGENT_PATH "/org/bluez/hfp_agent"
 #define HFP_AGENT_INTERFACE "Org.Hfp.App.Interface"
 
-#define TELEPHONY_APP_INTERFACE "org.tizen.csd.Call.Instance"
 #define CSD_CALL_APP_PATH "/org/tizen/csd/%d"
 #define HFP_NREC_STATUS_CHANGE "NrecStatusChanged"
 #define HFP_ANSWER_CALL "Answer"
@@ -93,8 +91,7 @@ static const gchar bt_telephony_introspection_xml[] =
 
 #define BT_TELEPHONY_CHECK_ENABLED() \
        do { \
-               if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED) \
-               { \
+               if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED) { \
                        BT_ERR("BT is not enabled"); \
                        return BLUETOOTH_TELEPHONY_ERROR_NOT_ENABLED; \
                } \
@@ -103,8 +100,7 @@ static const gchar bt_telephony_introspection_xml[] =
 static gboolean is_initialized = FALSE;
 #define BT_TELEPHONY_CHECK_INITIALIZED() \
        do { \
-               if (is_initialized == FALSE) \
-               { \
+               if (is_initialized == FALSE) { \
                        BT_ERR("Bluetooth telephony not initilized"); \
                        return BLUETOOTH_TELEPHONY_ERROR_NOT_INITIALIZED; \
                } \
@@ -145,8 +141,6 @@ static void __bluetooth_telephony_event_filter(GDBusConnection *connection,
                                                 GVariant *parameters,
                                                 gpointer user_data);
 
-static int __bluetooth_telephony_proxy_init(void);
-static void __bluetooth_telephony_proxy_deinit(void);
 static int __bluetooth_telephony_register(void);
 static int __bluetooth_telephony_unregister(void);
 
@@ -310,11 +304,11 @@ static GVariant *__bluetooth_telephony_dbus_method_send(const char *path,
                const char *interface, const char *method,
                GError **err, GVariant *parameters)
 {
-#ifdef TIZEN_WEARABLE
-       int timeout = 4000;
-#else
        int timeout = -1;
-#endif
+
+       if (TIZEN_PROFILE_WEARABLE || TIZEN_PROFILE_IVI)
+               timeout = 4000;
+
        GVariant *reply;
        GDBusProxy *proxy;
        GDBusConnection *conn;
@@ -324,7 +318,7 @@ static GVariant *__bluetooth_telephony_dbus_method_send(const char *path,
        conn = telephony_dbus_info.conn;
        retv_if(conn == NULL, NULL);
 
-       proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+       proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
                        NULL, HFP_AGENT_SERVICE, path, interface, NULL, err);
        if (proxy == NULL) {
                BT_ERR("Unable to allocate new proxy");
@@ -496,18 +490,7 @@ static void __bluetooth_telephony_event_filter(GDBusConnection *connection,
 {
        BT_DBG("+");
 
-       if (strcasecmp(interface_name, HFP_AGENT_SERVICE) == 0) {
-               if (strcasecmp(signal_name, HFP_NREC_STATUS_CHANGE) == 0)
-                       __bluetooth_handle_nrec_status_change(parameters);
-               else if (strcasecmp(signal_name, HFP_ANSWER_CALL) == 0)
-                       __bluetooth_telephony_answer_call(parameters);
-               else if (strcasecmp(signal_name, HFP_REJECT_CALL) == 0)
-                       __bluetooth_telephony_reject_call(parameters);
-               else if (strcasecmp(signal_name, HFP_RELEASE_CALL) == 0)
-                       __bluetooth_telephony_release_call(parameters);
-               else if (strcasecmp(signal_name, HFP_THREEWAY_CALL) == 0)
-                       __bluetooth_telephony_threeway_call(parameters);
-       } else if (strcasecmp(interface_name, BLUEZ_HEADSET_INTERFACE) == 0) {
+       if (strcasecmp(interface_name, BLUEZ_HEADSET_INTERFACE) == 0) {
                if (strcasecmp(signal_name, "PropertyChanged") == 0) {
                        GVariant *values;
                        gchar *property;
@@ -632,7 +615,16 @@ static void __bluetooth_telephony_event_filter(GDBusConnection *connection,
                        }
 
                        g_variant_unref(values);
-               }
+               } else if (strcasecmp(signal_name, HFP_NREC_STATUS_CHANGE) == 0)
+                       __bluetooth_handle_nrec_status_change(parameters);
+               else if (strcasecmp(signal_name, HFP_ANSWER_CALL) == 0)
+                       __bluetooth_telephony_answer_call(parameters);
+               else if (strcasecmp(signal_name, HFP_REJECT_CALL) == 0)
+                       __bluetooth_telephony_reject_call(parameters);
+               else if (strcasecmp(signal_name, HFP_RELEASE_CALL) == 0)
+                       __bluetooth_telephony_release_call(parameters);
+               else if (strcasecmp(signal_name, HFP_THREEWAY_CALL) == 0)
+                       __bluetooth_telephony_threeway_call(parameters);
        }
        BT_DBG("-");
 }
@@ -694,19 +686,19 @@ int __bluetooth_telephony_register_object(int reg, GDBusNodeInfo *node_info)
 static int __bluetooth_telephony_proxy_init(void)
 {
        FN_START;
-       guint owner_id;
        GDBusNodeInfo *node_info;
+       gchar *name;
 
-       owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
-                               TELEPHONY_APP_INTERFACE,
-                               G_BUS_NAME_OWNER_FLAGS_NONE,
-                               NULL, NULL, NULL,
-                               NULL, NULL);
-       BT_DBG("owner_id is [%d]", owner_id);
+       name = g_strdup_printf("org.tizen.csd.Call.Instance.p%d", getpid());
+       BT_DBG("well-known name: %s", name);
+
+       owner_id = g_bus_own_name_on_connection(telephony_dbus_info.conn,
+                               name, G_BUS_NAME_OWNER_FLAGS_NONE, NULL, NULL, NULL, NULL);
+       BT_DBG("owner_id: %d", owner_id);
+       g_free(name);
 
        node_info = __bt_telephony_create_method_node_info(
                                bt_telephony_introspection_xml);
-
        if (node_info == NULL) {
                BT_ERR("node_info NULL");
                return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
@@ -714,10 +706,12 @@ static int __bluetooth_telephony_proxy_init(void)
        if (__bluetooth_telephony_register_object(TRUE, node_info) !=
                        BLUETOOTH_TELEPHONY_ERROR_NONE) {
                BT_ERR("Registation of Method Failed");
+               g_dbus_node_info_unref(node_info);
                return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
        }
 
        FN_END;
+       g_dbus_node_info_unref(node_info);
        return BLUETOOTH_TELEPHONY_ERROR_NONE;
 }
 
@@ -726,9 +720,9 @@ static void __bluetooth_telephony_proxy_deinit(void)
        FN_START;
 
        __bluetooth_telephony_register_object(FALSE, NULL);
+       g_bus_unown_name(owner_id);
 
        FN_END;
-       return;
 }
 
 static int __bluetooth_telephony_register(void)
@@ -802,7 +796,6 @@ static  int __bluetooth_telephony_unregister(void)
        return BLUETOOTH_TELEPHONY_ERROR_NONE;
 }
 
-#ifndef TIZEN_WEARABLE
 static void __bluetooth_telephony_init_headset_state(void)
 {
        GVariant *reply;
@@ -842,7 +835,6 @@ static void __bluetooth_telephony_init_headset_state(void)
 
        FN_END;
 }
-#endif
 
 static gboolean __bluetooth_telephony_is_headset(uint32_t device_class)
 {
@@ -937,11 +929,11 @@ static int __bluetooth_telephony_get_connected_device(void)
        int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
 
        FN_START;
-       conn = _bt_gdbus_get_system_gconn();
+       conn = _bt_get_system_private_conn();
        retv_if(conn == NULL, BLUETOOTH_TELEPHONY_ERROR_INTERNAL);
 
        manager_proxy = g_dbus_proxy_new_sync(
-                       conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
+                       conn, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
                        BLUEZ_SERVICE_NAME, "/",
                        BLUEZ_MANAGER_INTERFACE, NULL, &error);
        if (manager_proxy == NULL) {
@@ -995,7 +987,7 @@ static int __bluetooth_telephony_get_connected_device(void)
                        g_variant_unref(path_values); /* path_values unused*/
 
                        proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
-                                       G_DBUS_PROXY_FLAGS_NONE, NULL,
+                                       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
                                        BLUEZ_SERVICE_NAME, object_path,
                                        BLUEZ_PROPERTIES_INTERFACE, NULL, &error);
                        if (proxy == NULL) {
@@ -1063,7 +1055,7 @@ static int __bluetooth_telephony_get_connected_device(void)
 
                        /* this is headset; Check for Connection */
                        headset_agent_proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
-                                       G_DBUS_PROXY_FLAGS_NONE, NULL,
+                                       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
                                        HFP_AGENT_SERVICE, object_path,
                                        HFP_AGENT_INTERFACE, NULL, &error);
                        if (headset_agent_proxy == NULL) {
@@ -1164,7 +1156,7 @@ static GDBusProxy *__bluetooth_telephony_get_connected_device_proxy(void)
                return NULL;
 
        proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
-                       G_DBUS_PROXY_FLAGS_NONE, NULL,
+                       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
                        HFP_AGENT_SERVICE, HFP_AGENT_PATH,
                        HFP_AGENT_INTERFACE, NULL, &error);
        if (proxy == NULL) {
@@ -1188,9 +1180,10 @@ int __bt_telephony_subscribe_adapter_signal(GDBusConnection *conn,
        if (conn == NULL)
                return -1;
 
-       static int subscribe_adapter_id = -1;
+       static guint subscribe_adapter_id = 0;
+
        if (subscribe == TRUE) {
-               if (subscribe_adapter_id == -1) {
+               if (subscribe_adapter_id == 0) {
                        subscribe_adapter_id = g_dbus_connection_signal_subscribe(conn,
                                        NULL, "org.freedesktop.DBus.ObjectManager",
                                        "InterfacesAdded", NULL, NULL, 0,
@@ -1199,10 +1192,10 @@ int __bt_telephony_subscribe_adapter_signal(GDBusConnection *conn,
                }
                return BLUETOOTH_TELEPHONY_ERROR_NONE;
        } else {
-               if (subscribe_adapter_id != -1) {
+               if (subscribe_adapter_id > 0) {
                        g_dbus_connection_signal_unsubscribe(conn,
                                        subscribe_adapter_id);
-                       subscribe_adapter_id = -1;
+                       subscribe_adapter_id = 0;
                }
                return BLUETOOTH_TELEPHONY_ERROR_NONE;
        }
@@ -1214,88 +1207,22 @@ int __bt_telephony_event_subscribe_signal(GDBusConnection *conn,
        if (conn == NULL)
                return -1;
 
-       static int subscribe_event1_id = -1;
-       static int subscribe_event2_id = -1;
-       static int subscribe_event3_id = -1;
-       static int subscribe_event4_id = -1;
-       static int subscribe_event5_id = -1;
-       static int subscribe_event6_id = -1;
+       static guint event_id = 0;
+
        if (subscribe == TRUE) {
-               if (subscribe_event1_id == -1) {
-                       subscribe_event1_id = g_dbus_connection_signal_subscribe(conn,
+               if (event_id == 0) {
+                       event_id = g_dbus_connection_signal_subscribe(conn,
                                        NULL, BLUEZ_HEADSET_INTERFACE,
-                                       "PropertyChanged", NULL, NULL, 0,
+                                       NULL, NULL, NULL, 0,
                                        __bluetooth_telephony_event_filter,
                                        NULL, NULL);
                }
-               if (subscribe_event2_id == -1) {
-                       subscribe_event2_id = g_dbus_connection_signal_subscribe(conn,
-                                       NULL, HFP_AGENT_SERVICE,
-                                       HFP_NREC_STATUS_CHANGE, NULL, NULL, 0,
-                                       __bluetooth_telephony_event_filter,
-                                       NULL, NULL);
-               }
-
-               if (subscribe_event3_id == -1) {
-                       subscribe_event3_id = g_dbus_connection_signal_subscribe(conn,
-                                       NULL, HFP_AGENT_SERVICE,
-                                       HFP_ANSWER_CALL, NULL, NULL, 0,
-                                       __bluetooth_telephony_event_filter,
-                                       NULL, NULL);
-               }
-               if (subscribe_event4_id == -1) {
-                       subscribe_event4_id = g_dbus_connection_signal_subscribe(conn,
-                                       NULL, HFP_AGENT_SERVICE,
-                                       HFP_REJECT_CALL, NULL, NULL, 0,
-                                       __bluetooth_telephony_event_filter,
-                                       NULL, NULL);
-               }
-               if (subscribe_event5_id == -1) {
-                       subscribe_event5_id = g_dbus_connection_signal_subscribe(conn,
-                                       NULL, HFP_AGENT_SERVICE,
-                                       HFP_RELEASE_CALL, NULL, NULL, 0,
-                                       __bluetooth_telephony_event_filter,
-                                       NULL, NULL);
-               }
-               if (subscribe_event6_id == -1) {
-                       subscribe_event6_id = g_dbus_connection_signal_subscribe(conn,
-                                       NULL, HFP_AGENT_SERVICE,
-                                       HFP_THREEWAY_CALL, NULL, NULL, 0,
-                                       __bluetooth_telephony_event_filter,
-                                       NULL, NULL);
-               }
-
                return BLUETOOTH_TELEPHONY_ERROR_NONE;
        } else {
-               if (subscribe_event1_id != -1) {
-                       g_dbus_connection_signal_unsubscribe(conn,
-                                       subscribe_event1_id);
-                       subscribe_event1_id = -1;
-               }
-               if (subscribe_event2_id != -1) {
-                       g_dbus_connection_signal_unsubscribe(conn,
-                                       subscribe_event2_id);
-                       subscribe_event2_id = -1;
-               }
-               if (subscribe_event3_id != -1) {
-                       g_dbus_connection_signal_unsubscribe(conn,
-                                       subscribe_event3_id);
-                       subscribe_event3_id = -1;
-               }
-               if (subscribe_event4_id != -1) {
+               if (event_id > 0) {
                        g_dbus_connection_signal_unsubscribe(conn,
-                                       subscribe_event4_id);
-                       subscribe_event4_id = -1;
-               }
-               if (subscribe_event5_id != -1) {
-                       g_dbus_connection_signal_unsubscribe(conn,
-                                       subscribe_event5_id);
-                       subscribe_event5_id = -1;
-               }
-               if (subscribe_event6_id != -1) {
-                       g_dbus_connection_signal_unsubscribe(conn,
-                                       subscribe_event6_id);
-                       subscribe_event6_id = -1;
+                                       event_id);
+                       event_id = 0;
                }
                return BLUETOOTH_TELEPHONY_ERROR_NONE;
        }
@@ -1313,12 +1240,15 @@ BT_EXPORT_API int bluetooth_telephony_init(bt_telephony_func_ptr cb,
 
        if (is_initialized == TRUE) {
                BT_ERR("Bluetooth telephony already initilized");
-               return BLUETOOTH_TELEPHONY_ERROR_ALREADY_INITIALIZED;
+               return BLUETOOTH_TELEPHONY_ERROR_NONE;
        }
 
        is_initialized = TRUE;
 
-       telephony_dbus_info.conn = _bt_gdbus_init_system_gconn();
+       /* As a result of discussion with system team, private connection is not
+        * suitable in here. It is better to use shared connection. */
+       //telephony_dbus_info.conn = _bt_gdbus_init_system_gconn();
+       telephony_dbus_info.conn = _bt_get_system_shared_conn();
        if (!telephony_dbus_info.conn) {
                is_initialized = FALSE;
                BT_ERR("Could not get DBus Connection");
@@ -1328,7 +1258,7 @@ BT_EXPORT_API int bluetooth_telephony_init(bt_telephony_func_ptr cb,
        /* Call Path */
        snprintf(telephony_info.call_path, sizeof(telephony_info.call_path),
                                        CSD_CALL_APP_PATH, getpid());
-       BT_DBG("Call Path = %s", telephony_info.call_path);
+       BT_INFO("Call Path = %s", telephony_info.call_path);
        memset(telephony_info.address, 0x00, sizeof(telephony_info.address));
 
        if (__bluetooth_telephony_proxy_init()) {
@@ -1340,7 +1270,7 @@ BT_EXPORT_API int bluetooth_telephony_init(bt_telephony_func_ptr cb,
 
        telephony_dbus_info.manager_proxy = g_dbus_proxy_new_sync(
                        telephony_dbus_info.conn,
-                       G_DBUS_PROXY_FLAGS_NONE, NULL,
+                       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
                        BLUEZ_SERVICE_NAME, "/",
                        BLUEZ_MANAGER_INTERFACE, NULL, &error);
        if (telephony_dbus_info.manager_proxy == NULL) {
@@ -1360,9 +1290,9 @@ BT_EXPORT_API int bluetooth_telephony_init(bt_telephony_func_ptr cb,
 
        telephony_dbus_info.dbus_proxy = g_dbus_proxy_new_sync(
                        telephony_dbus_info.conn,
-                       G_DBUS_PROXY_FLAGS_NONE, NULL,
-                       DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
-                       DBUS_INTERFACE_DBUS, NULL, &error);
+                       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
+                       BT_EVENT_FREEDESKTOP, BT_FREEDESKTOP_PATH,
+                       BT_EVENT_FREEDESKTOP, NULL, &error);
        if (NULL == telephony_dbus_info.dbus_proxy) {
                __bluetooth_telephony_proxy_deinit();
                telephony_dbus_info.conn = NULL;
@@ -1408,9 +1338,8 @@ BT_EXPORT_API int bluetooth_telephony_init(bt_telephony_func_ptr cb,
                goto fail;
        }
 
-#ifndef TIZEN_WEARABLE
-       __bluetooth_telephony_init_headset_state();
-#endif
+       if (TIZEN_PROFILE_WEARABLE || TIZEN_PROFILE_IVI)
+               __bluetooth_telephony_init_headset_state();
 
        FN_END;
        return ret;
@@ -1452,9 +1381,8 @@ BT_EXPORT_API int bluetooth_telephony_deinit(void)
                telephony_dbus_info.manager_proxy = NULL;
        }
 
-       if (telephony_dbus_info.conn != NULL) {
+       if (telephony_dbus_info.conn != NULL)
                telephony_dbus_info.conn = NULL;
-       }
 
        if (telephony_dbus_info.dbus_proxy != NULL) {
                g_object_unref(telephony_dbus_info.dbus_proxy);
@@ -1491,10 +1419,9 @@ BT_EXPORT_API gboolean bluetooth_telephony_is_sco_connected(void)
        g_variant_get(reply, "(b)", &status);
        g_variant_unref(reply);
 
-#ifdef TIZEN_WEARABLE
-       if (status == TRUE && telephony_info.headset_state != BLUETOOTH_STATE_PLAYING)
-               telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
-#endif
+       if (TIZEN_PROFILE_WEARABLE || TIZEN_PROFILE_IVI)
+               if (status == TRUE && telephony_info.headset_state != BLUETOOTH_STATE_PLAYING)
+                       telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
 
        BT_INFO("SCO Connected Status = [%d]", status);
        return status;
@@ -1617,6 +1544,7 @@ BT_EXPORT_API int bluetooth_telephony_send_vendor_cmd(const char *cmd)
 {
        GError *error = NULL;
        GVariant *reply, *parameters;
+       int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
 
        FN_START;
 
@@ -1639,8 +1567,15 @@ BT_EXPORT_API int bluetooth_telephony_send_vendor_cmd(const char *cmd)
 
        g_variant_unref(reply);
 
+       if (error) {
+               g_dbus_error_strip_remote_error(error);
+               ret = __bt_telephony_get_error(error->message);
+               BT_ERR("Error here %d\n", ret);
+               g_error_free(error);
+       }
+
        FN_END;
-       return BLUETOOTH_TELEPHONY_ERROR_NONE;
+       return ret;
 }
 
 BT_EXPORT_API int bluetooth_telephony_start_voice_recognition(void)
@@ -1716,7 +1651,7 @@ static void __bluetooth_telephony_sco_start_cb(GDBusProxy *proxy,
                GAsyncResult *res, gpointer user_data)
 {
        GError *error = NULL;
-       GVariant *value;
+       GVariant *value = NULL;
 
        value = g_dbus_proxy_call_finish(proxy, res, &error);
        if (value == NULL) {
@@ -1727,6 +1662,8 @@ static void __bluetooth_telephony_sco_start_cb(GDBusProxy *proxy,
                } else {
                        BT_ERR("SCo Start Failed");
                }
+               g_object_unref(proxy);
+               return;
        }
 
        BT_DBG("sco_start_cb : -");
@@ -1753,7 +1690,7 @@ BT_EXPORT_API int bluetooth_telephony_audio_open(void)
                return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
        }
 
-       conn = _bt_gdbus_get_system_gconn();
+       conn = _bt_get_system_private_conn();
        if (!conn) {
                BT_DBG("No System Bus found\n");
                return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
@@ -1762,7 +1699,7 @@ BT_EXPORT_API int bluetooth_telephony_audio_open(void)
        if (telephony_info.headset_state == BLUETOOTH_STATE_PLAYING)
                return BLUETOOTH_TELEPHONY_ERROR_ALREADY_CONNECTED;
 
-       proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
+       proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
                        HFP_AGENT_SERVICE, HFP_AGENT_PATH,
                        HFP_AGENT_INTERFACE, NULL, &err);
        if (proxy == NULL) {
@@ -1788,7 +1725,7 @@ static void __bluetooth_telephony_sco_close_cb(GDBusProxy *proxy,
                GAsyncResult *res, gpointer user_data)
 {
        GError *error = NULL;
-       GVariant *value;
+       GVariant *value = NULL;
 
        value = g_dbus_proxy_call_finish(proxy, res, &error);
        if (value == NULL) {
@@ -1799,6 +1736,8 @@ static void __bluetooth_telephony_sco_close_cb(GDBusProxy *proxy,
                } else {
                        BT_ERR("SCo close Failed");
                }
+               g_object_unref(proxy);
+               return;
        }
 
        BT_DBG("sco_close_cb : -");
@@ -1824,7 +1763,7 @@ BT_EXPORT_API int bluetooth_telephony_audio_close(void)
                return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
        }
 
-       conn = _bt_gdbus_get_system_gconn();
+       conn = _bt_get_system_private_conn();
        if (!conn) {
                BT_DBG("No System Bus found\n");
                return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
@@ -1833,7 +1772,7 @@ BT_EXPORT_API int bluetooth_telephony_audio_close(void)
        if (telephony_info.headset_state != BLUETOOTH_STATE_PLAYING)
                return BLUETOOTH_TELEPHONY_ERROR_NOT_CONNECTED;
 
-       proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
+       proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
                        HFP_AGENT_SERVICE, HFP_AGENT_PATH,
                        HFP_AGENT_INTERFACE, NULL, &err);
        if (proxy == NULL) {
@@ -1870,12 +1809,12 @@ BT_EXPORT_API int bluetooth_telephony_call_remote_ringing(unsigned int call_id)
        /*Make sure SCO is already connected */
        ret = __bluetooth_telephony_send_call_status(
                                CSD_CALL_STATUS_MO_ALERTING, call_id, NULL);
-       if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
+
+       if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
                BT_ERR("send call status Failed = [%d]", ret);
-               return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
-       }
+
        FN_END;
-       return BLUETOOTH_TELEPHONY_ERROR_NONE;
+       return ret;
 }
 
 BT_EXPORT_API int bluetooth_telephony_call_answered(unsigned int call_id,
@@ -1899,10 +1838,8 @@ BT_EXPORT_API int bluetooth_telephony_call_answered(unsigned int call_id,
        if (bt_audio) {
                if (!bluetooth_telephony_is_sco_connected()) {
                        ret = bluetooth_telephony_audio_open();
-                       if (ret != 0) {
+                       if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
                                BT_ERR("Audio connection call Failed[%d]", ret);
-                               return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
-                       }
                }
        }
 
@@ -1933,10 +1870,8 @@ BT_EXPORT_API int bluetooth_telephony_call_end(unsigned int call_id)
 
        ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_MT_RELEASE,
                                                                call_id, NULL);
-       if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
+       if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
                BT_ERR("send call status Failed = [%d]", ret);
-               return ret;
-       }
 
        FN_END;
        return ret;
@@ -2086,7 +2021,7 @@ BT_EXPORT_API int bluetooth_telephony_indicate_outgoing_call(
        GVariant *param;
        GError *err = NULL;
        const char *path = telephony_info.call_path;
-       int ret;
+       int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
 
        FN_START;
 
@@ -2120,15 +2055,13 @@ BT_EXPORT_API int bluetooth_telephony_indicate_outgoing_call(
        if (bt_audio) {
                if (!bluetooth_telephony_is_sco_connected()) {
                        ret = bluetooth_telephony_audio_open();
-                       if (ret != 0) {
+                       if (ret != 0)
                                BT_ERR(" Audio connection Failed = %d", ret);
-                               return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
-                       }
                }
        }
 
        FN_END;
-       return BLUETOOTH_TELEPHONY_ERROR_NONE;
+       return ret;
 }
 
 BT_EXPORT_API int bluetooth_telephony_indicate_incoming_call(
@@ -2277,6 +2210,42 @@ BT_EXPORT_API int bluetooth_telephony_is_connected(gboolean *ag_connected)
        return BLUETOOTH_ERROR_NONE;
 }
 
+BT_EXPORT_API int bluetooth_telephony_set_active_headset(const char *remote_addr)
+{
+#ifdef TIZEN_BT_DUAL_HEADSET_CONNECT
+       GVariant *reply;
+       GVariant *param;
+       GError *err = NULL;
+       int ret;
+
+       BT_CHECK_ENABLED(return);
+
+       if (NULL == remote_addr)
+               return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
+
+       param = g_variant_new("(s)", remote_addr);
+       reply = __bluetooth_telephony_dbus_method_send(
+                       HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
+                       "SwapHeadset", &err, param);
+
+       if (!reply) {
+               BT_ERR("Error returned in method call\n");
+               if (err) {
+                       g_dbus_error_strip_remote_error(err);
+                       ret = __bt_telephony_get_error(err->message);
+                       g_error_free(err);
+                       return ret;
+               }
+               return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
+       }
+
+       g_variant_unref(reply);
+       return BLUETOOTH_TELEPHONY_ERROR_NONE;
+#else
+       return BLUETOOTH_ERROR_NOT_SUPPORT;
+#endif
+}
+
 static void __bt_telephony_adapter_filter(GDBusConnection *connection,
                                        const gchar *sender_name,
                                        const gchar *object_path,
@@ -2300,7 +2269,7 @@ static void __bt_telephony_adapter_filter(GDBusConnection *connection,
                        return;
                }
 
-               BT_INFO("Adapter Path = [%s]", path);
+               BT_DBG("Adapter Path = [%s]", path);
                if (strcasecmp(path, DEFAULT_ADAPTER_OBJECT_PATH) == 0) {
                        if (__bt_telephony_get_src_addr(optional_param))
                                BT_ERR("Fail to get the local adapter address");
@@ -2309,6 +2278,8 @@ static void __bt_telephony_adapter_filter(GDBusConnection *connection,
                        if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
                                BT_ERR("__bluetooth_telephony_register failed");
                }
+
+               g_variant_unref(optional_param);
        }
 
        FN_END;