From: Roland Schober <rolandschober1968@gmail.com>
To: linux-bluetooth@vger.kernel.org
Cc: luiz.dentz@gmail.com, Roland Schober <rolandschober1968@gmail.com>
Subject: [RFC PATCH BlueZ v2 8/9] emulator/btdev: Add PAwR advertiser
Date: Wed, 2 Dec 2026 13:52:40 +0100 [thread overview]
Message-ID: <20261202125241.348452-9-rolandschober1968@gmail.com> (raw)
In-Reply-To: <20261202125241.348452-1-rolandschober1968@gmail.com>
Add the advertiser side of Periodic Advertising with Responses to
controllers of version 6.0 and later:
- the PAwR Advertiser feature;
- LE Set Periodic Advertising Parameters [v2], which checks the
subevent parameters against each other and against the interval;
- LE Periodic Advertising Subevent Data Request, sent once per
periodic advertising interval for all subevents while the train is
enabled;
- LE Set Periodic Advertising Subevent Data, which takes the data of
a subevent once per request. Data which was not asked for is
refused with Command Disallowed: before the first request, a second
time for one subevent, and for a subevent the train does not have.
LE Set Periodic Advertising Data is refused for a set with subevents.
Nothing is transmitted and no responses are reported, so the command
never answers Too Late or Too Early.
Assisted-by: Claude:claude-fable-5-2
---
emulator/btdev.c | 292 ++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 291 insertions(+), 1 deletion(-)
diff --git a/emulator/btdev.c b/emulator/btdev.c
index c4ade031e..baec3e996 100644
--- a/emulator/btdev.c
+++ b/emulator/btdev.c
@@ -267,6 +267,11 @@ struct btdev {
uint8_t le_pa_data_len;
uint8_t le_pa_data[MAX_PA_DATA_LEN];
uint8_t le_pa_handle;
+ uint8_t le_pa_num_subevents;
+ uint8_t le_pa_num_rsp_slots;
+ bool le_pa_data_requested;
+ uint8_t le_pa_subevents[16]; /* Subevents which wait for data */
+ unsigned int le_pa_subevent_id;
uint8_t le_ltk[16];
uint8_t hdt_def_mic_len;
uint8_t hdt_def_format;
@@ -694,7 +699,13 @@ static void btdev_reset(struct btdev *btdev)
btdev->inquiry_id = 0;
}
+ if (btdev->le_pa_subevent_id > 0) {
+ timeout_remove(btdev->le_pa_subevent_id);
+ btdev->le_pa_subevent_id = 0;
+ }
+
btdev->le_pa_enable = 0x00;
+ btdev->le_pa_num_subevents = 0;
queue_remove_all(btdev->conns, NULL, NULL, conn_remove);
queue_remove_all(btdev->le_ext_adv, NULL, NULL, le_ext_adv_free);
@@ -5995,6 +6006,10 @@ static int cmd_remove_adv_set(struct btdev *dev, const void *data,
queue_remove(dev->le_ext_adv, ext_adv);
free(ext_adv);
+ /* The subevents go with their set */
+ if (dev->le_pa_handle == cmd->handle)
+ dev->le_pa_num_subevents = 0;
+
cmd_complete(dev, BT_HCI_CMD_LE_REMOVE_ADV_SET, &status,
sizeof(status));
@@ -6027,6 +6042,7 @@ static int cmd_clear_adv_sets(struct btdev *dev, const void *data,
}
queue_remove_all(dev->le_ext_adv, NULL, NULL, le_ext_adv_free);
+ dev->le_pa_num_subevents = 0;
cmd_complete(dev, BT_HCI_CMD_LE_CLEAR_ADV_SETS, &status,
sizeof(status));
@@ -6048,6 +6064,7 @@ static int cmd_set_pa_params(struct btdev *dev, const void *data,
dev->le_pa_min_interval = cmd->min_interval;
dev->le_pa_max_interval = cmd->max_interval;
dev->le_pa_handle = cmd->handle;
+ dev->le_pa_num_subevents = 0;
}
cmd_complete(dev, BT_HCI_CMD_LE_SET_PA_PARAMS, &status,
@@ -6055,6 +6072,236 @@ static int cmd_set_pa_params(struct btdev *dev, const void *data,
return 0;
}
+/*
+ * The intervals and the response slot delay are in units of 1.25 ms, the
+ * response slot spacing in units of 0.125 ms.
+ */
+static bool pa_params_v2_valid(const struct bt_hci_cmd_le_set_pa_params_v2 *cmd)
+{
+ uint16_t min_interval = le16_to_cpu(cmd->min_interval);
+ uint16_t max_interval = le16_to_cpu(cmd->max_interval);
+ uint16_t subevent_interval = cmd->subevent_interval;
+
+ if (min_interval < 0x0006 || min_interval > max_interval ||
+ cmd->num_subevents > 0x80)
+ return false;
+
+ /* Without subevents the other parameters are ignored */
+ if (!cmd->num_subevents)
+ return true;
+
+ /* A single subevent has the periodic advertising interval */
+ if (cmd->num_subevents == 1)
+ subevent_interval = max_interval;
+ else if (subevent_interval < 0x06 ||
+ cmd->num_subevents * subevent_interval > min_interval)
+ return false;
+
+ if (!cmd->num_response_slots)
+ return true;
+
+ /* The response slots follow the packet and end with the subevent */
+ if (!cmd->response_slot_delay ||
+ cmd->response_slot_delay >= subevent_interval)
+ return false;
+
+ if (cmd->num_response_slots > 1 &&
+ (cmd->response_slot_spacing < 0x02 ||
+ cmd->response_slot_spacing * cmd->num_response_slots >
+ 10 * (subevent_interval - cmd->response_slot_delay)))
+ return false;
+
+ return true;
+}
+
+static int cmd_set_pa_params_v2(struct btdev *dev, const void *data,
+ uint8_t len)
+{
+ const struct bt_hci_cmd_le_set_pa_params_v2 *cmd = data;
+ struct bt_hci_rsp_le_set_pa_params_v2 rsp;
+
+ memset(&rsp, 0, sizeof(rsp));
+ rsp.handle = cmd->handle;
+
+ if (!queue_find(dev->le_ext_adv, match_ext_adv_handle,
+ UINT_TO_PTR(cmd->handle))) {
+ rsp.status = BT_HCI_ERR_UNKNOWN_ADVERTISING_ID;
+ goto done;
+ }
+
+ if (dev->le_pa_enable) {
+ rsp.status = BT_HCI_ERR_COMMAND_DISALLOWED;
+ goto done;
+ }
+
+ if (!pa_params_v2_valid(cmd)) {
+ rsp.status = BT_HCI_ERR_INVALID_PARAMETERS;
+ goto done;
+ }
+
+ dev->le_pa_properties = le16_to_cpu(cmd->properties);
+ dev->le_pa_min_interval = cmd->min_interval;
+ dev->le_pa_max_interval = cmd->max_interval;
+ dev->le_pa_handle = cmd->handle;
+ dev->le_pa_num_subevents = cmd->num_subevents;
+ dev->le_pa_num_rsp_slots = cmd->num_response_slots;
+
+done:
+ cmd_complete(dev, BT_HCI_CMD_LE_SET_PA_PARAMS_V2, &rsp, sizeof(rsp));
+
+ return 0;
+}
+
+static int cmd_set_pa_subevent_data(struct btdev *dev, const void *data,
+ uint8_t len)
+{
+ struct iovec iov = { .iov_base = (void *) data, .iov_len = len };
+ const struct bt_hci_cmd_le_set_pa_subevent_data *cmd;
+ const struct bt_hci_le_pa_subevent_data *se;
+ struct bt_hci_rsp_le_set_pa_subevent_data rsp;
+ uint8_t seen[sizeof(dev->le_pa_subevents)] = {};
+ uint8_t i;
+
+ memset(&rsp, 0, sizeof(rsp));
+
+ cmd = util_iov_pull_mem(&iov, sizeof(*cmd));
+ if (!cmd)
+ return -EINVAL;
+
+ rsp.handle = cmd->handle;
+
+ if (!queue_find(dev->le_ext_adv, match_ext_adv_handle,
+ UINT_TO_PTR(cmd->handle))) {
+ rsp.status = BT_HCI_ERR_UNKNOWN_ADVERTISING_ID;
+ goto done;
+ }
+
+ /* Only a running train with subevents asks for data */
+ if (!dev->le_pa_enable || dev->le_pa_handle != cmd->handle ||
+ !dev->le_pa_num_subevents) {
+ rsp.status = BT_HCI_ERR_COMMAND_DISALLOWED;
+ goto done;
+ }
+
+ if (!cmd->num_subevents || cmd->num_subevents > 0x0f) {
+ rsp.status = BT_HCI_ERR_INVALID_PARAMETERS;
+ goto done;
+ }
+
+ for (i = 0; i < cmd->num_subevents; i++) {
+ se = util_iov_pull_mem(&iov, sizeof(*se));
+ if (!se || !util_iov_pull(&iov, se->data_len)) {
+ rsp.status = BT_HCI_ERR_INVALID_PARAMETERS;
+ goto done;
+ }
+
+ /* Outside of the subevents which are asked for */
+ if (se->subevent >= dev->le_pa_num_subevents) {
+ rsp.status = BT_HCI_ERR_COMMAND_DISALLOWED;
+ goto done;
+ }
+
+ if (se->response_slot_start + se->response_slot_count >
+ dev->le_pa_num_rsp_slots) {
+ rsp.status = BT_HCI_ERR_INVALID_PARAMETERS;
+ goto done;
+ }
+
+ /* A subevent is named once */
+ if (seen[se->subevent / 8] & BIT(se->subevent % 8)) {
+ rsp.status = BT_HCI_ERR_INVALID_PARAMETERS;
+ goto done;
+ }
+
+ seen[se->subevent / 8] |= BIT(se->subevent % 8);
+ }
+
+ if (iov.iov_len) {
+ rsp.status = BT_HCI_ERR_INVALID_PARAMETERS;
+ goto done;
+ }
+
+ /* Data is taken once for each subevent asked for: not before the
+ * first request, and not a second time in one periodic advertising
+ * event.
+ */
+ iov.iov_base = (void *) cmd->subevents;
+ iov.iov_len = len - sizeof(*cmd);
+
+ for (i = 0; i < cmd->num_subevents; i++) {
+ se = util_iov_pull_mem(&iov, sizeof(*se));
+ util_iov_pull(&iov, se->data_len);
+
+ if (!dev->le_pa_data_requested ||
+ !(dev->le_pa_subevents[se->subevent / 8] &
+ BIT(se->subevent % 8))) {
+ rsp.status = BT_HCI_ERR_COMMAND_DISALLOWED;
+ goto done;
+ }
+ }
+
+ iov.iov_base = (void *) cmd->subevents;
+ iov.iov_len = len - sizeof(*cmd);
+
+ for (i = 0; i < cmd->num_subevents; i++) {
+ se = util_iov_pull_mem(&iov, sizeof(*se));
+ util_iov_pull(&iov, se->data_len);
+
+ dev->le_pa_subevents[se->subevent / 8] &=
+ ~BIT(se->subevent % 8);
+ }
+
+done:
+ cmd_complete(dev, BT_HCI_CMD_LE_SET_PA_SUBEVENT_DATA, &rsp,
+ sizeof(rsp));
+
+ return 0;
+}
+
+/* Ask the host for the data of all subevents, once per periodic advertising
+ * interval.
+ */
+static bool pa_subevent_data_request(void *user_data)
+{
+ struct btdev *dev = user_data;
+ struct bt_hci_evt_le_pa_subevent_data_request ev;
+ uint8_t start = 0;
+
+ if (!dev->le_pa_enable || !dev->le_pa_num_subevents) {
+ dev->le_pa_subevent_id = 0;
+ return false;
+ }
+
+ /* LE Periodic Advertising Subevent Data Request event */
+ if (!(dev->le_event_mask[4] & 0x40))
+ return true;
+
+ /* Ask for no more than one command can answer, 15 subevents */
+ while (start < dev->le_pa_num_subevents) {
+ uint8_t count = dev->le_pa_num_subevents - start;
+ uint8_t i;
+
+ if (count > 0x0f)
+ count = 0x0f;
+
+ for (i = start; i < start + count; i++)
+ dev->le_pa_subevents[i / 8] |= BIT(i % 8);
+
+ dev->le_pa_data_requested = true;
+
+ ev.handle = dev->le_pa_handle;
+ ev.subevent_start = start;
+ ev.subevent_data_count = count;
+
+ le_meta_event(dev, BT_HCI_EVT_LE_PA_SUBEVENT_DATA_REQUEST,
+ &ev, sizeof(ev));
+
+ start += count;
+ }
+
+ return true;
+}
+
static int cmd_set_pa_data(struct btdev *dev, const void *data,
uint8_t len)
{
@@ -6062,6 +6309,14 @@ static int cmd_set_pa_data(struct btdev *dev, const void *data,
uint8_t status = BT_HCI_ERR_SUCCESS;
uint8_t data_len = cmd->data_len;
+ /* A train with subevents carries its data in the subevents */
+ if (dev->le_pa_num_subevents && dev->le_pa_handle == cmd->handle) {
+ status = BT_HCI_ERR_COMMAND_DISALLOWED;
+ cmd_complete(dev, BT_HCI_CMD_LE_SET_PA_DATA, &status,
+ sizeof(status));
+ return 0;
+ }
+
if (data_len > MAX_PA_DATA_LEN)
data_len = MAX_PA_DATA_LEN;
@@ -6268,6 +6523,28 @@ static bool match_remote(const void *data, const void *match_data)
return pa->remote == remote;
}
+/* Start, or stop, asking the host for subevent data */
+static void pa_subevent_restart(struct btdev *dev)
+{
+ if (dev->le_pa_subevent_id > 0) {
+ timeout_remove(dev->le_pa_subevent_id);
+ dev->le_pa_subevent_id = 0;
+ }
+
+ dev->le_pa_data_requested = false;
+ memset(dev->le_pa_subevents, 0, sizeof(dev->le_pa_subevents));
+
+ if (dev->le_pa_enable && dev->le_pa_num_subevents) {
+ /* The interval is in units of 1.25 ms */
+ unsigned int msec = le16_to_cpu(dev->le_pa_min_interval) *
+ 5 / 4;
+
+ dev->le_pa_subevent_id = timeout_add(msec,
+ pa_subevent_data_request,
+ dev, NULL);
+ }
+}
+
static int cmd_set_pa_enable(struct btdev *dev, const void *data, uint8_t len)
{
const struct bt_hci_cmd_le_set_pa_enable *cmd = data;
@@ -6284,6 +6561,10 @@ static int cmd_set_pa_enable(struct btdev *dev, const void *data, uint8_t len)
cmd_complete(dev, BT_HCI_CMD_LE_SET_PA_ENABLE, &status,
sizeof(status));
+ /* A command which is refused leaves the train as it is */
+ if (status == BT_HCI_ERR_SUCCESS)
+ pa_subevent_restart(dev);
+
for (i = 0; i < MAX_BTDEV_ENTRIES; i++) {
struct btdev *remote = btdev_list[i];
@@ -8362,7 +8643,10 @@ done:
CMD(BT_HCI_CMD_LE_READ_ALL_REMOTE_FEATURES, \
cmd_le_read_all_remote_features, NULL), \
CMD(BT_HCI_CMD_LE_SET_HOST_FEATURE_V2, \
- cmd_set_host_feature_v2, NULL)
+ cmd_set_host_feature_v2, NULL), \
+ CMD(BT_HCI_CMD_LE_SET_PA_PARAMS_V2, cmd_set_pa_params_v2, NULL), \
+ CMD(BT_HCI_CMD_LE_SET_PA_SUBEVENT_DATA, cmd_set_pa_subevent_data, \
+ NULL)
static const struct btdev_cmd cmd_le_6_0[] = {
CMD_COMMON_ALL,
@@ -8382,6 +8666,8 @@ static void set_le_60_commands(struct btdev *btdev)
btdev->commands[47] |= BIT(2); /* LE Read All Local Features */
btdev->commands[47] |= BIT(3); /* LE Read All Remote Features */
btdev->commands[47] |= BIT(4); /* LE Set Host Feature V2 */
+ btdev->commands[46] |= BIT(5); /* LE Set PA Subevent Data */
+ btdev->commands[47] |= BIT(1); /* LE Set PA Parameters v2 */
btdev->cmds = cmd_le_6_0;
}
@@ -9159,6 +9445,7 @@ static void set_bredrle_features(struct btdev *btdev)
if (btdev->type >= BTDEV_TYPE_BREDRLE60) {
btdev->version = 0x0e;
+ btdev->le_features[5] |= BIT(3); /* PAwR Advertiser */
btdev->le_features[7] |= BIT(7); /* LL Extended Features */
}
@@ -9381,6 +9668,9 @@ void btdev_destroy(struct btdev *btdev)
if (btdev->inquiry_id > 0)
timeout_remove(btdev->inquiry_id);
+ if (btdev->le_pa_subevent_id > 0)
+ timeout_remove(btdev->le_pa_subevent_id);
+
bt_crypto_unref(btdev->crypto);
del_btdev(btdev);
--
2.47.3
next prev parent reply other threads:[~2026-12-02 12:53 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 12:52 [RFC PATCH BlueZ v2 0/9] Periodic advertising and PAwR over MGMT Roland Schober
2026-12-02 12:52 ` [RFC PATCH BlueZ v2 1/9] doc/mgmt: Add PA and PAwR commands and events Roland Schober
2026-12-02 12:52 ` [RFC PATCH BlueZ v2 2/9] lib/mgmt: Add periodic advertising and PAwR definitions Roland Schober
2026-12-02 12:52 ` [RFC PATCH BlueZ v2 3/9] monitor: Decode PA and PAwR MGMT commands and events Roland Schober
2026-12-02 12:52 ` [RFC PATCH BlueZ v2 4/9] client/mgmt: Fix length of byte arrays with empty entries Roland Schober
2026-12-02 12:52 ` [RFC PATCH BlueZ v2 5/9] client/mgmt: Add pa-params, pa-data and pawr-serve commands Roland Schober
2026-12-02 12:52 ` [RFC PATCH BlueZ v2 6/9] monitor: Add PAwR HCI definitions Roland Schober
2026-12-02 12:52 ` [RFC PATCH BlueZ v2 7/9] emulator/btdev: Keep advertising sets with PA enabled Roland Schober
2026-12-02 12:52 ` Roland Schober [this message]
2026-12-02 12:52 ` [RFC PATCH BlueZ v2 9/9] mgmt-tester: Add periodic advertising and PAwR tests Roland Schober
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=20261202125241.348452-9-rolandschober1968@gmail.com \
--to=rolandschober1968@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
--cc=luiz.dentz@gmail.com \
/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