Skip to content

Commit 0a8bbbd

Browse files
jori-nordicalwa-nordic
authored andcommitted
Bluetooth: adv: add USE_NRPA advertising option
Allows the application to force the use of an NRPA. This is applied regardless of any other roles running (ie scanner) or advertising type. Signed-off-by: Jonathan Rico <[email protected]> Co-authored-by: Aleksander Wasaznik <[email protected]>
1 parent d379542 commit 0a8bbbd

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

include/zephyr/bluetooth/bluetooth.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,20 @@ enum {
685685
* @note Requires @ref BT_LE_ADV_OPT_USE_NAME
686686
*/
687687
BT_LE_ADV_OPT_FORCE_NAME_IN_AD = BIT(18),
688+
689+
/**
690+
* @brief Advertise using a Non-Resolvable Private Address.
691+
*
692+
* A new NRPA is set when updating the advertising parameters.
693+
*
694+
* This is an advanced feature; most users will want to enable
695+
* @kconfig{CONFIG_BT_EXT_ADV} instead.
696+
*
697+
* @note Not implemented when @kconfig{CONFIG_BT_PRIVACY}.
698+
*
699+
* @note Mutually exclusive with BT_LE_ADV_OPT_USE_IDENTITY.
700+
*/
701+
BT_LE_ADV_OPT_USE_NRPA = BIT(19),
688702
};
689703

690704
/** LE Advertising Parameters. */

subsys/bluetooth/host/adv.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,8 @@ static int le_ext_adv_param_set(struct bt_le_ext_adv *adv,
11301130
cp = net_buf_add(buf, sizeof(*cp));
11311131
(void)memset(cp, 0, sizeof(*cp));
11321132

1133+
adv->options = param->options;
1134+
11331135
err = bt_id_set_adv_own_addr(adv, param->options, dir_adv,
11341136
&cp->own_addr_type);
11351137
if (err) {
@@ -1151,8 +1153,6 @@ static int le_ext_adv_param_set(struct bt_le_ext_adv *adv,
11511153
cp->filter_policy = get_filter_policy(param->options);
11521154
cp->tx_power = BT_HCI_LE_ADV_TX_POWER_NO_PREF;
11531155

1154-
adv->options = param->options;
1155-
11561156
cp->prim_adv_phy = BT_HCI_LE_PHY_1M;
11571157
if ((param->options & BT_LE_ADV_OPT_EXT_ADV) &&
11581158
!(param->options & BT_LE_ADV_OPT_NO_2M)) {
@@ -2206,6 +2206,9 @@ void bt_hci_le_adv_set_terminated(struct net_buf *buf)
22062206
bt_addr_copy(&conn->le.resp_addr.a,
22072207
&adv->random_addr.a);
22082208
}
2209+
} else if (adv->options & BT_LE_ADV_OPT_USE_NRPA) {
2210+
bt_addr_le_copy(&conn->le.resp_addr,
2211+
&adv->random_addr);
22092212
} else {
22102213
bt_addr_le_copy(&conn->le.resp_addr,
22112214
&bt_dev.id_addr[conn->id]);

subsys/bluetooth/host/id.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@ int bt_id_set_adv_private_addr(struct bt_le_ext_adv *adv)
329329
return -EINVAL;
330330
}
331331

332+
if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
333+
(adv->options & BT_LE_ADV_OPT_USE_NRPA)) {
334+
/* The host doesn't support setting NRPAs when BT_PRIVACY=y.
335+
* In that case you probably want to use an RPA anyway.
336+
*/
337+
LOG_ERR("NRPA not supported when BT_PRIVACY=y");
338+
339+
return -ENOSYS;
340+
}
341+
332342
if (!(IS_ENABLED(CONFIG_BT_EXT_ADV) &&
333343
BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features))) {
334344
return bt_id_set_private_addr(adv->id);
@@ -1754,6 +1764,23 @@ int bt_id_set_adv_own_addr(struct bt_le_ext_adv *adv, uint32_t options,
17541764
/* Set which local identity address we're advertising with */
17551765
id_addr = &bt_dev.id_addr[adv->id];
17561766

1767+
/* Short-circuit to force NRPA usage */
1768+
if (options & BT_LE_ADV_OPT_USE_NRPA) {
1769+
if (options & BT_LE_ADV_OPT_USE_IDENTITY) {
1770+
LOG_ERR("Can't set both IDENTITY & NRPA");
1771+
1772+
return -EINVAL;
1773+
}
1774+
1775+
err = bt_id_set_adv_private_addr(adv);
1776+
if (err) {
1777+
return err;
1778+
}
1779+
*own_addr_type = BT_ADDR_LE_RANDOM;
1780+
1781+
return 0;
1782+
}
1783+
17571784
if (options & BT_LE_ADV_OPT_CONNECTABLE) {
17581785
if (dir_adv && (options & BT_LE_ADV_OPT_DIR_ADDR_RPA) &&
17591786
!BT_FEAT_LE_PRIVACY(bt_dev.le.features)) {

0 commit comments

Comments
 (0)