public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v1 2/3] client/player: Fix prompting for bcode when one is already set
Date: Thu,  9 Oct 2025 12:21:10 -0400	[thread overview]
Message-ID: <20251009162111.221677-2-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20251009162111.221677-1-luiz.dentz@gmail.com>

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

If bcode is non-zeroed it means it already has been set so there is no
reason to ask the user to overwrite it, also fixes the assumption that
only strings could be entered as bcode rather than a byte array.
---
 client/player.c | 67 +++++++++++++++++++++++++++++++++++++------------
 1 file changed, 51 insertions(+), 16 deletions(-)

diff --git a/client/player.c b/client/player.c
index bb193dafeba0..a8d05cf175c9 100644
--- a/client/player.c
+++ b/client/player.c
@@ -5263,17 +5263,26 @@ static void set_bcode_cb(const DBusError *error, void *user_data)
 static void set_bcode(const char *input, void *user_data)
 {
 	struct transport_select_args *args = user_data;
-	char *bcode;
+	uint8_t *bcode = NULL;
+	size_t len = 0;
 
 	if (!strcasecmp(input, "n") || !strcasecmp(input, "no"))
-		bcode = g_new0(char, 16);
-	else
-		bcode = g_strdup(input);
+		bcode = g_new0(uint8_t, 16);
+	else {
+		bcode = str2bytearray((char *) input, &len);
+		/* If the input is not 16 bytes, perhaps it was entered as
+		 * string so just use it instead.
+		 */
+		if (len != 16) {
+			bcode = (uint8_t *)strdup(input);
+			len = strlen(input);
+		}
+	}
 
 	if (g_dbus_proxy_set_property_dict(args->proxy, "QoS",
 				set_bcode_cb, user_data,
 				NULL, "BCode", DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
-				strlen(bcode), bcode, NULL) == FALSE) {
+				len, bcode, NULL) == FALSE) {
 		bt_shell_printf("Setting broadcast code failed\n");
 		g_free(bcode);
 		free_transport_select_args(args);
@@ -5295,9 +5304,11 @@ static void transport_select(struct transport_select_args *args)
 
 static void transport_set_bcode(struct transport_select_args *args)
 {
-	DBusMessageIter iter, array, entry, value;
-	unsigned char encryption;
+	DBusMessageIter iter, array;
+	unsigned char encryption = 0;
 	const char *key;
+	uint8_t *bcode, zeroed_bcode[16] = {};
+	int bcode_len = 0;
 
 	if (g_dbus_proxy_get_property(args->proxy, "QoS", &iter) == FALSE) {
 		free_transport_select_args(args);
@@ -5308,24 +5319,48 @@ static void transport_set_bcode(struct transport_select_args *args)
 
 	while (dbus_message_iter_get_arg_type(&array) !=
 						DBUS_TYPE_INVALID) {
+		DBusMessageIter entry, value, array_value;
+		int var;
+
 		dbus_message_iter_recurse(&array, &entry);
 		dbus_message_iter_get_basic(&entry, &key);
 
+		dbus_message_iter_next(&entry);
+		dbus_message_iter_recurse(&entry, &value);
+
+		var = dbus_message_iter_get_arg_type(&value);
+
 		if (!strcasecmp(key, "Encryption")) {
-			dbus_message_iter_next(&entry);
-			dbus_message_iter_recurse(&entry, &value);
+			if (var != DBUS_TYPE_BYTE)
+				break;
+
 			dbus_message_iter_get_basic(&value, &encryption);
-			if (encryption == 1) {
-				bt_shell_prompt_input("",
-					"Enter brocast code[value/no]:",
-					set_bcode, args);
-				return;
-			}
-			break;
+		} else if (!strcasecmp(key, "BCode")) {
+			if (var != DBUS_TYPE_ARRAY)
+				break;
+
+			dbus_message_iter_recurse(&value, &array_value);
+			dbus_message_iter_get_fixed_array(&array_value, &bcode,
+								&bcode_len);
+
+			if (bcode_len != 16 || !memcmp(bcode, zeroed_bcode, 16))
+				bcode_len = 0;
 		}
+
 		dbus_message_iter_next(&array);
 	}
 
+	/* Only attempt to set bcode if encryption is enabled and
+	 * bcode is not already set.
+	 */
+	if (encryption == 1 && !bcode_len) {
+		const char *path = g_dbus_proxy_get_path(args->proxy);
+
+		bt_shell_prompt_input(path, "Enter bcode[value/no]:",
+					set_bcode, args);
+		return;
+	}
+
 	/* Go straight to selecting transport, if Broadcast Code
 	 * is not required.
 	 */
-- 
2.51.0


  reply	other threads:[~2025-10-09 16:21 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-09 16:21 [PATCH BlueZ v1 1/3] monitor: Fix instance where BN is printed as PTO Luiz Augusto von Dentz
2025-10-09 16:21 ` Luiz Augusto von Dentz [this message]
2025-10-09 16:21 ` [PATCH BlueZ v1 3/3] bass: Fix restricting the BIS indexes to 1 as assistant Luiz Augusto von Dentz
2025-10-09 17:44 ` [BlueZ,v1,1/3] monitor: Fix instance where BN is printed as PTO bluez.test.bot
2025-10-09 19:40 ` [PATCH BlueZ v1 1/3] " patchwork-bot+bluetooth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251009162111.221677-2-luiz.dentz@gmail.com \
    --to=luiz.dentz@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox