Skip to content

Commit

Permalink
Check for SEP init failure when creating transport
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Oct 13, 2024
1 parent e640aa4 commit 4b7f1ce
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 56 deletions.
11 changes: 0 additions & 11 deletions src/a2dp.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#endif
#include "a2dp-sbc.h"
#include "ba-config.h"
#include "ba-transport.h"
#include "shared/a2dp-codecs.h"
#include "shared/log.h"

Expand Down Expand Up @@ -390,13 +389,3 @@ const char *a2dp_check_strerror(
debug("Unknown error code: %#x", err);
return "Check error";
}

int a2dp_transport_init(
struct ba_transport *t) {
return t->a2dp.sep->transport_init(t);
}

int a2dp_transport_start(
struct ba_transport *t) {
return t->a2dp.sep->transport_start(t);
}
5 changes: 0 additions & 5 deletions src/a2dp.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,4 @@ enum a2dp_check_err a2dp_check_configuration(
const char *a2dp_check_strerror(
enum a2dp_check_err err);

int a2dp_transport_init(
struct ba_transport *t);
int a2dp_transport_start(
struct ba_transport *t);

#endif
63 changes: 35 additions & 28 deletions src/ba-transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,9 @@ struct ba_transport *ba_transport_new_a2dp(
t->a2dp.sep = sep;
memcpy(&t->a2dp.configuration, configuration, sep->config.caps_size);

t->acquire = transport_acquire_bt_a2dp;
t->release = transport_release_bt_a2dp;

err |= transport_pcm_init(&t->a2dp.pcm,
is_sink ? BA_TRANSPORT_PCM_MODE_SOURCE : BA_TRANSPORT_PCM_MODE_SINK,
t, true);
Expand All @@ -432,17 +435,18 @@ struct ba_transport *ba_transport_new_a2dp(
if (err != 0)
goto fail;

/* do codec-specific initialization */
if (sep->transport_init(t) != 0) {
errno = EINVAL;
goto fail;
}

if ((errno = pthread_create(&t->thread_manager_thread_id,
NULL, PTHREAD_FUNC(transport_thread_manager), t)) != 0) {
t->thread_manager_thread_id = config.main_thread;
goto fail;
}

t->acquire = transport_acquire_bt_a2dp;
t->release = transport_release_bt_a2dp;

a2dp_transport_init(t);

storage_pcm_data_sync(&t->a2dp.pcm);
storage_pcm_data_sync(&t->a2dp.pcm_bc);

Expand Down Expand Up @@ -552,26 +556,6 @@ struct ba_transport *ba_transport_new_sco(
* there is no other option than the CVSD codec. */
t->codec_id = HFP_CODEC_CVSD;

err |= transport_pcm_init(&t->sco.pcm_spk,
is_ag ? BA_TRANSPORT_PCM_MODE_SINK : BA_TRANSPORT_PCM_MODE_SOURCE,
t, true);

err |= transport_pcm_init(&t->sco.pcm_mic,
is_ag ? BA_TRANSPORT_PCM_MODE_SOURCE : BA_TRANSPORT_PCM_MODE_SINK,
t, false);

if (err != 0)
goto fail;

if ((errno = pthread_create(&t->thread_manager_thread_id,
NULL, PTHREAD_FUNC(transport_thread_manager), t)) != 0) {
t->thread_manager_thread_id = config.main_thread;
goto fail;
}

t->acquire = transport_acquire_bt_sco;
t->release = transport_release_bt_sco;

#if ENABLE_HFP_CODEC_SELECTION
/* Only HFP supports codec selection. */
if (profile & BA_TRANSPORT_PROFILE_MASK_HFP &&
Expand All @@ -589,7 +573,30 @@ struct ba_transport *ba_transport_new_sco(
}
#endif

sco_transport_init(t);
t->acquire = transport_acquire_bt_sco;
t->release = transport_release_bt_sco;

err |= transport_pcm_init(&t->sco.pcm_spk,
is_ag ? BA_TRANSPORT_PCM_MODE_SINK : BA_TRANSPORT_PCM_MODE_SOURCE,
t, true);

err |= transport_pcm_init(&t->sco.pcm_mic,
is_ag ? BA_TRANSPORT_PCM_MODE_SOURCE : BA_TRANSPORT_PCM_MODE_SINK,
t, false);

if (err != 0)
goto fail;

if (sco_transport_init(t) != 0) {
errno = EINVAL;
goto fail;
}

if ((errno = pthread_create(&t->thread_manager_thread_id,
NULL, PTHREAD_FUNC(transport_thread_manager), t)) != 0) {
t->thread_manager_thread_id = config.main_thread;
goto fail;
}

storage_pcm_data_sync(&t->sco.pcm_spk);
storage_pcm_data_sync(&t->sco.pcm_mic);
Expand Down Expand Up @@ -1026,7 +1033,7 @@ void ba_transport_set_codec(
return;

if (t->profile & BA_TRANSPORT_PROFILE_MASK_A2DP)
a2dp_transport_init(t);
t->a2dp.sep->transport_init(t);
else if (t->profile & BA_TRANSPORT_PROFILE_MASK_SCO)
sco_transport_init(t);

Expand Down Expand Up @@ -1077,7 +1084,7 @@ int ba_transport_start(struct ba_transport *t) {
debug("Starting transport: %s", ba_transport_debug_name(t));

if (t->profile & BA_TRANSPORT_PROFILE_MASK_A2DP)
return a2dp_transport_start(t);
return t->a2dp.sep->transport_start(t);

if (t->profile & BA_TRANSPORT_PROFILE_MASK_SCO)
return sco_transport_start(t);
Expand Down
3 changes: 2 additions & 1 deletion src/sco.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ void *sco_dec_thread(struct ba_transport_pcm *pcm) {
}
}

void sco_transport_init(struct ba_transport *t) {
int sco_transport_init(struct ba_transport *t) {

t->sco.pcm_spk.format = BA_TRANSPORT_PCM_FORMAT_S16_2LE;
t->sco.pcm_spk.channels = 1;
Expand Down Expand Up @@ -305,6 +305,7 @@ void sco_transport_init(struct ba_transport *t) {
BA_DBUS_PCM_UPDATE_CODEC |
BA_DBUS_PCM_UPDATE_CLIENT_DELAY);

return 0;
}

int sco_transport_start(struct ba_transport *t) {
Expand Down
4 changes: 2 additions & 2 deletions src/sco.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* BlueALSA - sco.h
* Copyright (c) 2016-2023 Arkadiusz Bokowy
* Copyright (c) 2016-2024 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
*
Expand All @@ -20,7 +20,7 @@
#include "ba-transport.h"

int sco_setup_connection_dispatcher(struct ba_adapter *a);
void sco_transport_init(struct ba_transport *t);
int sco_transport_init(struct ba_transport *t);
int sco_transport_start(struct ba_transport *t);

#endif
15 changes: 11 additions & 4 deletions test/test-ba.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
/* Keep persistent storage in the current directory. */
#define TEST_BLUEALSA_STORAGE_DIR "storage-test-ba"

int a2dp_transport_init(struct ba_transport *t) { (void)t; return 0; }
int a2dp_transport_start(struct ba_transport *t) { (void)t; return 0; }
int midi_transport_alsa_seq_create(struct ba_transport *t) { (void)t; return 0; }
int midi_transport_alsa_seq_delete(struct ba_transport *t) { (void)t; return 0; }
int midi_transport_start(struct ba_transport *t) { (void)t; return 0; }
Expand Down Expand Up @@ -313,6 +311,11 @@ CK_START_TEST(test_ba_transport_pcm_format) {

} CK_END_TEST

static int sep_transport_init(struct ba_transport *t) {
(void)t;
return 0;
}

CK_START_TEST(test_ba_transport_pcm_volume) {

struct ba_adapter *a;
Expand All @@ -325,7 +328,9 @@ CK_START_TEST(test_ba_transport_pcm_volume) {
ck_assert_ptr_ne(d = ba_device_new(a, &addr), NULL);
ck_assert_int_eq(storage_device_clear(d), 0);

struct a2dp_sep sep = { .config = { .type = A2DP_SINK, .codec_id = A2DP_CODEC_SBC } };
struct a2dp_sep sep = {
.config = { .type = A2DP_SINK, .codec_id = A2DP_CODEC_SBC },
.transport_init = sep_transport_init };
a2dp_sbc_t configuration = { .channel_mode = SBC_CHANNEL_MODE_STEREO };
ck_assert_ptr_ne(t_a2dp = ba_transport_new_a2dp(d,
BA_TRANSPORT_PROFILE_A2DP_SINK, "/owner", "/path/a2dp", &sep,
Expand Down Expand Up @@ -410,7 +415,9 @@ CK_START_TEST(test_storage) {
ck_assert_ptr_ne(a = ba_adapter_new(0), NULL);
ck_assert_ptr_ne(d = ba_device_new(a, &addr), NULL);

struct a2dp_sep sep = { .config = { .type = A2DP_SINK, .codec_id = A2DP_CODEC_SBC } };
struct a2dp_sep sep = {
.config = { .type = A2DP_SINK, .codec_id = A2DP_CODEC_SBC },
.transport_init = sep_transport_init };
a2dp_sbc_t configuration = { .channel_mode = SBC_CHANNEL_MODE_STEREO };
ck_assert_ptr_ne(t = ba_transport_new_a2dp(d,
BA_TRANSPORT_PROFILE_A2DP_SINK, "/owner", "/path", &sep,
Expand Down
9 changes: 6 additions & 3 deletions test/test-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,9 @@ static struct ba_transport *test_transport_new_a2dp(
if (input_bt_file != NULL)
configuration = &btdin->a2dp_configuration;
#endif
struct ba_transport *t = ba_transport_new_a2dp(device, profile, ":test",
dbus_path, sep, configuration);
struct ba_transport *t;
ck_assert_ptr_nonnull(t = ba_transport_new_a2dp(device, profile, ":test",
dbus_path, sep, configuration));
t->acquire = test_transport_acquire;
t->release = test_transport_release_bt_a2dp;
return t;
Expand Down Expand Up @@ -772,7 +773,9 @@ CK_START_TEST(test_a2dp_sbc) {

CK_START_TEST(test_a2dp_sbc_invalid_config) {

const a2dp_sbc_t config_sbc_invalid = { 0 };
const a2dp_sbc_t config_sbc_invalid = {
.sampling_freq = SBC_SAMPLING_FREQ_44100,
.channel_mode = SBC_CHANNEL_MODE_STEREO };
struct ba_transport *t = test_transport_new_a2dp(device1,
BA_TRANSPORT_PROFILE_A2DP_SOURCE, "/path/sbc", &a2dp_sbc_source,
&config_sbc_invalid);
Expand Down
2 changes: 0 additions & 2 deletions test/test-rfcomm.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ static void dbus_update_counters_wait(unsigned int *counter, unsigned int value)
pthread_mutex_unlock(&dbus_update_mtx);
}

int a2dp_transport_init(struct ba_transport *t) { (void)t; return 0; }
int a2dp_transport_start(struct ba_transport *t) { (void)t; return 0; }
int midi_transport_alsa_seq_create(struct ba_transport *t) { (void)t; return 0; }
int midi_transport_alsa_seq_delete(struct ba_transport *t) { (void)t; return 0; }
int midi_transport_start(struct ba_transport *t) { (void)t; return 0; }
Expand Down

0 comments on commit 4b7f1ce

Please sign in to comment.