From fd508d395bf9390b8a60b2c807b7312ca8a5a0bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Danis?= Date: Mon, 26 Sep 2022 15:17:43 +0200 Subject: [PATCH] bluez5: backend-native: Add a property to select the modem to use By default no modem is allowed. Property "bluez5.hfphsp-backend-native-modem" can be 'none', 'any' or the modem device string has found in 'Device' property of org.freedesktop.ModemManager1.Modem interface, e.g. for PinePhone "/sys/devices/platform/soc/1c1b000.usb/usb2/2-1". --- spa/plugins/bluez5/backend-native.c | 2 +- spa/plugins/bluez5/modemmanager.c | 48 ++++++++++++++++++++++++++++- spa/plugins/bluez5/modemmanager.h | 6 ++-- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/spa/plugins/bluez5/backend-native.c b/spa/plugins/bluez5/backend-native.c index f7d29d562..dce57a269 100644 --- a/spa/plugins/bluez5/backend-native.c +++ b/spa/plugins/bluez5/backend-native.c @@ -2758,7 +2758,7 @@ struct spa_bt_backend *backend_native_new(struct spa_bt_monitor *monitor, } #endif - backend->modemmanager = mm_register(backend->log, backend->conn, &mm_ops, backend); + backend->modemmanager = mm_register(backend->log, backend->conn, info, &mm_ops, backend); backend->upower = upower_register(backend->log, backend->conn, set_battery_level, backend); return &backend->this; diff --git a/spa/plugins/bluez5/modemmanager.c b/spa/plugins/bluez5/modemmanager.c index e05bdd3ac..165d4e7b8 100644 --- a/spa/plugins/bluez5/modemmanager.c +++ b/spa/plugins/bluez5/modemmanager.c @@ -43,6 +43,7 @@ struct impl { struct spa_log *log; DBusConnection *conn; + char *allowed_modem_device; bool filters_added; DBusPendingCall *pending; DBusPendingCall *voice_pending; @@ -382,6 +383,32 @@ static DBusHandlerResult mm_parse_interfaces(struct impl *this, DBusMessageIter if (spa_streq(interface, MM_DBUS_INTERFACE_MODEM)) { spa_log_debug(this->log, "Found Modem interface %s, path %s", interface, path); if (this->modem.path == NULL) { + if (this->allowed_modem_device) { + DBusMessageIter i; + + dbus_message_iter_recurse(&iface_i, &i); + while (dbus_message_iter_get_arg_type(&i) != DBUS_TYPE_INVALID) { + DBusMessageIter key_i, value_i; + const char *key; + + dbus_message_iter_recurse(&i, &key_i); + + dbus_message_iter_get_basic(&key_i, &key); + dbus_message_iter_next(&key_i); + dbus_message_iter_recurse(&key_i, &value_i); + + if (spa_streq(key, MM_MODEM_PROPERTY_DEVICE)) { + char *device; + + dbus_message_iter_get_basic(&value_i, &device); + if (!spa_streq(this->allowed_modem_device, device)) { + spa_log_debug(this->log, "Modem not allowed: %s", device); + goto next; + } + } + dbus_message_iter_next(&i); + } + } this->modem.path = strdup(path); } else if (!spa_streq(this->modem.path, path)) { spa_log_debug(this->log, "A modem is already registered"); @@ -1148,13 +1175,27 @@ struct spa_list *mm_get_calls(void *modemmanager) return &this->call_list; } -void *mm_register(struct spa_log *log, void *dbus_connection, const struct mm_ops *ops, void *user_data) +void *mm_register(struct spa_log *log, void *dbus_connection, const struct spa_dict *info, + const struct mm_ops *ops, void *user_data) { struct impl *this; + const char *modem_device_str = NULL; + bool modem_device_found = false; spa_assert(log); spa_assert(dbus_connection); + if (info) { + if ((modem_device_str = spa_dict_lookup(info, "bluez5.hfphsp-backend-native-modem")) != NULL) { + if (!spa_streq(modem_device_str, "none")) + modem_device_found = true; + } + } + if (!modem_device_found) { + spa_log_info(log, "No modem allowed, doesn't link to ModemManager"); + return NULL; + } + this = calloc(1, sizeof(struct impl)); if (this == NULL) return NULL; @@ -1163,6 +1204,8 @@ void *mm_register(struct spa_log *log, void *dbus_connection, const struct mm_op this->conn = dbus_connection; this->ops = ops; this->user_data = user_data; + if (modem_device_str && !spa_streq(modem_device_str, "any")) + this->allowed_modem_device = strdup(modem_device_str); spa_list_init(&this->call_list); if (add_filters(this) < 0) { @@ -1209,5 +1252,8 @@ void mm_unregister(void *data) this->filters_added = false; } + if (this->allowed_modem_device) + free(this->allowed_modem_device); + free(this); } diff --git a/spa/plugins/bluez5/modemmanager.h b/spa/plugins/bluez5/modemmanager.h index 27c590f08..869dfd785 100644 --- a/spa/plugins/bluez5/modemmanager.h +++ b/spa/plugins/bluez5/modemmanager.h @@ -87,7 +87,8 @@ struct mm_ops { }; #ifdef HAVE_BLUEZ_5_BACKEND_NATIVE_MM -void *mm_register(struct spa_log *log, void *dbus_connection, const struct mm_ops *ops, void *user_data); +void *mm_register(struct spa_log *log, void *dbus_connection, const struct spa_dict *info, + const struct mm_ops *ops, void *user_data); void mm_unregister(void *data); bool mm_is_available(void *modemmanager); unsigned int mm_supported_features(); @@ -98,7 +99,8 @@ bool mm_send_dtmf(void *modemmanager, const char *dtmf, void *user_data, enum cm const char *mm_get_incoming_call_number(void *modemmanager); struct spa_list *mm_get_calls(void *modemmanager); #else -void *mm_register(struct spa_log *log, void *dbus_connection, const struct mm_ops *ops, void *user_data) +void *mm_register(struct spa_log *log, void *dbus_connection, const struct spa_dict *info, + const struct mm_ops *ops, void *user_data) { return NULL; }