Fix the coding style errors (bt-service)
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-audio.c
old mode 100644 (file)
new mode 100755 (executable)
index a85d4ed..9d4eca6
@@ -1,11 +1,5 @@
 /*
- * Bluetooth-frwk
- *
- * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
- *              Girishashok Joshi <girish.joshi@samsung.com>
- *              Chanyeol Park <chanyeol.park@samsung.com>
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,8 +15,7 @@
  *
  */
 
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus.h>
+#include <gio/gio.h>
 #include <glib.h>
 #include <dlog.h>
 #include <string.h>
@@ -67,18 +60,17 @@ static void __bt_free_wait_data();
 static gboolean __bt_device_support_uuid(char *remote_address,
                                bt_audio_type_t type);
 
-static void __bt_hf_request_cb(DBusGProxy *proxy, DBusGProxyCall *call,
+static void __bt_hf_request_cb(GDBusProxy *proxy, GAsyncResult *res,
                                    gpointer user_data)
 {
        GError *g_error = NULL;
-       GArray *out_param1 = NULL;
-       GArray *out_param2 = NULL;
+       GVariant *out_param1 = NULL;
+       GVariant *reply = NULL;
        int result = BLUETOOTH_ERROR_NONE;
        bt_function_data_t *func_data;
        request_info_t *req_info;
 
-       dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
-
+       reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
        g_object_unref(proxy);
 
        func_data = user_data;
@@ -95,34 +87,29 @@ static void __bt_hf_request_cb(DBusGProxy *proxy, DBusGProxyCall *call,
                goto done;
        }
 
-       if (g_error == NULL)
-               goto dbus_return;
-
-       BT_ERR("HFG request Dbus Call Error: %s\n", g_error->message);
-
-       result = BLUETOOTH_ERROR_INTERNAL;
+       if (reply == NULL) {
+               BT_ERR("HF Connect Dbus Call Error");
+               if (g_error) {
+                       BT_ERR("Error: %s\n", g_error->message);
+                       g_clear_error(&g_error);
+               }
+               result = BLUETOOTH_ERROR_INTERNAL;
+       } else {
+               g_variant_unref(reply);
+       }
 
-dbus_return:
        if (req_info->context == NULL)
                goto done;
 
-       out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
-       out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
-
-       g_array_append_vals(out_param1, func_data->address,
-                               BT_ADDRESS_STR_LEN);
-       g_array_append_vals(out_param2, &result, sizeof(int));
-
-       dbus_g_method_return(req_info->context, out_param1, out_param2);
+       out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
+               func_data->address, BT_ADDRESS_STR_LEN, TRUE, NULL, NULL);
 
-       g_array_free(out_param1, TRUE);
-       g_array_free(out_param2, TRUE);
+       g_dbus_method_invocation_return_value(req_info->context,
+                       g_variant_new("(iv)", result, out_param1));
 
        _bt_delete_request_list(req_info->req_id);
-done:
-       if (g_error)
-               g_error_free(g_error);
 
+done:
        if (func_data) {
                g_free(func_data->address);
                g_free(func_data);
@@ -141,7 +128,6 @@ void _bt_audio_check_pending_connect()
 
                _bt_convert_addr_string_to_type(device_address.addr,
                                pdata->address);
-
                _bt_audio_connect(pdata->req_id,
                                BT_AUDIO_A2DP,
                                &device_address,
@@ -156,21 +142,19 @@ void _bt_audio_check_pending_connect()
        return;
 }
 
-static void __bt_audio_request_cb(DBusGProxy *proxy, DBusGProxyCall *call,
+static void __bt_audio_request_cb(GDBusProxy *proxy, GAsyncResult *res,
                                    gpointer user_data)
 {
        GError *g_error = NULL;
-       GArray *out_param1 = NULL;
-       GArray *out_param2 = NULL;
+       GVariant *out_param1 = NULL;
+       GVariant *reply = NULL;
        int result = BLUETOOTH_ERROR_NONE;
-
        bt_audio_function_data_t *func_data;
-
        request_info_t *req_info;
 
-       dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID);
-
+       reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
        g_object_unref(proxy);
+       g_variant_unref(reply);
 
        func_data = user_data;
 
@@ -250,22 +234,15 @@ dbus_return:
                goto done;
        }
 
-       out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
-       out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
-
-       g_array_append_vals(out_param1, func_data->address,
-                       BT_ADDRESS_STR_LEN);
-       g_array_append_vals(out_param2, &result, sizeof(int));
-
-       dbus_g_method_return(req_info->context, out_param1, out_param2);
+       out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
+               func_data->address, BT_ADDRESS_STR_LEN, TRUE, NULL, NULL);
 
-       g_array_free(out_param1, TRUE);
-       g_array_free(out_param2, TRUE);
+       g_dbus_method_invocation_return_value(req_info->context,
+                       g_variant_new("(iv)", result, out_param1));
 
        _bt_delete_request_list(req_info->req_id);
 done:
-       if (g_error)
-               g_error_free(g_error);
+       g_clear_error(&g_error);
 
        if (func_data) {
                g_free(func_data->address);
@@ -273,78 +250,6 @@ done:
        }
 }
 
-static char *__bt_get_audio_path(bluetooth_device_address_t *address)
-{
-
-       char *object_path = NULL;
-       char addr_str[BT_ADDRESS_STRING_SIZE + 1] = { 0 };
-       DBusGProxy *audio_proxy;
-       DBusGProxy *adapter_proxy;
-       DBusGConnection *g_conn;
-
-       retv_if(address == NULL, NULL);
-
-       g_conn = _bt_get_system_gconn();
-       retv_if(g_conn == NULL, NULL);
-
-       adapter_proxy = _bt_get_adapter_proxy();
-       retv_if(adapter_proxy == NULL, NULL);
-
-       _bt_convert_addr_type_to_string(addr_str, address->addr);
-
-       object_path = _bt_get_device_object_path(addr_str);
-
-       retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_FOUND);
-
-       audio_proxy = dbus_g_proxy_new_for_name(g_conn,
-                                       BT_BLUEZ_NAME,
-                                       object_path,
-                                       BT_HFP_AGENT_INTERFACE);
-
-       retv_if(audio_proxy == NULL, NULL);
-
-       g_object_unref(audio_proxy);
-
-       return object_path;
-}
-
-static char *__bt_get_connected_audio_path(void)
-{
-       int i;
-       guint size;
-       char *audio_path = NULL;
-       GArray *device_list;
-       bluetooth_device_info_t info;
-
-       /* allocate the g_pointer_array */
-       device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
-
-       if (_bt_get_bonded_devices(&device_list)
-                       != BLUETOOTH_ERROR_NONE) {
-               g_array_free(device_list, TRUE);
-               return NULL;
-       }
-
-       size = device_list->len;
-       size = (device_list->len) / sizeof(bluetooth_device_info_t);
-
-       for (i = 0; i < size; i++) {
-
-               info = g_array_index(device_list,
-                               bluetooth_device_info_t, i);
-
-               if (info.connected == TRUE) {
-                       audio_path = __bt_get_audio_path(&info.device_address);
-                       if (audio_path)
-                               break;
-               }
-       }
-
-       g_array_free(device_list, TRUE);
-
-       return audio_path;
-}
-
 static void __bt_free_wait_data()
 {
        if (g_wait_data != NULL) {
@@ -357,8 +262,7 @@ static void __bt_free_wait_data()
 static void __bt_remove_device_from_wait_list()
 {
        /* Before deleting the request update the UI */
-       GArray *out_param_1 = NULL;
-       GArray *out_param_2 = NULL;
+       GVariant *out_param_1 = NULL;
        int result = BLUETOOTH_ERROR_INTERNAL;
        request_info_t *req_info;
 
@@ -368,15 +272,12 @@ static void __bt_remove_device_from_wait_list()
                return;
        }
 
-       out_param_1 = g_array_new(FALSE, FALSE, sizeof(gchar));
-       out_param_2 = g_array_new(FALSE, FALSE, sizeof(gchar));
-       g_array_append_vals(out_param_1, g_wait_data->address,
-                       BT_ADDRESS_STR_LEN);
-       g_array_append_vals(out_param_2, &result, sizeof(int));
-       dbus_g_method_return(req_info->context,
-                       out_param_1, out_param_2);
-       g_array_free(out_param_1, TRUE);
-       g_array_free(out_param_2, TRUE);
+       out_param_1 = g_variant_new_from_data((const GVariantType *)"ay",
+               g_wait_data->address, BT_ADDRESS_STR_LEN, TRUE, NULL, NULL);
+
+       g_dbus_method_invocation_return_value(req_info->context,
+                       g_variant_new("(iv)", result, out_param_1));
+
        _bt_delete_request_list(g_wait_data->req_id);
 }
 
@@ -556,8 +457,14 @@ void _bt_add_headset_to_list(int type, int status, const char *address)
        }
 
        connected_device = g_malloc0(sizeof(bt_connected_headset_data_t));
+       /* Fix : NULL_RETURNS */
+       if (connected_device == NULL) {
+               BT_ERR("No memory allocated");
+               return;
+       }
+
        connected_device->device_state = status;
-       if (status == BT_STATE_CONNECTED)
+       if ((status == BT_STATE_CONNECTED) || (status == BT_STATE_CONNECTING))
                connected_device->type |= type;
        g_strlcpy(connected_device->device_address, address,
                        sizeof(connected_device->device_address));
@@ -637,19 +544,21 @@ static gboolean __bt_device_support_uuid(char *remote_address,
                                bt_audio_type_t type)
 {
        GArray *dev_list = NULL;
-       int size,i,j;
+       int size;
+       int i;
+       int j;
        bluetooth_device_info_t info;
        char bond_address[BT_ADDRESS_STRING_SIZE] = { 0 };
        gboolean ret = FALSE;
 
        BT_DBG("+");
 
-       dev_list = g_array_new (FALSE, FALSE, sizeof(gchar));
+       dev_list = g_array_new(FALSE, FALSE, sizeof(gchar));
 
        _bt_get_bonded_devices(&dev_list);
        size = (dev_list->len) / sizeof(bluetooth_device_info_t);
 
-       for (i=0; i < size; i++) {
+       for (i = 0; i < size; i++) {
                info = g_array_index(dev_list, bluetooth_device_info_t, i);
                _bt_convert_addr_type_to_string(bond_address,
                                info.device_address.addr);
@@ -707,8 +616,8 @@ int _bt_audio_connect(int request_id, int type,
 {
        int result = BLUETOOTH_ERROR_NONE;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
-       DBusGProxy *adapter_proxy;
-       DBusGConnection *g_conn;
+       GDBusProxy *adapter_proxy;
+       GDBusConnection *g_conn;
        int ret;
        char *uuid;
        int value = BLUETOOTH_ERROR_NONE;
@@ -725,6 +634,11 @@ int _bt_audio_connect(int request_id, int type,
        _bt_convert_addr_type_to_string(address, device_address->addr);
 
        func_data = g_malloc0(sizeof(bt_audio_function_data_t));
+       /* Fix : NULL_RETURNS */
+       if (func_data == NULL) {
+               result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
+               goto fail;
+       }
 
        func_data->address = g_strdup(address);
        func_data->req_id = request_id;
@@ -742,12 +656,17 @@ int _bt_audio_connect(int request_id, int type,
        case BT_AVRCP:
                uuid = AVRCP_TARGET_UUID;
                break;
+       case BT_AUDIO_A2DP_SOURCE:
+               uuid = A2DP_SOURCE_UUID;
+               break;
        case BT_AUDIO_ALL:
                if (__bt_device_support_uuid(address, BT_AUDIO_HSP)) {
                        uuid = HFP_HS_UUID;
                        func_data->pending = BT_PENDING_CONNECT;
+                       type = BT_AUDIO_HSP;
                } else if (__bt_device_support_uuid(address, BT_AUDIO_A2DP)) {
                        uuid = A2DP_SINK_UUID;
+                       type = BT_AUDIO_A2DP;
                } else {
                        BT_ERR("No audio role supported");
                        result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
@@ -797,8 +716,8 @@ int _bt_audio_disconnect(int request_id, int type,
        int result = BLUETOOTH_ERROR_NONE;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
        bt_audio_function_data_t *func_data;
-       DBusGProxy *adapter_proxy;
-       DBusGConnection *g_conn;
+       GDBusProxy *adapter_proxy;
+       GDBusConnection *g_conn;
        GList *node;
        int ret;
        char *uuid;
@@ -814,6 +733,7 @@ int _bt_audio_disconnect(int request_id, int type,
        _bt_convert_addr_type_to_string(address, device_address->addr);
 
        func_data = g_malloc0(sizeof(bt_audio_function_data_t));
+       retv_if(func_data == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        func_data->address = g_strdup(address);
        func_data->req_id = request_id;
@@ -831,6 +751,9 @@ int _bt_audio_disconnect(int request_id, int type,
        case BT_AVRCP:
                uuid = AVRCP_TARGET_UUID;
                break;
+       case BT_AUDIO_A2DP_SOURCE:
+               uuid = A2DP_SOURCE_UUID;
+               break;
        case BT_AUDIO_ALL:
                if (_bt_is_service_connected(address, BT_AUDIO_HSP)) {
                        uuid = HFP_HS_UUID;
@@ -911,13 +834,11 @@ int _bt_hf_connect(int request_id,
        int result = BLUETOOTH_ERROR_NONE;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
        bt_function_data_t *func_data;
-       DBusGProxy *adapter_proxy;
-       DBusGConnection *g_conn;
-
+       GDBusProxy *adapter_proxy;
+       GDBusConnection *g_conn;
        int ret;
        char *uuid;
 
-
        BT_CHECK_PARAMETER(device_address, return);
 
        _bt_convert_addr_type_to_string(address, device_address->addr);
@@ -935,6 +856,11 @@ int _bt_hf_connect(int request_id,
        }
 
        func_data = g_malloc0(sizeof(bt_function_data_t));
+       /* Fix : NULL_RETURNS */
+       if (func_data == NULL) {
+               result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
+               goto fail;
+       }
 
        func_data->address = g_strdup(address);
        func_data->req_id = request_id;
@@ -969,8 +895,8 @@ int _bt_hf_disconnect(int request_id,
        int result = BLUETOOTH_ERROR_NONE;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
        bt_function_data_t *func_data;
-       DBusGProxy *adapter_proxy;
-       DBusGConnection *g_conn;
+       GDBusProxy *adapter_proxy;
+       GDBusConnection *g_conn;
 
        int ret;
        char *uuid;
@@ -992,6 +918,11 @@ int _bt_hf_disconnect(int request_id,
        }
 
        func_data = g_malloc0(sizeof(bt_function_data_t));
+       /* Fix : NULL_RETURNS */
+       if (func_data == NULL) {
+               result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
+               goto fail;
+       }
 
        func_data->address = g_strdup(address);
        func_data->req_id = request_id;
@@ -1018,126 +949,33 @@ fail:
        return result;
 }
 
-int _bt_audio_get_speaker_gain(unsigned int *gain)
-{
-       char *device_path = NULL;
-       DBusGProxy *adapter_proxy;
-       DBusGProxy *profile_proxy;
-       DBusGConnection *g_conn;
-       GHashTable *hash = NULL;
-       GValue *value;
-
-       adapter_proxy = _bt_get_adapter_proxy();
-       retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       g_conn = _bt_get_system_gconn();
-       retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       device_path = __bt_get_connected_audio_path();
-       retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_CONNECTED);
-
-       profile_proxy = dbus_g_proxy_new_for_name(g_conn, BT_BLUEZ_NAME,
-                                     device_path, BT_HEADSET_INTERFACE);
-
-       g_free(device_path);
-
-       retv_if(profile_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       dbus_g_proxy_call(profile_proxy, "GetProperties", NULL,
-                       G_TYPE_INVALID,
-                       dbus_g_type_get_map("GHashTable",
-                       G_TYPE_STRING, G_TYPE_VALUE),
-                       &hash, G_TYPE_INVALID);
-
-       g_object_unref(profile_proxy);
-
-       retv_if(hash == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       value = g_hash_table_lookup(hash, "SpeakerGain");
-       *gain = value ? g_value_get_uint(value) : 0;
-       g_hash_table_destroy(hash);
-       return BLUETOOTH_ERROR_NONE;
-}
-
-int _bt_audio_set_speaker_gain(unsigned int gain)
-{
-       char *device_path = NULL;
-       char *gain_str = "SpeakerGain";
-       char sig[2] = {DBUS_TYPE_UINT16, '\0'};
-       int ret = BLUETOOTH_ERROR_NONE;
-       DBusMessage *msg;
-       DBusMessageIter iter;
-       DBusMessageIter value;
-       DBusConnection *conn;
-
-       conn = _bt_get_system_conn();
-       retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       device_path = __bt_get_connected_audio_path();
-       retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_CONNECTED);
-
-       msg = dbus_message_new_method_call(BT_BLUEZ_NAME,
-                       device_path, BT_HEADSET_INTERFACE,
-                       "SetProperty");
-
-       g_free(device_path);
-
-       retv_if(msg == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       dbus_message_iter_init_append(msg, &iter);
-       dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING,
-                       &gain_str);
-       dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
-                       sig, &value);
-       dbus_message_iter_append_basic(&value, DBUS_TYPE_UINT16,
-                       &gain);
-       dbus_message_iter_close_container(&iter, &value);
-
-       if (dbus_message_get_type(msg) == DBUS_MESSAGE_TYPE_METHOD_CALL)
-               dbus_message_set_no_reply(msg, TRUE);
-
-       if (!dbus_connection_send(conn, msg, NULL)) {
-               BT_ERR("Dbus sending failed\n");
-               ret = BLUETOOTH_ERROR_INTERNAL;
-       }
-       dbus_message_unref(msg);
-
-       return ret;
-}
-
 int _bt_audio_set_content_protect(gboolean status)
 {
-       DBusConnection *conn;
-       DBusMessage *signal;
+       GDBusConnection *conn;
+       GError *error = NULL;
 
        BT_DBG("+\n");
 
-       conn = _bt_get_system_conn();
+       conn = _bt_get_system_gconn();
        retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       BT_DBG("Content Protection status = [%d] \n", status);
-
-       /*Emit Content protection Status change signal with value*/
-       signal = dbus_message_new_signal(BT_CONTENT_PROTECTION_PATH,
-                                       BT_CONTENT_PROTECTION_INTERFACE,
-                                       "ProtectionRequired");
-       if (!signal)
-               goto err;
-
-       if (!dbus_message_append_args(signal,
-                               DBUS_TYPE_BOOLEAN, &status,
-                               DBUS_TYPE_INVALID)) {
-               BT_ERR("Signal appending failed\n");
-               dbus_message_unref(signal);
-               goto err;
-       }
+       BT_DBG("Content Protection status = [%d]", status);
 
-       dbus_connection_send(conn, signal, NULL);
-       dbus_message_unref(signal);
+       g_dbus_connection_emit_signal(conn,
+                       NULL, BT_CONTENT_PROTECTION_PATH,
+                       BT_CONTENT_PROTECTION_INTERFACE,
+                       "ProtectionRequired",
+                       g_variant_new("(b)", status),
+                       &error);
 
-       BT_DBG("-\n");
-       return BLUETOOTH_ERROR_NONE;
+       if (error) {
+               /* dBUS gives error cause */
+               ERR("Could not Emit Signal: errCode[%x], message[%s]",
+                       error->code, error->message);
+               g_clear_error(&error);
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
 
-err:
-       return BLUETOOTH_ERROR_INTERNAL;
+       BT_DBG("Emit Signal done = [ProtectionRequired]");
+       return BLUETOOTH_ERROR_NONE;
 }