From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.45 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796336065; b=pQ4ASIrqD3hK4TsMmOFwGe358z533P6AKtj7HDUd8c2BJuO2HticMiU+b+QIVTwryBi/ EIPkD01BsuvKtkYG2AKQzmlHkfqCV+DHUEYaWX+oYilxRnqdQJjEaCbkcC8k6h+g+wFzZ 9JOkn3prj9NgMY5Jc/D6WcOaoRKVb0RoWBFEE8Pwudf1nH2Fv5W+ZTlfVMyb73j2FLlIE Q+3ggO6iMXbDEFf6WxijEKZ6skWmSoNNfGFbMTt0a4IpcL83MZGc/tq1ydzNCxfqovK6q mZhn6KWK2r3xFynp+X6YajOJxuyK4MTk+pAUqBe4sVfV6zVVRauXO+Adp7pknDayI5w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796336065; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=uv+0dmhnY1u61Zx+stwzCuNTgs5AGzc0jU/ffAoVeZU=; b=BDJJio0IiIR95+MDsofKTUbLVig9TL7dcVMpBBAt1xYZB9yNQgkotrL7T69wv/Ir0UQm CKm2ND90Rju2V8E3GqkXxnKj24/1AejqmfBniyF16AiQwdj9lweUs9r3hmKyDfPyb7yQj zj0HWoAlaGmziIfxQEI9vxjnlN86njRAmz4+22TadyL9g0HtooZRlGqXGR3sAzniaK0MP TSti4d7jH5siDrZtQv3FwgYm/qlHbTIn3DdE0rVGoMh1L5bfzzQUp9YtZxfq49C/ZZW/5 zEUVzbhB6Ep9CWlEVIAtczJiCFuHWwNnxuF92iPs9uF93fulN8NnaiiZD/3W9Faocbw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=szZUty7d; arc=none smtp.remote-ip=209.85.221.45 Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A023D606A32D for ; Thu, 3 Dec 2026 22:14:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A023D606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=szZUty7d Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-4a440c9e13aso48227f8f.3 for ; Thu, 03 Dec 2026 14:14:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796336063; x=1796940863; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=uv+0dmhnY1u61Zx+stwzCuNTgs5AGzc0jU/ffAoVeZU=; b=szZUty7dHWNHvEHZIuMMQ/O9owYQHUiA16Et8nk6JkhR5jaWPaqFiSGV6Sbf/fqKwv XVCq5X9CImDGuxqGDXNNlai59Eyx7B9F59vBPVEQjsokUBwVAyrOCw/69wMmBKHHxGgs 8227YAd6+A2vP79384W27LexhTrGhzNLaVSjBsEILgVZjMwVzOqExVlT6c8YSIBV3gYs 7MJXPoUHrCuI0qZW28t66KimPnzLnqQradigvOeuVR0A3gYjsC/6IEw0schgXOD0tW1v QW5uJ9qK6UpIm73AMDjgP45uokNXKp1P2eu67dSXw3EXkDULtkhnsIr+UTIPTfZfdGWq pb9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796336063; x=1796940863; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=uv+0dmhnY1u61Zx+stwzCuNTgs5AGzc0jU/ffAoVeZU=; b=UNOON+9NpzriGUM5GMk/XMF8TUR0wwUgZRUKMI3r7eMYG3b6m8h/WRQZHEjxBWhvQa Y/FXmg6b+pdEl88IT3AdnuveAhnHRHOkSU6WbgkSvKc6EFlkA8GcikrzrXq2FKH246aq GiJ42jFmFc2fdIxAhmcSN5b2HwWKl5X4e8JYC/hwubUfznjinsPb6ZsRGhsDGPC+tFBA cf1fGzFt7aOjBQH00jOBWMJqPBu9N6gTJ1OAN0PlXMchQToSLi72EOUye2HgwLm4EMUZ Mg1Mexsa4WOG5a+/F1zmcuIUySx46fGJmWA7kg0wqZb/+y4v4cJP6jpQ7qX7krVaFlxC ePnA== X-Gm-Message-State: APuU4Z8EkOpyyr2mYhhpGEZDmr3hrSpZIHgJKx9iJwD6NxOCBmNHyuOY d8YwKMfmvWzkigtueC4NRXCNg86H4iSfyUDnuuBVVxpfafZLMlwQvJ29toAoVs30 X-Gm-Gg: AZFdTZfkGmLHyCMwzl3t6Pa4ILQDYLPYUHpfQUpmRgx0OoZgul13tPF2Ui5vydRlpT+ wRZvjLjqqJFjXDIYHzvE9T3kmmPa0lTjDW+mWuDooWvM3tagHOIXi0d2fXhetK0/76VjkAhqVCV SvdLwXNICj8vhw1DMwQo0KoFdPtoaeA5R8p0K5kkWfVG+LmmLShTEszk+ft96DmzPodfW6+V7AU ybEasScBZgyRcGFypS6d2XdLTpzxYP/9Lf/sonarkksXnsymNKgLruJo1Mm30bJ+WaML98iB422 uPNyUXP7dVv/SxyHTujmP0eP2ReFXn2u8/ugOudmKrwoYtC7QSW/WAmu29ig0d0v4UniVQ7j110 JwGECwiXu1JDk0ekeoQxCHUlcrzU+CmvNHvL6Jk4CAx9X2NEa53kfKmjsAovCnDtaQNESEeZ8q2 GE0XTw8qVG3kwWQBftLZzXnY3wae2GVaiGZVpqJUJIZWTTJIeHBd+PcBmvd49lonWAnBGs+E9Xs Le3PSbMsqnecNAMAaDR2dyAUAER0RVF/qWLNyM0CuOEL3VaVsYlBbQgOHJoA//humPnepYM++SM 5b8t4fFHwdBjpi1/lW3mzLjy9D4s/PuJe119Sl9lJ1zbEWM27I6OzXHjpPUiR5CpQ2snvA== X-Received: by 2002:a05:600d:6504:10b0:4bb:7ad9:301f with SMTP id 5b1f17b1804b1-4bbeb84cef9mr356455e9.2.1796336063029; Thu, 03 Dec 2026 14:14:23 -0800 (PST) Received: from schinder.fritz.box ([2001:9e8:b39a:7900:a13b:fe74:8c9c:c788]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbe00df154sm117266225e9.14.2026.12.03.14.14.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:14:22 -0800 (PST) From: Roland Schober To: linux-bluetooth@vger.kernel.org Cc: luiz.dentz@gmail.com, Roland Schober Subject: [RFC PATCH v2 4/6] Bluetooth: MGMT: Add Add Periodic Advertising Parameters command Date: Thu, 3 Dec 2026 23:14:03 +0100 Message-ID: <20261203221405.682222-5-rolandschober1968@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261203221405.682222-1-rolandschober1968@gmail.com> References: <20261203221405.682222-1-rolandschober1968@gmail.com> X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 01969ab57339..4d9c5f6a05c0 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