return btd_error_invalid_args(msg);
if (!g_strcmp0(a2dp_role, A2DP_SINK_ROLE)) {
- btd_adapter_set_a2dp_role(adapter->btd_adapter, BLUETOOTH_A2DP_SINK_ROLE);
- a2dp_remove_sep(source_endpoint->sep);
+ if (btd_adapter_get_a2dp_role(adapter->btd_adapter) == BLUETOOTH_A2DP_SINK_ROLE) {
+ DBG("Already selected A2DP sink role");
+ return btd_error_already_exists(msg);
+ }
+
+ if (sink_endpoint == NULL) {
+ DBG("sink_endpoint is NULL, so can't init the sink_endpoint");
+ return btd_error_not_available(msg);
+ }
+
ret = endpoint_init_a2dp_sink(sink_endpoint, current_delay_reporting, NULL);
- if (!ret)
+ if (!ret) {
DBG("could not init a2dp sink");
+ return btd_error_failed(msg, "Can't init a2dp sink");
+ }
+
+ btd_adapter_set_a2dp_role(adapter->btd_adapter, BLUETOOTH_A2DP_SINK_ROLE);
+
+ if (source_endpoint && source_endpoint->sep)
+ a2dp_remove_sep(source_endpoint->sep);
+
+ source_endpoint->sep = NULL;
} else if (!g_strcmp0(a2dp_role, A2DP_SOURCE_ROLE)) {
- btd_adapter_set_a2dp_role(adapter->btd_adapter, BLUETOOTH_A2DP_SOURCE_ROLE);
- a2dp_remove_sep(sink_endpoint->sep);
+ if (btd_adapter_get_a2dp_role(adapter->btd_adapter) == BLUETOOTH_A2DP_SOURCE_ROLE) {
+ DBG("Already selected A2DP sink role");
+ return btd_error_already_exists(msg);
+ }
+
+ if (source_endpoint == NULL) {
+ DBG("source_endpoint is NULL, so can't init the source_endpoint");
+ return btd_error_not_available(msg);
+ }
+
ret = endpoint_init_a2dp_source(source_endpoint, current_delay_reporting, NULL);
- if (!ret)
+ if (!ret) {
DBG("could not init a2dp source");
+ return btd_error_failed(msg, "Can't init a2dp source");
+ }
+
+ btd_adapter_set_a2dp_role(adapter->btd_adapter, BLUETOOTH_A2DP_SOURCE_ROLE);
+
+ if (sink_endpoint && sink_endpoint->sep)
+ a2dp_remove_sep(sink_endpoint->sep);
+
+ sink_endpoint->sep = NULL;
} else {
DBG("invalid a2dp role");
return btd_error_invalid_args(msg);