[PATCH v2 2/2] client: make advertise.name use public broadcast name

raghu447
2026-04-24T14:19:04Z

From: raghavendra <raghavendra.rao@collabora.com>

---
client/advertising.c | 74 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 74 insertions(+)

diff --git a/client/advertising.c b/client/advertising.c
index fc51f8193..6921bedec 100644
--- a/client/advertising.c
+++ b/client/advertising.c
@@ -31,6 +31,7 @@
#define AD_PATH "/org/bluez/advertising"
#define AD_IFACE "org.bluez.LEAdvertisement1"
#define AD_PUBLIC_BROADCAST_UUID "0x1856"
+#define AD_TYPE_PUBLIC_BROADCAST_NAME 0x30

struct ad_data {
uint8_t data[245];
@@ -1014,6 +1015,62 @@ static bool ad_is_public_broadcast_uuid(const char *uuid)
return uuid && !bt_uuid_strcmp(uuid, AD_PUBLIC_BROADCAST_UUID);
}

+static bool ad_broadcast_in_use(void)
+{
+ return ad_is_public_broadcast_uuid(ad.service[AD_TYPE_AD].uuid);
+}
+
+static bool ad_has_public_broadcast_name(void)
+{
+ return ad.data[AD_TYPE_AD].valid &&
+ ad.data[AD_TYPE_AD].type == AD_TYPE_PUBLIC_BROADCAST_NAME &&
+ ad.data[AD_TYPE_AD].data.len > 0;
+}
+
+static void ad_print_public_broadcast_name(void)
+{
+ if (!ad_has_public_broadcast_name()) {
+ bt_shell_printf("Public Broadcast Name not set\n");
+ return;
+ }
+
+ bt_shell_printf("Public Broadcast Name: %.*s\n",
+ (int) ad.data[AD_TYPE_AD].data.len,
+ (char *) ad.data[AD_TYPE_AD].data.data);
+}
+
+static bool ad_set_public_broadcast_name(DBusConnection *conn,
+ const char *name)
+{
+ struct ad_data data;
+ size_t len;
+
+ if (!name || !strlen(name)) {
+ bt_shell_printf("Public Broadcast Name cannot be empty\n");
+ return false;
+ }
+
+ len = strlen(name);
+ if (len > sizeof(data.data)) {
+ bt_shell_printf("Public Broadcast Name too long\n");
+ return false;
+ }
+
+ memset(&data, 0, sizeof(data));
+ memcpy(data.data, name, len);
+ data.len = len;
+
+ ad_clear_data(AD_TYPE_AD);
+ ad.data[AD_TYPE_AD].valid = true;
+ ad.data[AD_TYPE_AD].type = AD_TYPE_PUBLIC_BROADCAST_NAME;
+ ad.data[AD_TYPE_AD].data = data;
+
+ g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE,
+ prop_names.data[AD_TYPE_AD]);
+
+ return true;
+}
+
static const char *ad_public_broadcast_state(void)
{
if (!ad_is_public_broadcast_uuid(ad.service[AD_TYPE_AD].uuid))
@@ -1200,6 +1257,23 @@ void ad_advertise_name(DBusConnection *conn, bool value)

void ad_advertise_local_name(DBusConnection *conn, const char *name)
{
+ if (ad_broadcast_in_use()) {
+ if (!name) {
+ ad_print_public_broadcast_name();
+ return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+ }
+
+ if (ad_has_public_broadcast_name() &&
+ ad.data[AD_TYPE_AD].data.len == strlen(name) &&
+ !memcmp(ad.data[AD_TYPE_AD].data.data, name, strlen(name)))
+ return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+
+ if (!ad_set_public_broadcast_name(conn, name))
+ return bt_shell_noninteractive_quit(EXIT_FAILURE);
+
+ return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+ }
+
if (!name) {
if (ad.local_name)
bt_shell_printf("LocalName: %s\n", ad.local_name);
--
2.53.0