Linux bluetooth development
 help / color / mirror / Atom feed
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 5/9] client/mgmt: Add pa-params, pa-data and pawr-serve commands
Date: Wed,  2 Dec 2026 13:52:37 +0100	[thread overview]
Message-ID: <20261202125241.348452-6-rolandschober1968@gmail.com> (raw)
In-Reply-To: <20261202125241.348452-1-rolandschober1968@gmail.com>

pa-params adds periodic advertising to an instance created with
add-ext-adv-params, with the parameters of Periodic Advertising with
Responses if subevents are given. pa-data sets the data of a periodic
advertising train.

pawr-serve answers the subevent data requests of an instance, with
the data given or with manufacturer data which carries the subevent
and a counter, and prints the response reports. Data which does not
fit into one command is sent in several. It runs until it is stopped
or replaced by another pawr-serve.

Assisted-by: Claude:claude-fable-5-2
---
 client/mgmt.c             | 367 ++++++++++++++++++++++++++++++++++++++
 doc/bluetoothctl-mgmt.rst |  51 ++++++
 2 files changed, 418 insertions(+)

diff --git a/client/mgmt.c b/client/mgmt.c
index bdf1bbfec..e115654b3 100644
--- a/client/mgmt.c
+++ b/client/mgmt.c
@@ -2504,6 +2504,359 @@ static void cmd_hci_cmd(int argc, char **argv)
 	}
 }
 
+static void pa_params_rsp(uint8_t status, uint16_t len, const void *param,
+							void *user_data)
+{
+	const struct mgmt_rp_add_periodic_adv_params *rp = param;
+
+	if (status != 0) {
+		error("Add Periodic Advertising Parameters failed with status "
+				"0x%02x (%s)", status, mgmt_errstr(status));
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	if (len < sizeof(*rp)) {
+		error("Too small (%u bytes) reply", len);
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	print("Periodic advertising set on instance %u", rp->instance);
+
+	bt_shell_noninteractive_quit(EXIT_SUCCESS);
+}
+
+/* A number for a field of a command: all of the argument, in its range */
+static bool parse_num(const char *str, unsigned long max, unsigned long *val)
+{
+	char *endptr = NULL;
+
+	*val = strtoul(str, &endptr, 0);
+	if (!endptr || endptr == str || *endptr != '\0' || *val > max) {
+		error("Invalid value %s (0-%lu)", str, max);
+		return false;
+	}
+
+	return true;
+}
+
+/* The data of a command: the octets of all the arguments which are left */
+static bool parse_data(int argc, char **argv, uint8_t *data, long size,
+								long *len)
+{
+	int i;
+
+	*len = 0;
+
+	for (i = 0; i < argc; i++) {
+		long n = size - *len;
+
+		if (!str2bytearray(argv[i], data + *len, &n))
+			return false;
+
+		*len += n;
+	}
+
+	return true;
+}
+
+static void cmd_pa_params(int argc, char **argv)
+{
+	struct mgmt_cp_add_periodic_adv_params cp;
+	unsigned long val[8];
+	uint16_t index;
+	int i;
+
+	if (argc != 4 && argc != 9) {
+		error("Either none or all five PAwR parameters");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	/* The two intervals have 16 bits, the other parameters 8 */
+	for (i = 1; i < argc; i++) {
+		if (!parse_num(argv[i], i == 2 || i == 3 ? UINT16_MAX :
+						UINT8_MAX, &val[i - 1]))
+			return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	memset(&cp, 0, sizeof(cp));
+	cp.instance = val[0];
+	cp.min_interval = cpu_to_le16(val[1]);
+	cp.max_interval = cpu_to_le16(val[2]);
+
+	if (argc > 4) {
+		cp.num_subevents = val[3];
+		cp.subevent_interval = val[4];
+		cp.response_slot_delay = val[5];
+		cp.response_slot_spacing = val[6];
+		cp.num_response_slots = val[7];
+	}
+
+	index = mgmt_index;
+	if (index == MGMT_INDEX_NONE)
+		index = 0;
+
+	if (mgmt_send(mgmt, MGMT_OP_ADD_PERIODIC_ADV_PARAMS, index,
+			sizeof(cp), &cp, pa_params_rsp, NULL, NULL) == 0) {
+		error("Unable to send Add Periodic Advertising Parameters");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+}
+
+static void pa_data_rsp(uint8_t status, uint16_t len, const void *param,
+							void *user_data)
+{
+	const struct mgmt_rp_add_periodic_adv_data *rp = param;
+
+	if (status != 0) {
+		error("Add Periodic Advertising Data failed with status "
+				"0x%02x (%s)", status, mgmt_errstr(status));
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	if (len < sizeof(*rp)) {
+		error("Too small (%u bytes) reply", len);
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	print("Periodic advertising data set on instance %u", rp->instance);
+
+	bt_shell_noninteractive_quit(EXIT_SUCCESS);
+}
+
+static void cmd_pa_data(int argc, char **argv)
+{
+	uint8_t buf[sizeof(struct mgmt_cp_add_periodic_adv_data) + 252];
+	struct mgmt_cp_add_periodic_adv_data *cp = (void *) buf;
+	unsigned long instance;
+	long data_len;
+	uint16_t index;
+
+	memset(buf, 0, sizeof(buf));
+
+	if (!parse_num(argv[1], UINT8_MAX, &instance) ||
+			!parse_data(argc - 2, argv + 2, cp->data, 252,
+								&data_len))
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+
+	cp->instance = instance;
+	cp->data_len = data_len;
+
+	index = mgmt_index;
+	if (index == MGMT_INDEX_NONE)
+		index = 0;
+
+	if (mgmt_send(mgmt, MGMT_OP_ADD_PERIODIC_ADV_DATA, index,
+			sizeof(*cp) + data_len, cp, pa_data_rsp, NULL,
+			NULL) == 0) {
+		error("Unable to send Add Periodic Advertising Data");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+}
+
+/*
+ * One command has room for 255 octets of parameters and for 15 subevents,
+ * like the HCI command the kernel makes of it.
+ */
+#define PAWR_CMD_SIZE		255
+#define PAWR_CMD_SUBEVENTS	15
+#define PAWR_DATA_SIZE		(PAWR_CMD_SIZE - \
+				sizeof(struct mgmt_cp_set_pa_subevent_data) - \
+				sizeof(struct mgmt_pa_subevent_data))
+
+/* What pawr-serve answers the subevent data requests with */
+static struct {
+	uint8_t instance;
+	uint8_t num_subevents;
+	uint8_t num_response_slots;
+	bool has_data;
+	uint8_t data[PAWR_DATA_SIZE];
+	long data_len;
+	uint8_t counter;
+	unsigned int request_id;
+	unsigned int report_id;
+} pawr;
+
+static void pawr_data_rsp(uint8_t status, uint16_t len, const void *param,
+							void *user_data)
+{
+	if (status != 0)
+		error("Set Periodic Advertising Subevent Data failed with "
+			"status 0x%02x (%s)", status, mgmt_errstr(status));
+}
+
+/*
+ * Not mgmt_send(): it holds a command back until the one before it has its
+ * reply, and the commands which answer one request cannot wait that long.
+ */
+static void pawr_send_data(uint16_t index, const void *buf, size_t len)
+{
+	if (mgmt_send_nowait(mgmt, MGMT_OP_SET_PA_SUBEVENT_DATA, index, len,
+				buf, pawr_data_rsp, NULL, NULL) == 0)
+		error("Unable to send Set Periodic Advertising Subevent Data");
+}
+
+static void pawr_data_request(uint16_t index, uint16_t len, const void *param,
+							void *user_data)
+{
+	const struct mgmt_ev_pa_subevent_data_request *ev = param;
+	uint8_t buf[PAWR_CMD_SIZE];
+	struct mgmt_cp_set_pa_subevent_data *cp = (void *) buf;
+	size_t pos = sizeof(*cp);
+	uint8_t i;
+
+	if (len < sizeof(*ev)) {
+		error("Too small (%u bytes) pa_subevent_data_request event",
+									len);
+		return;
+	}
+
+	if (ev->instance != pawr.instance)
+		return;
+
+	print("hci%u pa_subevent_data_request: instance %u start %u count %u",
+			index, ev->instance, ev->subevent_start,
+			ev->subevent_data_count);
+
+	cp->instance = ev->instance;
+	cp->num_subevents = 0;
+
+	for (i = 0; i < ev->subevent_data_count; i++) {
+		struct mgmt_pa_subevent_data *subevent;
+		uint8_t data[sizeof(pawr.data)];
+		uint8_t data_len = pawr.data_len;
+		uint8_t nr = (ev->subevent_start + i) % pawr.num_subevents;
+
+		/* Without data from the command line: manufacturer data
+		 * with the subevent and a counter.
+		 */
+		if (pawr.has_data) {
+			memcpy(data, pawr.data, data_len);
+		} else {
+			data[0] = 0x05;
+			data[1] = 0xff;
+			data[2] = 0x59;
+			data[3] = 0x00;
+			data[4] = nr;
+			data[5] = pawr.counter++;
+			data_len = 6;
+		}
+
+		/* The command is full: send it and go on with a new one */
+		if (pos + sizeof(*subevent) + data_len > sizeof(buf) ||
+				cp->num_subevents == PAWR_CMD_SUBEVENTS) {
+			pawr_send_data(index, buf, pos);
+			pos = sizeof(*cp);
+			cp->num_subevents = 0;
+		}
+
+		subevent = (void *) (buf + pos);
+		subevent->subevent = nr;
+		subevent->response_slot_start = 0;
+		subevent->response_slot_count = pawr.num_response_slots;
+		subevent->data_len = data_len;
+		memcpy(subevent->data, data, data_len);
+
+		pos += sizeof(*subevent) + data_len;
+		cp->num_subevents++;
+	}
+
+	if (cp->num_subevents)
+		pawr_send_data(index, buf, pos);
+}
+
+static void pawr_response_report(uint16_t index, uint16_t len,
+					const void *param, void *user_data)
+{
+	const struct mgmt_ev_pa_response_report *ev = param;
+	const uint8_t *ptr;
+	uint8_t i;
+
+	if (len < sizeof(*ev)) {
+		error("Too small (%u bytes) pa_response_report event", len);
+		return;
+	}
+
+	if (ev->instance != pawr.instance)
+		return;
+
+	ptr = ev->responses;
+	len -= sizeof(*ev);
+
+	for (i = 0; i < ev->num_responses; i++) {
+		const struct mgmt_pa_response *rsp = (const void *) ptr;
+		char str[2 * UINT8_MAX + 1];
+		uint8_t j;
+
+		if (len < sizeof(*rsp) || len < sizeof(*rsp) + rsp->data_len) {
+			error("Truncated pa_response_report event");
+			return;
+		}
+
+		for (j = 0; j < rsp->data_len; j++)
+			sprintf(str + 2 * j, "%02x", rsp->data[j]);
+		str[2 * rsp->data_len] = '\0';
+
+		print("hci%u pa_response_report: instance %u subevent %u "
+			"tx_status %u slot %u rssi %d status 0x%02x data %s",
+			index, ev->instance, ev->subevent, ev->tx_status,
+			rsp->response_slot, rsp->rssi, rsp->data_status, str);
+
+		ptr += sizeof(*rsp) + rsp->data_len;
+		len -= sizeof(*rsp) + rsp->data_len;
+	}
+}
+
+/* Runs until it is stopped */
+static void cmd_pawr_serve(int argc, char **argv)
+{
+	unsigned long instance, num_subevents, num_response_slots;
+	uint8_t data[sizeof(pawr.data)];
+	long data_len;
+	uint16_t index;
+
+	if (!parse_num(argv[1], UINT8_MAX, &instance) ||
+			!parse_num(argv[2], 128, &num_subevents) ||
+			!parse_num(argv[3], UINT8_MAX, &num_response_slots) ||
+			!parse_data(argc - 4, argv + 4, data, sizeof(data),
+								&data_len))
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+
+	if (!num_subevents) {
+		error("Invalid number of subevents");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	/* A second pawr-serve replaces the first */
+	if (pawr.request_id)
+		mgmt_unregister(mgmt, pawr.request_id);
+
+	if (pawr.report_id)
+		mgmt_unregister(mgmt, pawr.report_id);
+
+	memset(&pawr, 0, sizeof(pawr));
+	pawr.instance = instance;
+	pawr.num_subevents = num_subevents;
+	pawr.num_response_slots = num_response_slots;
+	pawr.has_data = argc > 4;
+	pawr.data_len = data_len;
+	memcpy(pawr.data, data, data_len);
+
+	index = mgmt_index;
+	if (index == MGMT_INDEX_NONE)
+		index = 0;
+
+	pawr.request_id = mgmt_register(mgmt, MGMT_EV_PA_SUBEVENT_DATA_REQUEST,
+					index, pawr_data_request, NULL, NULL);
+	pawr.report_id = mgmt_register(mgmt, MGMT_EV_PA_RESPONSE_REPORT, index,
+					pawr_response_report, NULL, NULL);
+	if (!pawr.request_id || !pawr.report_id) {
+		error("Unable to register for the PAwR events");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	print("Serving subevent data of instance %u", pawr.instance);
+}
+
 /* Wrapper to get the index and opcode to the response callback */
 struct command_data {
 	uint16_t id;
@@ -6354,6 +6707,20 @@ static const struct bt_shell_menu mgmt_menu = {
 		cmd_conn_subrate,	"Load Connection Subrate"	},
 	{ "hci-cmd",		"<opcode> [event] [timeout] [param...]",
 		cmd_hci_cmd,	"Send HCI Command and wait for Event"	},
+	{ "pa-params",		"<instance> <min_interval> <max_interval> "
+				"[num_subevents] [subevent_interval] "
+				"[response_slot_delay] [response_slot_spacing] "
+				"[num_response_slots]",
+		cmd_pa_params,
+			"Add periodic advertising to an instance, with "
+			"responses if it has subevents"			},
+	{ "pa-data",		"<instance> [data...]",
+		cmd_pa_data,
+			"Set the periodic advertising data of an instance" },
+	{ "pawr-serve",		"<instance> <num_subevents> "
+				"<num_response_slots> [data...]",
+		cmd_pawr_serve,
+			"Answer subevent data requests, print responses" },
 	{} },
 };
 
diff --git a/doc/bluetoothctl-mgmt.rst b/doc/bluetoothctl-mgmt.rst
index 91406041c..8e27389ef 100644
--- a/doc/bluetoothctl-mgmt.rst
+++ b/doc/bluetoothctl-mgmt.rst
@@ -1314,6 +1314,57 @@ Load Connection Subrate
 :Example Load subrate for LE random address device:
 	| **> conn-subrate -t 2 AA:BB:CC:DD:EE:FF 16 1600 2 8 0 4 100**
 
+pa-params
+---------
+
+Add periodic advertising to an advertising instance. With the five optional
+parameters it is Periodic Advertising with Responses (PAwR).
+
+:Usage: **> pa-params <instance> <min_interval> <max_interval> [num_subevents]
+	[subevent_interval] [response_slot_delay] [response_slot_spacing]
+	[num_response_slots]**
+:<instance>: Advertising instance, added with add-ext-adv-params before
+:<min_interval>: Minimum periodic advertising interval (units of 1.25 ms)
+:<max_interval>: Maximum periodic advertising interval (units of 1.25 ms)
+:[num_subevents]: Number of subevents of a periodic advertising event
+:[subevent_interval]: Time between two subevents (units of 1.25 ms)
+:[response_slot_delay]: Time from the start of a subevent to its first
+	response slot (units of 1.25 ms)
+:[response_slot_spacing]: Time between two response slots (units of 0.125 ms)
+:[num_response_slots]: Number of response slots of a subevent
+:Example Add periodic advertising with an interval of 100 ms to instance 1:
+	| **> pa-params 1 80 80**
+:Example Add PAwR with 5 subevents of 60 ms and 5 response slots of 10 ms:
+	| **> pa-params 1 255 255 5 48 8 80 5**
+
+pa-data
+-------
+
+Set the periodic advertising data of an advertising instance.
+
+:Usage: **> pa-data <instance> [data...]**
+:<instance>: Advertising instance with periodic advertising
+:[data...]: Octets of the data, none for no data
+:Example Set a manufacturer specific AD structure as data of instance 1:
+	| **> pa-data 1 0x05 0xff 0x59 0x00 0x01 0x02**
+
+pawr-serve
+----------
+
+Answer the subevent data requests of an advertising instance with PAwR, and
+print the responses which are received. It runs until btmgmt is ended, or
+until another pawr-serve replaces it.
+
+:Usage: **> pawr-serve <instance> <num_subevents> <num_response_slots>
+	[data...]**
+:<instance>: Advertising instance with PAwR
+:<num_subevents>: Number of subevents of the instance (1 to 128)
+:<num_response_slots>: Number of response slots to listen to in a subevent
+:[data...]: Octets of the data of every subevent (up to 249). Without it, a
+	manufacturer specific AD structure with the subevent and a counter
+:Example Serve instance 1 with 5 subevents and 5 response slots:
+	| **> pawr-serve 1 5 5**
+
 RESOURCES
 =========
 
-- 
2.47.3


  parent reply	other threads:[~2026-12-02 12:52 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 ` Roland Schober [this message]
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 ` [RFC PATCH BlueZ v2 8/9] emulator/btdev: Add PAwR advertiser Roland Schober
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-6-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