Fix 64bit build error
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-telephony.c
index d13c6b7..9274cd9 100644 (file)
@@ -24,7 +24,6 @@
 
 #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,13 +67,13 @@ 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"
 #define HFP_REJECT_CALL "Reject"
 #define HFP_RELEASE_CALL "Release"
 #define HFP_THREEWAY_CALL "Threeway"
+#define HFP_HF_BATTERY_LEVEL_CHANGE "HfBatteryLevelChanged"
 
 #define DEFAULT_ADAPTER_OBJECT_PATH "/org/bluez/hci0"
 
@@ -143,8 +142,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);
 
@@ -322,7 +319,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");
@@ -484,6 +481,20 @@ static void __bluetooth_handle_nrec_status_change(GVariant *var)
 
 }
 
+static void __bluetooth_telephony_hf_battery_level_change(GVariant *var)
+{
+       telephony_event_hf_battery_level_t param;
+       char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
+
+       g_variant_get(var, "(&su)", &param.remote_address, &param.battery_level);
+
+       _bt_convert_addr_string_to_secure_string(secure_address, param.remote_address);
+       BT_INFO("address(%s) battery level(%u)", secure_address, param.battery_level);
+
+       __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_HF_BATTERY_LEVEL_CHANGED,
+               BLUETOOTH_TELEPHONY_ERROR_NONE, (void *)&param);
+}
+
 static void __bluetooth_telephony_event_filter(GDBusConnection *connection,
                const gchar *sender_name,
                const gchar *object_path,
@@ -494,18 +505,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;
@@ -630,7 +630,18 @@ 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);
+               else if (strcasecmp(signal_name, HFP_HF_BATTERY_LEVEL_CHANGE) == 0)
+                       __bluetooth_telephony_hf_battery_level_change(parameters);
        }
        BT_DBG("-");
 }
@@ -665,7 +676,6 @@ int __bluetooth_telephony_register_object(int reg, GDBusNodeInfo *node_info)
                        return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
 
                path = g_strdup(telephony_info.call_path);
-               BT_DBG("path is [%s]", path);
 
                bt_tel_id = g_dbus_connection_register_object(telephony_dbus_info.conn,
                                path, node_info->interfaces[0],
@@ -692,15 +702,15 @@ 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());
+
+       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("well-known name: %s, owner_id: %d", name, owner_id);
+       g_free(name);
 
        node_info = __bt_telephony_create_method_node_info(
                                bt_telephony_introspection_xml);
@@ -725,9 +735,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)
@@ -934,11 +944,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) {
@@ -992,7 +1002,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) {
@@ -1060,7 +1070,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) {
@@ -1161,7 +1171,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) {
@@ -1212,89 +1222,22 @@ int __bt_telephony_event_subscribe_signal(GDBusConnection *conn,
        if (conn == NULL)
                return -1;
 
-       static guint subscribe_event1_id = 0;
-       static guint subscribe_event2_id = 0;
-       static guint subscribe_event3_id = 0;
-       static guint subscribe_event4_id = 0;
-       static guint subscribe_event5_id = 0;
-       static guint subscribe_event6_id = 0;
+       static guint event_id = 0;
 
        if (subscribe == TRUE) {
-               if (subscribe_event1_id == 0) {
-                       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,
-                                       __bluetooth_telephony_event_filter,
-                                       NULL, NULL);
-               }
-               if (subscribe_event2_id == 0) {
-                       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 == 0) {
-                       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 == 0) {
-                       subscribe_event4_id = g_dbus_connection_signal_subscribe(conn,
-                                       NULL, HFP_AGENT_SERVICE,
-                                       HFP_REJECT_CALL, NULL, NULL, 0,
+                                       NULL, NULL, NULL, 0,
                                        __bluetooth_telephony_event_filter,
                                        NULL, NULL);
                }
-               if (subscribe_event5_id == 0) {
-                       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 == 0) {
-                       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 > 0) {
+               if (event_id > 0) {
                        g_dbus_connection_signal_unsubscribe(conn,
-                                       subscribe_event1_id);
-                       subscribe_event1_id = 0;
-               }
-               if (subscribe_event2_id > 0) {
-                       g_dbus_connection_signal_unsubscribe(conn,
-                                       subscribe_event2_id);
-                       subscribe_event2_id = 0;
-               }
-               if (subscribe_event3_id > 0) {
-                       g_dbus_connection_signal_unsubscribe(conn,
-                                       subscribe_event3_id);
-                       subscribe_event3_id = 0;
-               }
-               if (subscribe_event4_id > 0) {
-                       g_dbus_connection_signal_unsubscribe(conn,
-                                       subscribe_event4_id);
-                       subscribe_event4_id = 0;
-               }
-               if (subscribe_event5_id > 0) {
-                       g_dbus_connection_signal_unsubscribe(conn,
-                                       subscribe_event5_id);
-                       subscribe_event5_id = 0;
-               }
-               if (subscribe_event6_id > 0) {
-                       g_dbus_connection_signal_unsubscribe(conn,
-                                       subscribe_event6_id);
-                       subscribe_event6_id = 0;
+                                       event_id);
+                       event_id = 0;
                }
                return BLUETOOTH_TELEPHONY_ERROR_NONE;
        }
@@ -1317,7 +1260,10 @@ BT_EXPORT_API int bluetooth_telephony_init(bt_telephony_func_ptr cb,
 
        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");
@@ -1339,7 +1285,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) {
@@ -1359,7 +1305,7 @@ 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,
+                       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) {
@@ -1759,7 +1705,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;
@@ -1768,7 +1714,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) {
@@ -1832,7 +1778,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;
@@ -1841,7 +1787,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) {
@@ -2246,6 +2192,44 @@ BT_EXPORT_API int bluetooth_telephony_get_headset_volume(
        return BLUETOOTH_TELEPHONY_ERROR_NONE;
 }
 
+BT_EXPORT_API int bluetooth_telephony_get_battery_level(
+                       const char *remote_address, unsigned int *level)
+{
+       GVariant *reply;
+       GError *err = NULL;
+       GVariant *param = NULL;
+       int ret;
+
+       FN_START;
+
+       BT_TELEPHONY_CHECK_INITIALIZED();
+       BT_TELEPHONY_CHECK_ENABLED();
+
+       param = g_variant_new("(s)", remote_address);
+
+       reply = __bluetooth_telephony_dbus_method_send(
+                       HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
+                       "GetHfBatteryLevel", &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_get(reply, "(u)", level);
+
+       g_variant_unref(reply);
+
+       FN_END;
+       return BLUETOOTH_TELEPHONY_ERROR_NONE;
+}
+
 BT_EXPORT_API int bluetooth_telephony_is_connected(gboolean *ag_connected)
 {
        GVariant *reply;
@@ -2281,7 +2265,6 @@ BT_EXPORT_API int bluetooth_telephony_is_connected(gboolean *ag_connected)
 
 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;
@@ -2310,9 +2293,6 @@ BT_EXPORT_API int bluetooth_telephony_set_active_headset(const char *remote_addr
 
        g_variant_unref(reply);
        return BLUETOOTH_TELEPHONY_ERROR_NONE;
-#else
-       return BLUETOOTH_ERROR_NOT_SUPPORT;
-#endif
 }
 
 static void __bt_telephony_adapter_filter(GDBusConnection *connection,
@@ -2338,7 +2318,6 @@ static void __bt_telephony_adapter_filter(GDBusConnection *connection,
                        return;
                }
 
-               BT_INFO("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");
@@ -2347,6 +2326,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;