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, marcel@holtmann.org,
	Roland Schober <rolandschober1968@gmail.com>
Subject: [RFC PATCH 3/5] Bluetooth: MGMT: Add Add Periodic Advertising Parameters command
Date: Fri, 27 Nov 2026 09:53:58 +0100	[thread overview]
Message-ID: <20261127085401.263615-4-rolandschober1968@gmail.com> (raw)
In-Reply-To: <20261127085401.263615-1-rolandschober1968@gmail.com>

Periodic advertising can only be started by an ISO broadcast socket so
far. Add a MGMT command which makes an advertising instance periodic:

  Add Extended Advertising Parameters    creates the instance
  Add Periodic Advertising Parameters    makes it periodic (new)
  Add Extended Advertising Data          enables it

The command takes the periodic advertising interval and the parameters
of Periodic Advertising with Responses (PAwR): number of subevents,
subevent interval, response slot delay and spacing, number of response
slots. With a number of subevents of 0 the instance is plain periodic
advertising; otherwise the controller has to support the PAwR
advertiser feature and LE Set Periodic Advertising Parameters [v2] is
used. The values are those of the HCI command, in its units.

The instance has to use the extended advertising PDUs and must not be
connectable. Its extended advertising parameters are set again when the
command runs, since they may have been sent before the instance became
periodic.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Roland Schober <rolandschober1968@gmail.com>
---
 include/net/bluetooth/hci_core.h |   8 ++
 include/net/bluetooth/hci_sync.h |   1 +
 include/net/bluetooth/mgmt.h     |  26 ++++++
 net/bluetooth/hci_sync.c         |  60 ++++++++++++++
 net/bluetooth/mgmt.c             | 133 +++++++++++++++++++++++++++++++
 5 files changed, 228 insertions(+)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 250245186702..117408645b9b 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -260,6 +260,14 @@ struct adv_info {
 	bool	scan_rsp_changed;
 	__u16	per_adv_data_len;
 	__u8	per_adv_data[HCI_MAX_PER_AD_LENGTH];
+	/* Periodic Advertising with Responses, if num_subevents is not 0 */
+	__u16	per_adv_min_interval;
+	__u16	per_adv_max_interval;
+	__u8	num_subevents;
+	__u8	subevent_interval;
+	__u8	response_slot_delay;
+	__u8	response_slot_spacing;
+	__u8	num_response_slots;
 	__s8	tx_power;
 	__u32   min_interval;
 	__u32   max_interval;
diff --git a/include/net/bluetooth/hci_sync.h b/include/net/bluetooth/hci_sync.h
index eab827dfa829..03d7e267af7c 100644
--- a/include/net/bluetooth/hci_sync.h
+++ b/include/net/bluetooth/hci_sync.h
@@ -118,6 +118,7 @@ int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 sid,
 			   u16 max_interval, u16 sync_interval);
 
 int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance);
+int hci_setup_per_adv_instance_sync(struct hci_dev *hdev, u8 instance);
 
 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
 				u8 instance, bool force);
diff --git a/include/net/bluetooth/mgmt.h b/include/net/bluetooth/mgmt.h
index 17bdd6345d19..57b9d8d8b57d 100644
--- a/include/net/bluetooth/mgmt.h
+++ b/include/net/bluetooth/mgmt.h
@@ -912,6 +912,32 @@ struct mgmt_cp_load_conn_subrate {
 } __packed;
 #define MGMT_LOAD_CONN_SUBRATE_SIZE	2
 
+/* Turn an instance added with Add Extended Advertising Parameters into
+ * periodic advertising; with num_subevents not 0, into Periodic Advertising
+ * with Responses.  The values are those of the HCI command, in its units:
+ *
+ *   min_interval, max_interval	1.25 ms (0x0006 to 0xffff)
+ *   subevent_interval		1.25 ms
+ *   response_slot_delay	1.25 ms
+ *   response_slot_spacing	0.125 ms
+ *   num_subevents, num_response_slots	counts
+ */
+#define MGMT_OP_ADD_PERIODIC_ADV_PARAMS	0x005D
+struct mgmt_cp_add_periodic_adv_params {
+	__u8	instance;
+	__le16	min_interval;
+	__le16	max_interval;
+	__u8	num_subevents;
+	__u8	subevent_interval;
+	__u8	response_slot_delay;
+	__u8	response_slot_spacing;
+	__u8	num_response_slots;
+} __packed;
+#define MGMT_ADD_PERIODIC_ADV_PARAMS_SIZE	10
+struct mgmt_rp_add_periodic_adv_params {
+	__u8	instance;
+} __packed;
+
 #define MGMT_EV_CMD_COMPLETE		0x0001
 struct mgmt_ev_cmd_complete {
 	__le16	opcode;
diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
index a1c723aad55e..3d8327180c6e 100644
--- a/net/bluetooth/hci_sync.c
+++ b/net/bluetooth/hci_sync.c
@@ -1968,6 +1968,66 @@ static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
 	return 0;
 }
 
+/* Program periodic advertising on an instance and enable it. The extended
+ * advertising parameters are set again first: they may have been chosen
+ * before the instance became periodic, and a periodic instance can neither be
+ * connectable nor use a non-resolvable private address. An instance with
+ * subevents is Periodic Advertising with Responses and takes the [v2]
+ * parameters. The train starts once extended advertising is enabled as well.
+ */
+int hci_setup_per_adv_instance_sync(struct hci_dev *hdev, u8 instance)
+{
+	struct hci_cp_le_set_per_adv_params_v2 cp;
+	struct adv_info *adv;
+	u16 min_interval, max_interval;
+	int err;
+
+	err = hci_disable_per_advertising_sync(hdev, instance);
+	if (err)
+		return err;
+
+	err = hci_setup_ext_adv_instance_sync(hdev, instance);
+	if (err)
+		return err;
+
+	memset(&cp, 0, sizeof(cp));
+
+	hci_dev_lock(hdev);
+
+	adv = hci_find_adv_instance(hdev, instance);
+	if (!adv || !adv->periodic) {
+		hci_dev_unlock(hdev);
+		return -EINVAL;
+	}
+
+	min_interval = adv->per_adv_min_interval;
+	max_interval = adv->per_adv_max_interval;
+
+	cp.handle = adv->handle;
+	cp.min_interval = cpu_to_le16(min_interval);
+	cp.max_interval = cpu_to_le16(max_interval);
+	cp.periodic_properties = 0x0000;
+	cp.num_subevents = adv->num_subevents;
+	cp.subevent_interval = adv->subevent_interval;
+	cp.response_slot_delay = adv->response_slot_delay;
+	cp.response_slot_spacing = adv->response_slot_spacing;
+	cp.num_response_slots = adv->num_response_slots;
+
+	hci_dev_unlock(hdev);
+
+	if (cp.num_subevents)
+		err = __hci_cmd_sync_status(hdev,
+					    HCI_OP_LE_SET_PER_ADV_PARAMS_V2,
+					    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
+	else
+		err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
+						  max_interval);
+	if (err)
+		return err;
+
+	return hci_enable_per_advertising_sync(hdev, instance);
+}
+
 /* Checks if periodic advertising data contains a Basic Announcement and if it
  * does generates a Broadcast ID and add Broadcast Announcement.
  */
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index d21538634bc3..2870278d51b0 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -130,6 +130,7 @@ static const u16 mgmt_commands[] = {
 	MGMT_OP_MESH_SEND,
 	MGMT_OP_MESH_SEND_CANCEL,
 	MGMT_OP_HCI_CMD_SYNC,
+	MGMT_OP_ADD_PERIODIC_ADV_PARAMS,
 };
 
 static const u16 mgmt_events[] = {
@@ -9797,6 +9798,137 @@ static int add_ext_adv_data(struct sock *sk, struct hci_dev *hdev, void *data,
 	return err;
 }
 
+static void add_periodic_adv_params_complete(struct hci_dev *hdev, void *data,
+					     int err)
+{
+	struct mgmt_pending_cmd *cmd = data;
+	struct mgmt_cp_add_periodic_adv_params *cp = cmd->param;
+	struct mgmt_rp_add_periodic_adv_params rp;
+	struct adv_info *adv;
+
+	bt_dev_dbg(hdev, "err %d", err);
+
+	if (err) {
+		/* The instance stays as it was added, without periodic
+		 * advertising.
+		 */
+		hci_dev_lock(hdev);
+		adv = hci_find_adv_instance(hdev, cp->instance);
+		if (adv) {
+			adv->periodic = false;
+			adv->num_subevents = 0;
+		}
+		hci_dev_unlock(hdev);
+
+		mgmt_cmd_status(cmd->sk, hdev->id, cmd->opcode,
+				mgmt_status(err));
+	} else {
+		rp.instance = cp->instance;
+		mgmt_cmd_complete(cmd->sk, hdev->id, cmd->opcode,
+				  MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
+	}
+
+	mgmt_pending_free(cmd);
+}
+
+static int add_periodic_adv_params_sync(struct hci_dev *hdev, void *data)
+{
+	struct mgmt_pending_cmd *cmd = data;
+	struct mgmt_cp_add_periodic_adv_params *cp = cmd->param;
+
+	return hci_setup_per_adv_instance_sync(hdev, cp->instance);
+}
+
+static int add_periodic_adv_params(struct sock *sk, struct hci_dev *hdev,
+				   void *data, u16 data_len)
+{
+	struct mgmt_cp_add_periodic_adv_params *cp = data;
+	struct mgmt_pending_cmd *cmd;
+	struct adv_info *adv;
+	u16 min_interval, max_interval;
+	u8 status;
+	int err;
+
+	bt_dev_dbg(hdev, "sock %p", sk);
+
+	status = mgmt_le_support(hdev);
+	if (status)
+		return mgmt_cmd_status(sk, hdev->id,
+				       MGMT_OP_ADD_PERIODIC_ADV_PARAMS, status);
+
+	if (!ext_adv_capable(hdev) || !per_adv_capable(hdev) ||
+	    (cp->num_subevents && !pawr_adv_capable(hdev)))
+		return mgmt_cmd_status(sk, hdev->id,
+				       MGMT_OP_ADD_PERIODIC_ADV_PARAMS,
+				       MGMT_STATUS_NOT_SUPPORTED);
+
+	min_interval = __le16_to_cpu(cp->min_interval);
+	max_interval = __le16_to_cpu(cp->max_interval);
+
+	if (min_interval < 0x0006 || min_interval > max_interval ||
+	    cp->num_subevents > 0x80)
+		return mgmt_cmd_status(sk, hdev->id,
+				       MGMT_OP_ADD_PERIODIC_ADV_PARAMS,
+				       MGMT_STATUS_INVALID_PARAMS);
+
+	hci_dev_lock(hdev);
+
+	if (!hdev_is_powered(hdev)) {
+		err = mgmt_cmd_status(sk, hdev->id,
+				      MGMT_OP_ADD_PERIODIC_ADV_PARAMS,
+				      MGMT_STATUS_REJECTED);
+		goto unlock;
+	}
+
+	/* Periodic advertising needs an advertising set which uses the
+	 * extended PDUs and is not connectable.
+	 */
+	adv = hci_find_adv_instance(hdev, cp->instance);
+	if (!adv || !(adv->flags & MGMT_ADV_FLAG_SEC_MASK) ||
+	    (adv->flags & MGMT_ADV_FLAG_CONNECTABLE)) {
+		err = mgmt_cmd_status(sk, hdev->id,
+				      MGMT_OP_ADD_PERIODIC_ADV_PARAMS,
+				      MGMT_STATUS_INVALID_PARAMS);
+		goto unlock;
+	}
+
+	if (adv_overridden_by_global(hdev, adv)) {
+		err = mgmt_cmd_status(sk, hdev->id,
+				      MGMT_OP_ADD_PERIODIC_ADV_PARAMS,
+				      MGMT_STATUS_BUSY);
+		goto unlock;
+	}
+
+	cmd = mgmt_pending_new(sk, MGMT_OP_ADD_PERIODIC_ADV_PARAMS, hdev, data,
+			       data_len);
+	if (!cmd) {
+		err = -ENOMEM;
+		goto unlock;
+	}
+
+	adv->periodic = true;
+	adv->per_adv_min_interval = min_interval;
+	adv->per_adv_max_interval = max_interval;
+	adv->num_subevents = cp->num_subevents;
+	adv->subevent_interval = cp->subevent_interval;
+	adv->response_slot_delay = cp->response_slot_delay;
+	adv->response_slot_spacing = cp->response_slot_spacing;
+	adv->num_response_slots = cp->num_response_slots;
+
+	err = hci_cmd_sync_queue(hdev, add_periodic_adv_params_sync, cmd,
+				 add_periodic_adv_params_complete);
+	if (err < 0) {
+		adv->periodic = false;
+		adv->num_subevents = 0;
+		mgmt_pending_free(cmd);
+	}
+
+unlock:
+	hci_dev_unlock(hdev);
+
+	return err;
+}
+
 static void remove_advertising_complete(struct hci_dev *hdev, void *data,
 					int err)
 {
@@ -10058,6 +10190,7 @@ static const struct hci_mgmt_handler mgmt_handlers[] = {
 	{ mgmt_hci_cmd_sync,       MGMT_HCI_CMD_SYNC_SIZE, HCI_MGMT_VAR_LEN },
 	{ load_conn_subrate,       MGMT_LOAD_CONN_SUBRATE_SIZE,
 						HCI_MGMT_VAR_LEN },
+	{ add_periodic_adv_params, MGMT_ADD_PERIODIC_ADV_PARAMS_SIZE },
 };
 
 void mgmt_index_added(struct hci_dev *hdev)
-- 
2.47.3


  parent reply	other threads:[~2026-11-27  8:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  8:53 [RFC PATCH 0/5] Bluetooth: Periodic advertising and PAwR over MGMT Roland Schober
2026-11-27  8:53 ` [RFC PATCH 1/5] Bluetooth: hci: Add Periodic Advertising with Responses definitions Roland Schober
2026-11-27  8:53 ` [RFC PATCH 2/5] Bluetooth: hci_sync: Disable periodic advertising before removing a set Roland Schober
2026-11-27  8:53 ` Roland Schober [this message]
2026-11-27  8:53 ` [RFC PATCH 4/5] Bluetooth: MGMT: Add Add Periodic Advertising Data command Roland Schober
2026-11-27  8:54 ` [RFC PATCH 5/5] Bluetooth: MGMT: Add PAwR subevent data and response reports 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=20261127085401.263615-4-rolandschober1968@gmail.com \
    --to=rolandschober1968@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.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