diff mbox series

[BlueZ,2/2] client: Add -e/--endpoint option to auto register endpoints

Message ID 20220829213738.779598-2-luiz.dentz@gmail.com
State New
Headers show
Series [BlueZ,1/2] shell: Set empty argument if optarg is NULL | expand

Commit Message

Luiz Augusto von Dentz Aug. 29, 2022, 9:37 p.m. UTC
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This adds -e/--endpoint option that can be used to auto register
supported endpoints.
---
 client/main.c   |  14 ++++--
 client/player.c | 122 ++++++++++++++++++++++++++++++++++--------------
 2 files changed, 98 insertions(+), 38 deletions(-)
diff mbox series

Patch

diff --git a/client/main.c b/client/main.c
index 54f21fbdf9a0..6773d52627db 100644
--- a/client/main.c
+++ b/client/main.c
@@ -3109,23 +3109,27 @@  static const struct bt_shell_menu main_menu = {
 
 static const struct option options[] = {
 	{ "agent",	required_argument, 0, 'a' },
+	{ "endpoints",	no_argument, 0, 'e' },
 	{ 0, 0, 0, 0 }
 };
 
 static const char *agent_option;
+static const char *endpoint_option;
 
 static const char **optargs[] = {
-	&agent_option
+	&agent_option,
+	&endpoint_option
 };
 
 static const char *help[] = {
-	"Register agent handler: <capability>"
+	"Register agent handler: <capability>",
+	"Register Media endpoints"
 };
 
 static const struct bt_shell_opt opt = {
 	.options = options,
 	.optno = sizeof(options) / sizeof(struct option),
-	.optstr = "a:",
+	.optstr = "a:e",
 	.optarg = optargs,
 	.help = help,
 };
@@ -3158,6 +3162,10 @@  int main(int argc, char *argv[])
 
 	bt_shell_set_env("DBUS_CONNECTION", dbus_conn);
 
+	if (endpoint_option)
+		bt_shell_set_env("AUTO_REGISTER_ENDPOINT",
+					(void *)endpoint_option);
+
 	admin_add_submenu();
 	player_add_submenu();
 
diff --git a/client/player.c b/client/player.c
index 99b036b8c3ec..3d2f41cb3666 100644
--- a/client/player.c
+++ b/client/player.c
@@ -510,41 +510,6 @@  static char *proxy_description(GDBusProxy *proxy, const char *title,
 					title, path);
 }
 
-static void print_media(GDBusProxy *proxy, const char *description)
-{
-	char *str;
-
-	str = proxy_description(proxy, "Media", description);
-
-	bt_shell_printf("%s\n", str);
-
-	g_free(str);
-}
-
-static void print_player(GDBusProxy *proxy, const char *description)
-{
-	char *str;
-
-	str = proxy_description(proxy, "Player", description);
-
-	bt_shell_printf("%s%s\n", str,
-			default_player == proxy ? "[default]" : "");
-
-	g_free(str);
-}
-
-static void cmd_list(int argc, char *arg[])
-{
-	GList *l;
-
-	for (l = players; l; l = g_list_next(l)) {
-		GDBusProxy *proxy = l->data;
-		print_player(proxy, NULL);
-	}
-
-	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
-}
-
 static void print_iter(const char *label, const char *name,
 						DBusMessageIter *iter)
 {
@@ -627,6 +592,42 @@  static void print_property(GDBusProxy *proxy, const char *name)
 	print_iter("\t", name, &iter);
 }
 
+static void print_media(GDBusProxy *proxy, const char *description)
+{
+	char *str;
+
+	str = proxy_description(proxy, "Media", description);
+
+	bt_shell_printf("%s\n", str);
+	print_property(proxy, "SupportedUUIDs");
+
+	g_free(str);
+}
+
+static void print_player(GDBusProxy *proxy, const char *description)
+{
+	char *str;
+
+	str = proxy_description(proxy, "Player", description);
+
+	bt_shell_printf("%s%s\n", str,
+			default_player == proxy ? "[default]" : "");
+
+	g_free(str);
+}
+
+static void cmd_list(int argc, char *arg[])
+{
+	GList *l;
+
+	for (l = players; l; l = g_list_next(l)) {
+		GDBusProxy *proxy = l->data;
+		print_player(proxy, NULL);
+	}
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+}
+
 static void cmd_show_item(int argc, char *argv[])
 {
 	GDBusProxy *proxy;
@@ -2412,11 +2413,62 @@  static const struct bt_shell_menu endpoint_menu = {
 	{} },
 };
 
+static struct endpoint *endpoint_new(const struct capabilities *cap)
+{
+	struct endpoint *ep;
+
+	ep = new0(struct endpoint, 1);
+	ep->uuid = g_strdup(cap->uuid);
+	ep->codec = cap->codec_id;
+	ep->path = g_strdup_printf("%s/ep%u", BLUEZ_MEDIA_ENDPOINT_PATH,
+					g_list_length(local_endpoints));
+	/* Copy capabilities */
+	iov_append(&ep->caps, cap->data.iov_base, cap->data.iov_len);
+	local_endpoints = g_list_append(local_endpoints, ep);
+
+	return ep;
+}
+
+static void register_endpoints(GDBusProxy *proxy)
+{
+	struct endpoint *ep;
+	DBusMessageIter iter, array;
+
+	if (!g_dbus_proxy_get_property(proxy, "SupportedUUIDs", &iter))
+		return;
+
+	dbus_message_iter_recurse(&iter, &array);
+	while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
+		const char *uuid;
+		size_t i;
+
+		dbus_message_iter_get_basic(&array, &uuid);
+
+		for (i = 0; i < ARRAY_SIZE(caps); i++) {
+			const struct capabilities *cap = &caps[i];
+
+			if (strcasecmp(cap->uuid, uuid))
+				continue;
+
+			ep = endpoint_new(cap);
+			ep->auto_accept = true;
+			ep->cig = BT_ISO_QOS_CIG_UNSET;
+			ep->cis = BT_ISO_QOS_CIS_UNSET;
+			endpoint_register(ep);
+		}
+
+		dbus_message_iter_next(&array);
+	}
+}
+
 static void media_added(GDBusProxy *proxy)
 {
 	medias = g_list_append(medias, proxy);
 
 	print_media(proxy, COLORED_NEW);
+
+	if (bt_shell_get_env("AUTO_REGISTER_ENDPOINT"))
+		register_endpoints(proxy);
 }
 
 static void player_added(GDBusProxy *proxy)