Apply tizen 3.0 based product patchsets
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-audio.c
index 0bbb8c9..1cadaab 100644 (file)
@@ -206,12 +206,6 @@ static void __bt_audio_request_cb(GDBusProxy *proxy, GAsyncResult *res,
        }
 
 check_req_info:
-       req_info = _bt_get_request_info(func_data->req_id);
-       if (req_info == NULL) {
-               BT_ERR("req_info == NULL");
-               goto done;
-       }
-
        if (g_error == NULL)
                goto dbus_return;
 
@@ -241,8 +235,12 @@ check_req_info:
 
        /* Event will be sent by the event reciever */
 dbus_return:
-       if (req_info->context == NULL) {
-               BT_DBG("req_info->context is NULL");
+       req_info = _bt_get_request_info(func_data->req_id);
+       if (req_info == NULL) {
+               BT_ERR("req_info == NULL");
+               goto done;
+       } else if (req_info->context == NULL) {
+               BT_ERR("req_info->context is NULL");
                goto done;
        }
 
@@ -445,8 +443,10 @@ static int __bt_is_headset_connected(int type, int req_id,
                node = g_list_next(node);
        }
 
-       if (!connected)
+       if (!connected) {
+               __bt_free_wait_data();
                return BLUETOOTH_ERROR_NOT_CONNECTED;
+       }
 
        BT_DBG("connected headset %s", connected_address);
 
@@ -815,17 +815,19 @@ int _bt_audio_connect(int request_id, int type,
                goto fail;
        }
        BT_INFO("Connecting to service %s", uuid);
+       /* First Check if device is in connecting state if it than return error */
+       value = __bt_is_headset_connecting(type);
+       if (value != BLUETOOTH_ERROR_NONE) {
+               result = BLUETOOTH_ERROR_IN_PROGRESS;
+               goto fail;
+       }
 
        value = __bt_is_headset_connected(type, request_id, address);
 
        if (value == BLUETOOTH_ERROR_ALREADY_CONNECT) {
-               return BLUETOOTH_ERROR_ALREADY_CONNECT;
+               result = BLUETOOTH_ERROR_ALREADY_CONNECT;
+               goto fail;
        } else if (value == BLUETOOTH_ERROR_NOT_CONNECTED) {
-               value = __bt_is_headset_connecting(type);
-               if (value != BLUETOOTH_ERROR_NONE) {
-                       result = BLUETOOTH_ERROR_IN_PROGRESS;
-                       goto fail;
-               }
                _bt_headset_set_local_connection(TRUE);
                ret = _bt_connect_profile(address, uuid,
                                __bt_audio_request_cb, func_data);
@@ -841,7 +843,8 @@ int _bt_audio_connect(int request_id, int type,
                /* Add data to the connected list */
                _bt_add_headset_to_list(type, BT_STATE_CONNECTING, address);
        } else if (value == BLUETOOTH_ERROR_IN_PROGRESS) {
-               return BLUETOOTH_ERROR_IN_PROGRESS;
+               result = BLUETOOTH_ERROR_IN_PROGRESS;
+               goto fail;
        }
 
        if (out_param1)
@@ -853,6 +856,9 @@ fail:
                g_array_append_vals(out_param1, address,
                                BT_ADDRESS_STR_LEN);
 
+       g_free(func_data->address);
+       g_free(func_data);
+
        return result;
 }
 
@@ -961,6 +967,9 @@ fail:
                g_array_append_vals(out_param1, address,
                                BT_ADDRESS_STR_LEN);
 
+       g_free(func_data->address);
+       g_free(func_data);
+
        return result;
 }
 
@@ -1298,3 +1307,56 @@ void _bt_audio_set_auto_connect_device_addr(const char *address)
        vconf_set_str(BT_LAST_CONNECTED_DEVICE, address);
 }
 #endif /*TIZEN_BT_A2DP_SINK_AUTO_CONNECT */
+
+int _bt_audio_select_role(bluetooth_audio_role_t role)
+{
+
+       GDBusProxy *proxy;
+       GVariant *ret;
+       GError *error = NULL;
+       GDBusConnection *g_conn = _bt_gdbus_get_system_gconn();
+       gchar *adapter_path = _bt_get_adapter_path();
+
+       BT_INFO("_bt_audio_select_role called [%d]", role);
+
+       retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
+       BT_INFO("Adapter Path = %s", adapter_path);
+
+       proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
+               BT_BLUEZ_NAME, adapter_path, BT_MEDIA_INTERFACE, NULL, &error);
+
+       g_free(adapter_path);
+
+       if (proxy == NULL) {
+               BT_ERR("Unable to create proxy");
+
+               if (error) {
+                       BT_ERR("Error: %s", error->message);
+                       g_clear_error(&error);
+               }
+
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       if (role == BLUETOOTH_A2DP_SOURCE)
+               ret = g_dbus_proxy_call_sync(proxy, "SelectRole", g_variant_new("(s)", "source"),
+                       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
+       else
+               ret = g_dbus_proxy_call_sync(proxy, "SelectRole", g_variant_new("(s)", "sink"),
+                       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
+
+       g_object_unref(proxy);
+
+       if (ret == NULL) {
+               BT_ERR("Call SelectRole Failed");
+               if (error) {
+                       BT_ERR("errCode[%x], message[%s]", error->code, error->message);
+                       g_clear_error(&error);
+               }
+
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       g_variant_unref(ret);
+       return BLUETOOTH_ERROR_NONE;
+}