From 4bdf4957477eaf2d241a3b3117ef290c6ddc2190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Thu, 21 Oct 2021 00:42:49 +0200 Subject: [PATCH] spa: bluez: move profile selection to function Move the profile selection from `profile_new_connection()` into a separate function to avoid checking the string more than necessary. --- spa/plugins/bluez5/backend-native.c | 51 ++++++++++++++--------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/spa/plugins/bluez5/backend-native.c b/spa/plugins/bluez5/backend-native.c index c0d30368b..9473a00b3 100644 --- a/spa/plugins/bluez5/backend-native.c +++ b/spa/plugins/bluez5/backend-native.c @@ -1630,13 +1630,34 @@ static const struct spa_bt_device_events device_events = { .destroy = device_destroy, }; +static enum spa_bt_profile path_to_profile(const char *path) +{ +#ifdef HAVE_BLUEZ_5_BACKEND_HSP_NATIVE + if (spa_streq(path, PROFILE_HSP_AG)) + return SPA_BT_PROFILE_HSP_HS; + + if (spa_streq(path, PROFILE_HSP_HS)) + return SPA_BT_PROFILE_HSP_AG; +#endif + +#ifdef HAVE_BLUEZ_5_BACKEND_HFP_NATIVE + if (spa_streq(path, PROFILE_HFP_AG)) + return SPA_BT_PROFILE_HFP_HF; + + if (spa_streq(path, PROFILE_HFP_HF)) + return SPA_BT_PROFILE_HFP_AG; +#endif + + return SPA_BT_PROFILE_NULL; +} + static DBusHandlerResult profile_new_connection(DBusConnection *conn, DBusMessage *m, void *userdata) { struct impl *backend = userdata; DBusMessage *r; DBusMessageIter it[5]; const char *handler, *path; - enum spa_bt_profile profile = SPA_BT_PROFILE_NULL; + enum spa_bt_profile profile; struct rfcomm *rfcomm; struct spa_bt_device *d; struct spa_bt_transport *t = NULL; @@ -1648,19 +1669,7 @@ static DBusHandlerResult profile_new_connection(DBusConnection *conn, DBusMessag } handler = dbus_message_get_path(m); -#ifdef HAVE_BLUEZ_5_BACKEND_HSP_NATIVE - if (spa_streq(handler, PROFILE_HSP_AG)) - profile = SPA_BT_PROFILE_HSP_HS; - else if (spa_streq(handler, PROFILE_HSP_HS)) - profile = SPA_BT_PROFILE_HSP_AG; -#endif -#ifdef HAVE_BLUEZ_5_BACKEND_HFP_NATIVE - if (spa_streq(handler, PROFILE_HFP_AG)) - profile = SPA_BT_PROFILE_HFP_HF; - else if (spa_streq(handler, PROFILE_HFP_HF)) - profile = SPA_BT_PROFILE_HFP_AG; -#endif - + profile = path_to_profile(handler); if (profile == SPA_BT_PROFILE_NULL) { spa_log_warn(backend->log, "invalid handler %s", handler); return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; @@ -1787,19 +1796,7 @@ static DBusHandlerResult profile_request_disconnection(DBusConnection *conn, DBu } handler = dbus_message_get_path(m); -#ifdef HAVE_BLUEZ_5_BACKEND_HSP_NATIVE - if (spa_streq(handler, PROFILE_HSP_AG)) - profile = SPA_BT_PROFILE_HSP_HS; - else if (spa_streq(handler, PROFILE_HSP_HS)) - profile = SPA_BT_PROFILE_HSP_AG; -#endif -#ifdef HAVE_BLUEZ_5_BACKEND_HFP_NATIVE - if (spa_streq(handler, PROFILE_HFP_AG)) - profile = SPA_BT_PROFILE_HFP_HF; - else if (spa_streq(handler, PROFILE_HFP_HF)) - profile = SPA_BT_PROFILE_HFP_AG; -#endif - + profile = path_to_profile(handler); if (profile == SPA_BT_PROFILE_NULL) { spa_log_warn(backend->log, "invalid handler %s", handler); return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;