* [PATCH BlueZ 1/3] client/btpclient: refactor read-commands bitmap building
2026-05-19 10:55 [PATCH BlueZ 0/3] client/btpclient: Add GAP extended advertising support Frédéric Danis
@ 2026-05-19 10:55 ` Frédéric Danis
2026-05-19 12:34 ` client/btpclient: Add GAP extended advertising support bluez.test.bot
2026-05-19 10:55 ` [PATCH BlueZ 2/3] client/btpclient: Replace advertising defines by shared ones Frédéric Danis
` (2 subsequent siblings)
3 siblings, 1 reply; 6+ messages in thread
From: Frédéric Danis @ 2026-05-19 10:55 UTC (permalink / raw)
To: linux-bluetooth
Add add_supported_command() and use it across all btp_*_read_commands
handlers.
This replaces fixed-size bitmask assembly with a dynamically growing
command bitmap based on opcode index, avoiding shift-width issues with
higher opcodes.
This will allow to add opcodes like GAP_SET_EXTENDED_ADVERTISING
(0x21).
Refactor core, gap, gatt and bap read-commands paths to build response
bitmaps through the shared helper and preserve existing error handling
for allocation failures.
Assisted-by: GPT:GPT-5.3-Codex
---
client/btpclient/bap.c | 27 +++++++++++++----
client/btpclient/btpclient.c | 25 +++++++++++++++
client/btpclient/btpclient.h | 5 +++
client/btpclient/core.c | 29 ++++++++++++++----
client/btpclient/gap.c | 59 +++++++++++++++++++++++-------------
client/btpclient/gatt.c | 39 ++++++++++++++++--------
6 files changed, 139 insertions(+), 45 deletions(-)
diff --git a/client/btpclient/bap.c b/client/btpclient/bap.c
index d64828894..0302fc1c0 100644
--- a/client/btpclient/bap.c
+++ b/client/btpclient/bap.c
@@ -27,7 +27,13 @@ static bool bap_service_registered;
static void btp_bap_read_commands(uint8_t index, const void *param,
uint16_t length, void *user_data)
{
- uint16_t commands = 0;
+ const uint8_t supported_commands[] = {
+ BTP_OP_BAP_READ_SUPPORTED_COMMANDS,
+ BTP_OP_BAP_DISCOVER,
+ };
+ uint8_t *commands = NULL;
+ size_t commands_len = 0;
+ size_t i;
if (index != BTP_INDEX_NON_CONTROLLER) {
btp_send_error(btp, BTP_BAP_SERVICE, index,
@@ -35,13 +41,22 @@ static void btp_bap_read_commands(uint8_t index, const void *param,
return;
}
- commands |= (1 << BTP_OP_BAP_READ_SUPPORTED_COMMANDS);
- commands |= (1 << BTP_OP_BAP_DISCOVER);
-
- commands = L_CPU_TO_LE16(commands);
+ for (i = 0; i < L_ARRAY_SIZE(supported_commands); i++) {
+ if (!add_supported_command(&commands, &commands_len,
+ supported_commands[i]))
+ goto failed;
+ }
btp_send(btp, BTP_BAP_SERVICE, BTP_OP_BAP_READ_SUPPORTED_COMMANDS,
- BTP_INDEX_NON_CONTROLLER, sizeof(commands), &commands);
+ BTP_INDEX_NON_CONTROLLER, commands_len, commands);
+
+ l_free(commands);
+
+ return;
+
+failed:
+ l_free(commands);
+ btp_send_error(btp, BTP_BAP_SERVICE, index, BTP_ERROR_FAIL);
}
static void btp_bap_discover(uint8_t index, const void *param, uint16_t length,
diff --git a/client/btpclient/btpclient.c b/client/btpclient/btpclient.c
index ee46966dc..0d124bc7a 100644
--- a/client/btpclient/btpclient.c
+++ b/client/btpclient/btpclient.c
@@ -38,6 +38,31 @@ static struct btp *btp;
static struct btp_agent ag;
+bool add_supported_command(uint8_t **commands, size_t *commands_len,
+ uint8_t command)
+{
+ size_t cmd_byte = command / 8;
+
+ if (cmd_byte >= *commands_len) {
+ size_t old_len = *commands_len;
+ size_t new_len = cmd_byte + 1;
+ uint8_t *tmp;
+
+ tmp = l_realloc(*commands, new_len);
+ if (!tmp)
+ return false;
+
+ memset(tmp + old_len, 0, new_len - old_len);
+
+ *commands = tmp;
+ *commands_len = new_len;
+ }
+
+ (*commands)[cmd_byte] |= (1U << (command % 8));
+
+ return true;
+}
+
struct l_queue *get_adapters_list(void)
{
return adapters;
diff --git a/client/btpclient/btpclient.h b/client/btpclient/btpclient.h
index e26a08413..8a5ea2172 100644
--- a/client/btpclient/btpclient.h
+++ b/client/btpclient/btpclient.h
@@ -7,6 +7,8 @@
*
*/
+#include <stddef.h>
+
struct btp_adapter {
struct l_dbus_proxy *proxy;
struct l_dbus_proxy *ad_proxy;
@@ -54,3 +56,6 @@ struct btp_agent *get_agent(void);
bool request_default_agent(l_dbus_client_proxy_result_func_t reply,
void *user_data,
l_dbus_destroy_func_t destroy);
+
+bool add_supported_command(uint8_t **commands, size_t *commands_len,
+ uint8_t command);
diff --git a/client/btpclient/core.c b/client/btpclient/core.c
index 772178670..f5d64904f 100644
--- a/client/btpclient/core.c
+++ b/client/btpclient/core.c
@@ -24,7 +24,15 @@ static struct l_dbus *dbus;
static void btp_core_read_commands(uint8_t index, const void *param,
uint16_t length, void *user_data)
{
- uint8_t commands = 0;
+ const uint8_t supported_commands[] = {
+ BTP_OP_CORE_READ_SUPPORTED_COMMANDS,
+ BTP_OP_CORE_READ_SUPPORTED_SERVICES,
+ BTP_OP_CORE_REGISTER,
+ BTP_OP_CORE_UNREGISTER,
+ };
+ uint8_t *commands = NULL;
+ size_t commands_len = 0;
+ size_t i;
if (index != BTP_INDEX_NON_CONTROLLER) {
btp_send_error(btp, BTP_CORE_SERVICE, index,
@@ -32,13 +40,22 @@ static void btp_core_read_commands(uint8_t index, const void *param,
return;
}
- commands |= (1 << BTP_OP_CORE_READ_SUPPORTED_COMMANDS);
- commands |= (1 << BTP_OP_CORE_READ_SUPPORTED_SERVICES);
- commands |= (1 << BTP_OP_CORE_REGISTER);
- commands |= (1 << BTP_OP_CORE_UNREGISTER);
+ for (i = 0; i < L_ARRAY_SIZE(supported_commands); i++) {
+ if (!add_supported_command(&commands, &commands_len,
+ supported_commands[i]))
+ goto failed;
+ }
btp_send(btp, BTP_CORE_SERVICE, BTP_OP_CORE_READ_SUPPORTED_COMMANDS,
- BTP_INDEX_NON_CONTROLLER, sizeof(commands), &commands);
+ BTP_INDEX_NON_CONTROLLER, commands_len, commands);
+
+ l_free(commands);
+
+ return;
+
+failed:
+ l_free(commands);
+ btp_send_error(btp, BTP_CORE_SERVICE, index, BTP_ERROR_FAIL);
}
static void btp_core_read_services(uint8_t index, const void *param,
diff --git a/client/btpclient/gap.c b/client/btpclient/gap.c
index 68e029dcc..916626f1d 100644
--- a/client/btpclient/gap.c
+++ b/client/btpclient/gap.c
@@ -99,7 +99,30 @@ static char *dupuuid2str(const uint8_t *uuid, uint8_t len)
static void btp_gap_read_commands(uint8_t index, const void *param,
uint16_t length, void *user_data)
{
- uint16_t commands = 0;
+ const uint8_t supported_commands[] = {
+ BTP_OP_GAP_READ_SUPPORTED_COMMANDS,
+ BTP_OP_GAP_READ_CONTROLLER_INDEX_LIST,
+ BTP_OP_GAP_READ_CONTROLLER_INFO,
+ BTP_OP_GAP_RESET,
+ BTP_OP_GAP_SET_POWERED,
+ BTP_OP_GAP_SET_CONNECTABLE,
+ BTP_OP_GAP_SET_DISCOVERABLE,
+ BTP_OP_GAP_SET_BONDABLE,
+ BTP_OP_GAP_START_ADVERTISING,
+ BTP_OP_GAP_STOP_ADVERTISING,
+ BTP_OP_GAP_START_DISCOVERY,
+ BTP_OP_GAP_STOP_DISCOVERY,
+ BTP_OP_GAP_CONNECT,
+ BTP_OP_GAP_DISCONNECT,
+ BTP_OP_GAP_SET_IO_CAPA,
+ BTP_OP_GAP_PAIR,
+ BTP_OP_GAP_UNPAIR,
+ BTP_OP_GAP_PASSKEY_ENTRY_RSP,
+ BTP_OP_GAP_PASSKEY_CONFIRM_RSP,
+ };
+ uint8_t *commands = NULL;
+ size_t commands_len = 0;
+ size_t i;
if (index != BTP_INDEX_NON_CONTROLLER) {
btp_send_error(btp, BTP_GAP_SERVICE, index,
@@ -107,28 +130,22 @@ static void btp_gap_read_commands(uint8_t index, const void *param,
return;
}
- commands |= (1 << BTP_OP_GAP_READ_SUPPORTED_COMMANDS);
- commands |= (1 << BTP_OP_GAP_READ_CONTROLLER_INDEX_LIST);
- commands |= (1 << BTP_OP_GAP_READ_CONTROLLER_INFO);
- commands |= (1 << BTP_OP_GAP_RESET);
- commands |= (1 << BTP_OP_GAP_SET_POWERED);
- commands |= (1 << BTP_OP_GAP_SET_CONNECTABLE);
- commands |= (1 << BTP_OP_GAP_SET_DISCOVERABLE);
- commands |= (1 << BTP_OP_GAP_SET_BONDABLE);
- commands |= (1 << BTP_OP_GAP_START_ADVERTISING);
- commands |= (1 << BTP_OP_GAP_STOP_ADVERTISING);
- commands |= (1 << BTP_OP_GAP_START_DISCOVERY);
- commands |= (1 << BTP_OP_GAP_STOP_DISCOVERY);
- commands |= (1 << BTP_OP_GAP_CONNECT);
- commands |= (1 << BTP_OP_GAP_DISCONNECT);
- commands |= (1 << BTP_OP_GAP_SET_IO_CAPA);
- commands |= (1 << BTP_OP_GAP_PAIR);
- commands |= (1 << BTP_OP_GAP_UNPAIR);
-
- commands = L_CPU_TO_LE16(commands);
+ for (i = 0; i < L_ARRAY_SIZE(supported_commands); i++) {
+ if (!add_supported_command(&commands, &commands_len,
+ supported_commands[i]))
+ goto failed;
+ }
btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_READ_SUPPORTED_COMMANDS,
- BTP_INDEX_NON_CONTROLLER, sizeof(commands), &commands);
+ BTP_INDEX_NON_CONTROLLER, commands_len, commands);
+
+ l_free(commands);
+
+ return;
+
+failed:
+ l_free(commands);
+ btp_send_error(btp, BTP_GAP_SERVICE, index, BTP_ERROR_FAIL);
}
static void btp_gap_read_controller_index(uint8_t index, const void *param,
diff --git a/client/btpclient/gatt.c b/client/btpclient/gatt.c
index 9d847141c..d384b9c74 100644
--- a/client/btpclient/gatt.c
+++ b/client/btpclient/gatt.c
@@ -44,7 +44,19 @@ static int create_uuid(bt_uuid_t *btuuid, uint8_t len, const uint8_t *data)
static void btp_gatt_read_commands(uint8_t index, const void *param,
uint16_t length, void *user_data)
{
- uint16_t commands = 0;
+ const uint8_t supported_commands[] = {
+ BTP_OP_GATT_READ_SUPPORTED_COMMANDS,
+ BTP_OP_GATT_DISC_PRIM_UUID,
+ BTP_OP_GATT_DISC_CHRC_UUID,
+ BTP_OP_GATT_DISC_ALL_DESC,
+ BTP_OP_GATT_READ,
+ BTP_OP_GATT_READ_UUID,
+ BTP_OP_GATT_WRITE_WITHOUT_RSP,
+ BTP_OP_GATT_WRITE,
+ };
+ uint8_t *commands = NULL;
+ size_t commands_len = 0;
+ size_t i;
if (index != BTP_INDEX_NON_CONTROLLER) {
btp_send_error(btp, BTP_GATT_SERVICE, index,
@@ -52,19 +64,22 @@ static void btp_gatt_read_commands(uint8_t index, const void *param,
return;
}
- commands |= (1 << BTP_OP_GATT_READ_SUPPORTED_COMMANDS);
- commands |= (1 << BTP_OP_GATT_DISC_PRIM_UUID);
- commands |= (1 << BTP_OP_GATT_DISC_CHRC_UUID);
- commands |= (1 << BTP_OP_GATT_DISC_ALL_DESC);
- commands |= (1 << BTP_OP_GATT_READ);
- commands |= (1 << BTP_OP_GATT_READ_UUID);
- commands |= (1 << BTP_OP_GATT_WRITE_WITHOUT_RSP);
- commands |= (1 << BTP_OP_GATT_WRITE);
-
- commands = L_CPU_TO_LE16(commands);
+ for (i = 0; i < L_ARRAY_SIZE(supported_commands); i++) {
+ if (!add_supported_command(&commands, &commands_len,
+ supported_commands[i]))
+ goto failed;
+ }
btp_send(btp, BTP_GATT_SERVICE, BTP_OP_GATT_READ_SUPPORTED_COMMANDS,
- BTP_INDEX_NON_CONTROLLER, sizeof(commands), &commands);
+ BTP_INDEX_NON_CONTROLLER, commands_len, commands);
+
+ l_free(commands);
+
+ return;
+
+failed:
+ l_free(commands);
+ btp_send_error(btp, BTP_GATT_SERVICE, index, BTP_ERROR_FAIL);
}
static bool match_attribute_uuid(const void *attr, const void *uuid)
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH BlueZ 3/3] client/btpclient: Add BTP_OP_GAP_SET_EXTENDED_ADVERTISING support
2026-05-19 10:55 [PATCH BlueZ 0/3] client/btpclient: Add GAP extended advertising support Frédéric Danis
2026-05-19 10:55 ` [PATCH BlueZ 1/3] client/btpclient: refactor read-commands bitmap building Frédéric Danis
2026-05-19 10:55 ` [PATCH BlueZ 2/3] client/btpclient: Replace advertising defines by shared ones Frédéric Danis
@ 2026-05-19 10:55 ` Frédéric Danis
2026-05-21 13:30 ` [PATCH BlueZ 0/3] client/btpclient: Add GAP extended advertising support patchwork-bot+bluetooth
3 siblings, 0 replies; 6+ messages in thread
From: Frédéric Danis @ 2026-05-19 10:55 UTC (permalink / raw)
To: linux-bluetooth
This set LEAdvertisement1's SecondaryChannel property to "2M" to
force Extended advertisement type.
---
client/btpclient/gap.c | 68 ++++++++++++++++++++++++++++++++++++++++--
src/shared/btp.h | 7 +++++
2 files changed, 73 insertions(+), 2 deletions(-)
diff --git a/client/btpclient/gap.c b/client/btpclient/gap.c
index 2d393dd4d..7c7dad966 100644
--- a/client/btpclient/gap.c
+++ b/client/btpclient/gap.c
@@ -73,7 +73,7 @@ static char *dupuuid2str(const uint8_t *uuid, uint8_t len)
{
switch (len) {
case 16:
- return l_strdup_printf("%hhx%hhx", uuid[0], uuid[1]);
+ return l_strdup_printf("%hhx%hhx", uuid[1], uuid[0]);
case 128:
return l_strdup_printf("%hhx%hhx%hhx%hhx%hhx%hhx%hhx%hhx%hhx"
"%hhx%hhx%hhx%hhx%hhx%hhx%hhx", uuid[0],
@@ -109,6 +109,7 @@ static void btp_gap_read_commands(uint8_t index, const void *param,
BTP_OP_GAP_UNPAIR,
BTP_OP_GAP_PASSKEY_ENTRY_RSP,
BTP_OP_GAP_PASSKEY_CONFIRM_RSP,
+ BTP_OP_GAP_SET_EXTENDED_ADVERTISING,
};
uint8_t *commands = NULL;
size_t commands_len = 0;
@@ -813,6 +814,22 @@ static bool ad_timeout_getter(struct l_dbus *dbus,
return true;
}
+static bool ad_secondarychannel_getter(struct l_dbus *dbus,
+ struct l_dbus_message *message,
+ struct l_dbus_message_builder *builder,
+ void *user_data)
+{
+ struct btp_adapter *adapter = user_data;
+ uint32_t settings = adapter->current_settings;
+
+ if (!(settings & BTP_GAP_SETTINGS_EXTENDED_ADVERTISING))
+ return false;
+
+ l_dbus_message_builder_append_basic(builder, 's', "2M");
+
+ return true;
+}
+
static void setup_ad_interface(struct l_dbus_interface *interface)
{
l_dbus_interface_method(interface, "Release",
@@ -839,6 +856,8 @@ static void setup_ad_interface(struct l_dbus_interface *interface)
ad_duration_getter, NULL);
l_dbus_interface_property(interface, "Timeout", 0, "q",
ad_timeout_getter, NULL);
+ l_dbus_interface_property(interface, "SecondaryChannel", 0, "s",
+ ad_secondarychannel_getter, NULL);
}
static void start_advertising_reply(struct l_dbus_proxy *proxy,
@@ -889,6 +908,9 @@ static void create_advertising_data(uint8_t adv_data_len, const uint8_t *data)
ad_data = &data[adv_data_len - remaining_data_len + 2];
switch (ad_type) {
+ case BT_AD_FLAGS:
+ /* Advertisement flags are set by bluetoothd */
+ break;
case BT_AD_UUID16_SOME:
{
char *uuid = dupuuid2str(ad_data, 16);
@@ -897,6 +919,14 @@ static void create_advertising_data(uint8_t adv_data_len, const uint8_t *data)
break;
}
+ case BT_AD_UUID16_ALL:
+ {
+ char *uuid = dupuuid2str(ad_data, 16);
+
+ l_queue_push_tail(ad.uuids, uuid);
+
+ break;
+ }
case BT_AD_NAME_SHORT:
case BT_AD_NAME_COMPLETE:
ad.local_name = malloc(ad_len + 1);
@@ -1008,7 +1038,7 @@ static void btp_gap_start_advertising(uint8_t index, const void *param,
goto failed;
}
- if (!l_dbus_object_add_interface(dbus, AD_PATH, AD_IFACE, NULL)) {
+ if (!l_dbus_object_add_interface(dbus, AD_PATH, AD_IFACE, adapter)) {
l_info("Unable to instantiate ad interface");
if (!l_dbus_unregister_interface(dbus, AD_IFACE))
@@ -2380,6 +2410,36 @@ failed:
btp_send_error(btp, BTP_GAP_SERVICE, index, status);
}
+
+static void btp_gap_set_extended_advertising(uint8_t index, const void *param,
+ uint16_t length, void *user_data)
+{
+ struct btp_adapter *adapter = find_adapter_by_index(index);
+ const struct btp_gap_set_extended_advertising_cp *cp = param;
+ uint32_t new_settings;
+ uint8_t status = BTP_ERROR_FAIL;
+
+ if (!adapter) {
+ status = BTP_ERROR_INVALID_INDEX;
+ goto failed;
+ }
+
+ new_settings = adapter->current_settings;
+ if (cp->settings)
+ new_settings |= BTP_GAP_SETTINGS_EXTENDED_ADVERTISING;
+ else
+ new_settings &= ~BTP_GAP_SETTINGS_EXTENDED_ADVERTISING;
+ update_current_settings(adapter, new_settings);
+
+ btp_send(btp, BTP_GAP_SERVICE, BTP_OP_GAP_SET_EXTENDED_ADVERTISING,
+ adapter->index, sizeof(new_settings),
+ &new_settings);
+
+ return;
+failed:
+ btp_send_error(btp, BTP_GAP_SERVICE, index, status);
+}
+
static void btp_gap_device_found_ev(struct l_dbus_proxy *proxy)
{
struct btp_device *device = find_device_by_proxy(proxy);
@@ -2632,6 +2692,10 @@ static void register_gap_service(void)
btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_PASSKEY_CONFIRM_RSP,
btp_gap_confirm_entry_rsp, NULL, NULL);
+
+ btp_register(btp, BTP_GAP_SERVICE, BTP_OP_GAP_SET_EXTENDED_ADVERTISING,
+ btp_gap_set_extended_advertising,
+ NULL, NULL);
}
void gap_proxy_added(struct l_dbus_proxy *proxy, void *user_data)
diff --git a/src/shared/btp.h b/src/shared/btp.h
index b99ad3a28..098c0c39f 100644
--- a/src/shared/btp.h
+++ b/src/shared/btp.h
@@ -82,6 +82,8 @@ struct btp_gap_read_index_rp {
#define BTP_GAP_SETTING_PRIVACY 0x00002000
#define BTP_GAP_SETTING_CONTROLLER_CONF 0x00004000
#define BTP_GAP_SETTING_STATIC_ADDRESS 0x00008000
+#define BTP_GAP_SETTINGS_SC_ONLY 0x00010000
+#define BTP_GAP_SETTINGS_EXTENDED_ADVERTISING 0x00020000
#define BTP_OP_GAP_READ_CONTROLLER_INFO 0x03
struct btp_gap_read_info_rp {
@@ -224,6 +226,11 @@ struct btp_gap_passkey_confirm_rsp_cp {
uint8_t match;
} __packed;
+#define BTP_OP_GAP_SET_EXTENDED_ADVERTISING 0x21
+struct btp_gap_set_extended_advertising_cp {
+ uint8_t settings;
+} __packed;
+
#define BTP_EV_GAP_NEW_SETTINGS 0x80
struct btp_new_settings_ev {
uint32_t current_settings;
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread