Skip to content

Commit 77805e7

Browse files
alwa-nordicsean-madigan
authored andcommitted
[nrf fromtree] Bluetooth: Deprecate adv auto-resume
The host-based adv auto-resume function has both a problematic implementation and disagreement in the community around how it should behave. See the issue linked resolved below for details. This patch makes the deprecation visible to the user. The user will be better served by a auto-resume tailored their applications use case, based on more primitive host API like `conn_cb.recycled`, which has obvious behavior that is unlikely to change. Resolves: zephyrproject-rtos/zephyr#72567 Signed-off-by: Aleksander Wasaznik <[email protected]> (cherry picked from commit 8cfad44) Signed-off-by: Sean Madigan <[email protected]>
1 parent 3d0bbe4 commit 77805e7

File tree

96 files changed

+394
-273
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+394
-273
lines changed

doc/releases/migration-guide-4.0.rst

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,78 @@ Bluetooth Classic
243243
Bluetooth Host
244244
==============
245245

246+
Automatic advertiser resumption is deprecated
247+
---------------------------------------------
248+
249+
.. note::
250+
251+
This deprecation is compiler-checked. If you get no warnings,
252+
you should not be affected.
253+
254+
Deprecated symbols:
255+
* :c:enumerator:`BT_LE_ADV_OPT_CONNECTABLE`
256+
* :c:enumerator:`BT_LE_ADV_OPT_ONE_TIME`
257+
* :c:macro:`BT_LE_ADV_CONN`
258+
259+
New symbols:
260+
* :c:enumerator:`BT_LE_ADV_OPT_CONN`
261+
* :c:macro:`BT_LE_ADV_CONN_FAST_1`
262+
* :c:macro:`BT_LE_ADV_CONN_FAST_2`
263+
264+
:c:enumerator:`BT_LE_ADV_OPT_CONNECTABLE` is a combined
265+
instruction to make the advertiser connectable and to enable
266+
automatic resumption. To disable the automatic resumption, use
267+
:c:enumerator:`BT_LE_ADV_OPT_CONN`.
268+
269+
Extended Advertising API with shorthands
270+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
271+
272+
Extended Advertising API ``bt_le_ext_adv_*`` implicitly assumes
273+
:c:enumerator:`BT_LE_ADV_OPT_ONE_TIME` and never automatically
274+
resume advertising. Therefore, the following search/replace can
275+
be applied without thinking:
276+
277+
Replace all
278+
279+
.. code-block:: diff
280+
281+
-bt_le_ext_adv_create(BT_LE_ADV_CONN, ...)
282+
+bt_le_ext_adv_create(BT_LE_ADV_FAST_2, ...)
283+
284+
.. code-block:: diff
285+
286+
-bt_le_ext_adv_update_param(..., BT_LE_ADV_CONN)
287+
+bt_le_ext_adv_update_param(..., BT_LE_ADV_FAST_2)
288+
289+
Extended Advertising API with custom parameters
290+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
291+
292+
You may have uses of :c:enumerator:`BT_LE_ADV_OPT_CONNECTABLE`
293+
in assignments to a :c:struct:`bt_le_adv_param`. If your struct
294+
is never passed to :c:func:`bt_le_adv_start`, you should:
295+
296+
* replace :c:enumerator:`BT_LE_ADV_OPT_CONNECTABLE` with
297+
:c:enumerator:`BT_LE_ADV_OPT_CONN`.
298+
* remove :c:enumerator:`BT_LE_ADV_OPT_ONE_TIME`.
299+
300+
Legacy Advertising API not using automatic resumption
301+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
302+
303+
Any calls to :c:func:`bt_le_adv_start` that use the combination
304+
:c:enumerator:`BT_LE_ADV_OPT_CONNECTABLE` and
305+
:c:enumerator:`BT_LE_ADV_OPT_ONE_TIME` should have that
306+
combination replaced with :c:enumerator:`BT_LE_ADV_OPT_CONN`.
307+
308+
Legacy Advertising API using automatic resumption
309+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
310+
311+
For this case, the application has to take over the
312+
responsibility of restarting the advertiser.
313+
314+
Refer to the extended advertising sample for an example
315+
implementation of advertiser restarting. The same technique can
316+
be used for legacy advertising.
317+
246318
Bluetooth Crypto
247319
================
248320

include/zephyr/bluetooth/bluetooth.h

Lines changed: 114 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -523,16 +523,42 @@ enum {
523523
/**
524524
* @brief Advertise as connectable.
525525
*
526+
* @deprecated Use @ref BT_LE_ADV_OPT_CONN instead.
527+
*
526528
* Advertise as connectable. If not connectable then the type of
527529
* advertising is determined by providing scan response data.
528530
* The advertiser address is determined by the type of advertising
529531
* and/or enabling privacy @kconfig{CONFIG_BT_PRIVACY}.
532+
*
533+
* Starting connectable advertising preallocates a connection
534+
* object. If this fails, the API returns @c -ENOMEM.
535+
*
536+
* When an advertiser set results in a connection creation, the
537+
* controller automatically disables that advertising set.
538+
*
539+
* If the advertising set was started with @ref bt_le_adv_start
540+
* without @ref BT_LE_ADV_OPT_ONE_TIME, the host will attempt to
541+
* resume the advertiser under some conditions.
542+
*/
543+
BT_LE_ADV_OPT_CONNECTABLE __deprecated = BIT(0),
544+
545+
/**
546+
* @internal
547+
*
548+
* Internal access to the deprecated value to maintain the
549+
* implementation of the deprecated feature.
550+
*
551+
* At the end of the deprecation period, ABI will change so
552+
* `BT_LE_ADV_OPT_CONN` is just `BIT(0)`, removing the need for this
553+
* symbol.
530554
*/
531-
BT_LE_ADV_OPT_CONNECTABLE = BIT(0),
555+
_BT_LE_ADV_OPT_CONNECTABLE = BIT(0),
532556

533557
/**
534558
* @brief Advertise one time.
535559
*
560+
* @deprecated Use @ref BT_LE_ADV_OPT_CONN instead.
561+
*
536562
* Don't try to resume connectable advertising after a connection.
537563
* This option is only meaningful when used together with
538564
* BT_LE_ADV_OPT_CONNECTABLE. If set the advertising will be stopped
@@ -544,7 +570,35 @@ enum {
544570
* @ref bt_le_ext_adv_start then this behavior is the default behavior
545571
* and this flag has no effect.
546572
*/
547-
BT_LE_ADV_OPT_ONE_TIME = BIT(1),
573+
BT_LE_ADV_OPT_ONE_TIME __deprecated = BIT(1),
574+
575+
/**
576+
* @internal
577+
*
578+
* Internal access to the deprecated value to maintain
579+
* the implementation of the deprecated feature.
580+
*/
581+
_BT_LE_ADV_OPT_ONE_TIME = BIT(1),
582+
583+
/**
584+
* @brief Connectable advertising
585+
*
586+
* Starting connectable advertising preallocates a connection
587+
* object. If this fails, the API returns @c -ENOMEM.
588+
*
589+
* The advertising set stops immediately after it creates a
590+
* connection. This happens automatically in the controller.
591+
*
592+
* @note To continue advertising after a connection is created,
593+
* the application should listen for the @ref bt_conn_cb.connected
594+
* event and start the advertising set again. Note that the
595+
* advertiser cannot be started when all connection objects are
596+
* in use. In that case, defer starting the advertiser until
597+
* @ref bt_conn_cb.recycled. To continue after a disconnection,
598+
* listen for @ref bt_conn_cb.recycled.
599+
600+
*/
601+
BT_LE_ADV_OPT_CONN = BIT(0) | BIT(1),
548602

549603
/**
550604
* @brief Advertise using identity address.
@@ -623,8 +677,7 @@ enum {
623677
* @brief Support scan response data.
624678
*
625679
* When used together with @ref BT_LE_ADV_OPT_EXT_ADV then this option
626-
* cannot be used together with the @ref BT_LE_ADV_OPT_CONNECTABLE
627-
* option.
680+
* cannot be used together with the @ref BT_LE_ADV_OPT_CONN option.
628681
* When used together with @ref BT_LE_ADV_OPT_EXT_ADV then scan
629682
* response data must be set.
630683
*/
@@ -912,20 +965,61 @@ struct bt_le_per_adv_param {
912965
BT_LE_ADV_PARAM_INIT(_options, _int_min, _int_max, _peer) \
913966
})
914967

915-
#define BT_LE_ADV_CONN_DIR(_peer) BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | \
916-
BT_LE_ADV_OPT_ONE_TIME, 0, 0,\
917-
_peer)
968+
#define BT_LE_ADV_CONN_DIR(_peer) BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONN, 0, 0, _peer)
918969

970+
/**
971+
* @deprecated This is a convenience macro for @ref
972+
* BT_LE_ADV_OPT_CONNECTABLE, which is deprecated. Please use
973+
* @ref BT_LE_ADV_CONN_FAST_1 or @ref BT_LE_ADV_CONN_FAST_2
974+
* instead.
975+
*/
976+
#define BT_LE_ADV_CONN \
977+
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE, BT_GAP_ADV_FAST_INT_MIN_2, \
978+
BT_GAP_ADV_FAST_INT_MAX_2, NULL) \
979+
__DEPRECATED_MACRO
919980

920-
#define BT_LE_ADV_CONN BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE, \
921-
BT_GAP_ADV_FAST_INT_MIN_2, \
922-
BT_GAP_ADV_FAST_INT_MAX_2, NULL)
981+
/** @brief GAP recommended connectable advertising
982+
*
983+
* This is the recommended default for when a person is likely
984+
* to be waiting the device to connect or be discovered.
985+
*
986+
* Use a longer interval to conserve battery at the cost of
987+
* responsiveness. Consider entering a lower power state with
988+
* longer intervals after a timeout.
989+
*
990+
* GAP recommends advertisers use this "when user-initiated".
991+
* The application developer decides what this means. It can by
992+
* any time the user interacts with the device, a press on a
993+
* dedicated Bluetooth wakeup button, or anything in-between.
994+
*
995+
* This is the recommended setting for limited discoverable
996+
* mode.
997+
*
998+
* See Bluetooth Core Specification:
999+
* - 3.C.A "Timers and Constants", T_GAP(adv_fast_interval1)
1000+
* - 3.C.9.3.11 "Connection Establishment Timing parameters"
1001+
*/
1002+
#define BT_LE_ADV_CONN_FAST_1 \
1003+
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONN, BT_GAP_ADV_FAST_INT_MIN_1, BT_GAP_ADV_FAST_INT_MAX_1, \
1004+
NULL)
9231005

924-
/** This is the recommended default for connectable advertisers.
1006+
/** @brief Connectable advertising with
1007+
* T_GAP(adv_fast_interval2)
1008+
*
1009+
* The advertising interval corresponds to what was offered as
1010+
* `BT_LE_ADV_CONN` in Zephyr 3.6 and earlier, but unlike
1011+
* `BT_LE_ADV_CONN`, the host does not automatically resume the
1012+
* advertiser after it results in a connection.
1013+
*
1014+
* See Bluetooth Core Specification:
1015+
* - 3.C.A "Timers and Constants", T_GAP(adv_fast_interval1)
1016+
* - 3.C.9.3.11 "Connection Establishment Timing parameters"
9251017
*/
926-
#define BT_LE_ADV_CONN_ONE_TIME \
927-
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_ONE_TIME, \
928-
BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2, NULL)
1018+
#define BT_LE_ADV_CONN_FAST_2 \
1019+
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONN, BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2, \
1020+
NULL)
1021+
1022+
#define BT_LE_ADV_CONN_ONE_TIME BT_LE_ADV_CONN_FAST_2 __DEPRECATED_MACRO
9291023

9301024
/**
9311025
* @deprecated This macro will be removed in the near future, see
@@ -948,11 +1042,9 @@ struct bt_le_per_adv_param {
9481042
BT_GAP_ADV_FAST_INT_MAX_2, NULL) \
9491043
__DEPRECATED_MACRO
9501044

951-
#define BT_LE_ADV_CONN_DIR_LOW_DUTY(_peer) \
952-
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_ONE_TIME | \
953-
BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY, \
954-
BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2, \
955-
_peer)
1045+
#define BT_LE_ADV_CONN_DIR_LOW_DUTY(_peer) \
1046+
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONN | BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY, \
1047+
BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2, _peer)
9561048

9571049
/** Non-connectable advertising with private address */
9581050
#define BT_LE_ADV_NCONN BT_LE_ADV_PARAM(0, BT_GAP_ADV_FAST_INT_MIN_2, \
@@ -976,11 +1068,9 @@ struct bt_le_per_adv_param {
9761068
NULL)
9771069

9781070
/** Connectable extended advertising */
979-
#define BT_LE_EXT_ADV_CONN BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | \
980-
BT_LE_ADV_OPT_CONNECTABLE, \
981-
BT_GAP_ADV_FAST_INT_MIN_2, \
982-
BT_GAP_ADV_FAST_INT_MAX_2, \
983-
NULL)
1071+
#define BT_LE_EXT_ADV_CONN \
1072+
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_CONN, BT_GAP_ADV_FAST_INT_MIN_2, \
1073+
BT_GAP_ADV_FAST_INT_MAX_2, NULL)
9841074

9851075
/**
9861076
* @deprecated This macro will be removed in the near future, see

samples/bluetooth/direct_adv/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static void bt_ready(void)
128128
adv_param.options |= BT_LE_ADV_OPT_DIR_ADDR_RPA;
129129
err = bt_le_adv_start(&adv_param, NULL, 0, NULL, 0);
130130
} else {
131-
err = bt_le_adv_start(BT_LE_ADV_CONN_ONE_TIME, ad, ARRAY_SIZE(ad), NULL, 0);
131+
err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0);
132132
}
133133

134134
if (err) {

samples/bluetooth/direction_finding_peripheral/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static void bt_ready(void)
100100

101101
printk("Bluetooth initialized\n");
102102

103-
err = bt_le_adv_start(BT_LE_ADV_CONN_ONE_TIME, ad, ARRAY_SIZE(ad), NULL, 0);
103+
err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0);
104104
if (err) {
105105
printk("Advertising failed to start (err %d)\n", err);
106106
return;

samples/bluetooth/eddystone/src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ static int eds_slot_restart(struct eds_slot *slot, uint8_t type)
431431
addr = oob.addr;
432432
}
433433

434-
err = bt_le_adv_start(BT_LE_ADV_CONN_ONE_TIME, ad, ARRAY_SIZE(ad), NULL, 0);
434+
err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0);
435435
} else {
436436
size_t count = 1;
437437

@@ -634,7 +634,7 @@ static void bt_ready(int err)
634634
printk("Bluetooth initialized\n");
635635

636636
/* Start advertising */
637-
err = bt_le_adv_start(BT_LE_ADV_CONN_ONE_TIME, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
637+
err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
638638
if (err) {
639639
printk("Advertising failed to start (err %d)\n", err);
640640
return;

samples/bluetooth/encrypted_advertising/peripheral/src/peripheral_ead.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,10 @@ static bool rpa_expired_cb(struct bt_le_ext_adv *adv)
128128
static int create_adv(struct bt_le_ext_adv **adv)
129129
{
130130
int err;
131-
struct bt_le_adv_param params;
132-
133-
memset(&params, 0, sizeof(struct bt_le_adv_param));
134-
135-
params.options |= BT_LE_ADV_OPT_CONNECTABLE;
136-
params.options |= BT_LE_ADV_OPT_EXT_ADV;
137-
138-
params.id = BT_ID_DEFAULT;
139-
params.sid = 0;
140-
params.interval_min = BT_GAP_ADV_FAST_INT_MIN_2;
141-
params.interval_max = BT_GAP_ADV_FAST_INT_MAX_2;
142131

143132
adv_cb.rpa_expired = rpa_expired_cb;
144133

145-
err = bt_le_ext_adv_create(&params, &adv_cb, adv);
134+
err = bt_le_ext_adv_create(BT_LE_EXT_ADV_CONN, &adv_cb, adv);
146135
if (err) {
147136
LOG_ERR("Failed to create advertiser (%d)", err);
148137
return -1;

samples/bluetooth/hci_pwr_ctrl/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static K_THREAD_STACK_DEFINE(pwr_thread_stack, 512);
4343
static const int8_t txpower[DEVICE_BEACON_TXPOWER_NUM] = {4, 0, -3, -8,
4444
-15, -18, -23, -30};
4545
static const struct bt_le_adv_param *param =
46-
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_ONE_TIME, 0x0020, 0x0020, NULL);
46+
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONN, 0x0020, 0x0020, NULL);
4747

4848
static void read_conn_rssi(uint16_t handle, int8_t *rssi)
4949
{

samples/bluetooth/iso_connected_benchmark/src/main.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,10 +1279,7 @@ static int run_peripheral(void)
12791279
}
12801280

12811281
LOG_INF("Starting advertising");
1282-
err = bt_le_adv_start(
1283-
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_ONE_TIME | BT_LE_ADV_OPT_CONNECTABLE,
1284-
BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2, NULL),
1285-
NULL, 0, sd, ARRAY_SIZE(sd));
1282+
err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, NULL, 0, sd, ARRAY_SIZE(sd));
12861283
if (err != 0) {
12871284
LOG_ERR("Advertising failed to start: %d", err);
12881285
return err;

samples/bluetooth/iso_peripheral/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ int main(void)
177177
return 0;
178178
}
179179

180-
err = bt_le_adv_start(BT_LE_ADV_CONN_ONE_TIME, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
180+
err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
181181
if (err) {
182182
printk("Advertising failed to start (err %d)\n", err);
183183
return 0;

samples/bluetooth/mtu_update/peripheral/src/peripheral_mtu_update.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void run_peripheral_sample(uint8_t *notify_data, size_t notify_data_size, uint16
9191
struct bt_gatt_attr *notify_crch =
9292
bt_gatt_find_by_uuid(mtu_test.attrs, 0xffff, &notify_characteristic_uuid.uuid);
9393

94-
bt_le_adv_start(BT_LE_ADV_CONN_ONE_TIME, adv_ad_data, ARRAY_SIZE(adv_ad_data), NULL, 0);
94+
bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, adv_ad_data, ARRAY_SIZE(adv_ad_data), NULL, 0);
9595

9696
bool infinite = seconds == 0;
9797

0 commit comments

Comments
 (0)