Skip to content

Commit 8cfad44

Browse files
alwa-nordiccarlescufi
authored andcommitted
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 <aleksander.wasaznik@nordicsemi.no>
1 parent d7ab4f2 commit 8cfad44

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
@@ -336,6 +336,78 @@ Bluetooth Classic
336336
Bluetooth Host
337337
==============
338338

339+
Automatic advertiser resumption is deprecated
340+
---------------------------------------------
341+
342+
.. note::
343+
344+
This deprecation is compiler-checked. If you get no warnings,
345+
you should not be affected.
346+
347+
Deprecated symbols:
348+
* :c:enumerator:`BT_LE_ADV_OPT_CONNECTABLE`
349+
* :c:enumerator:`BT_LE_ADV_OPT_ONE_TIME`
350+
* :c:macro:`BT_LE_ADV_CONN`
351+
352+
New symbols:
353+
* :c:enumerator:`BT_LE_ADV_OPT_CONN`
354+
* :c:macro:`BT_LE_ADV_CONN_FAST_1`
355+
* :c:macro:`BT_LE_ADV_CONN_FAST_2`
356+
357+
:c:enumerator:`BT_LE_ADV_OPT_CONNECTABLE` is a combined
358+
instruction to make the advertiser connectable and to enable
359+
automatic resumption. To disable the automatic resumption, use
360+
:c:enumerator:`BT_LE_ADV_OPT_CONN`.
361+
362+
Extended Advertising API with shorthands
363+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
364+
365+
Extended Advertising API ``bt_le_ext_adv_*`` implicitly assumes
366+
:c:enumerator:`BT_LE_ADV_OPT_ONE_TIME` and never automatically
367+
resume advertising. Therefore, the following search/replace can
368+
be applied without thinking:
369+
370+
Replace all
371+
372+
.. code-block:: diff
373+
374+
-bt_le_ext_adv_create(BT_LE_ADV_CONN, ...)
375+
+bt_le_ext_adv_create(BT_LE_ADV_FAST_2, ...)
376+
377+
.. code-block:: diff
378+
379+
-bt_le_ext_adv_update_param(..., BT_LE_ADV_CONN)
380+
+bt_le_ext_adv_update_param(..., BT_LE_ADV_FAST_2)
381+
382+
Extended Advertising API with custom parameters
383+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
384+
385+
You may have uses of :c:enumerator:`BT_LE_ADV_OPT_CONNECTABLE`
386+
in assignments to a :c:struct:`bt_le_adv_param`. If your struct
387+
is never passed to :c:func:`bt_le_adv_start`, you should:
388+
389+
* replace :c:enumerator:`BT_LE_ADV_OPT_CONNECTABLE` with
390+
:c:enumerator:`BT_LE_ADV_OPT_CONN`.
391+
* remove :c:enumerator:`BT_LE_ADV_OPT_ONE_TIME`.
392+
393+
Legacy Advertising API not using automatic resumption
394+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
395+
396+
Any calls to :c:func:`bt_le_adv_start` that use the combination
397+
:c:enumerator:`BT_LE_ADV_OPT_CONNECTABLE` and
398+
:c:enumerator:`BT_LE_ADV_OPT_ONE_TIME` should have that
399+
combination replaced with :c:enumerator:`BT_LE_ADV_OPT_CONN`.
400+
401+
Legacy Advertising API using automatic resumption
402+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
403+
404+
For this case, the application has to take over the
405+
responsibility of restarting the advertiser.
406+
407+
Refer to the extended advertising sample for an example
408+
implementation of advertiser restarting. The same technique can
409+
be used for legacy advertising.
410+
339411
Bluetooth Crypto
340412
================
341413

include/zephyr/bluetooth/bluetooth.h

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

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

551605
/**
552606
* @brief Advertise using identity address.
@@ -625,8 +679,7 @@ enum {
625679
* @brief Support scan response data.
626680
*
627681
* When used together with @ref BT_LE_ADV_OPT_EXT_ADV then this option
628-
* cannot be used together with the @ref BT_LE_ADV_OPT_CONNECTABLE
629-
* option.
682+
* cannot be used together with the @ref BT_LE_ADV_OPT_CONN option.
630683
* When used together with @ref BT_LE_ADV_OPT_EXT_ADV then scan
631684
* response data must be set.
632685
*/
@@ -914,20 +967,61 @@ struct bt_le_per_adv_param {
914967
BT_LE_ADV_PARAM_INIT(_options, _int_min, _int_max, _peer) \
915968
})
916969

917-
#define BT_LE_ADV_CONN_DIR(_peer) BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | \
918-
BT_LE_ADV_OPT_ONE_TIME, 0, 0,\
919-
_peer)
970+
#define BT_LE_ADV_CONN_DIR(_peer) BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONN, 0, 0, _peer)
920971

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

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

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

9321026
/**
9331027
* @deprecated This macro will be removed in the near future, see
@@ -950,11 +1044,9 @@ struct bt_le_per_adv_param {
9501044
BT_GAP_ADV_FAST_INT_MAX_2, NULL) \
9511045
__DEPRECATED_MACRO
9521046

953-
#define BT_LE_ADV_CONN_DIR_LOW_DUTY(_peer) \
954-
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_ONE_TIME | \
955-
BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY, \
956-
BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2, \
957-
_peer)
1047+
#define BT_LE_ADV_CONN_DIR_LOW_DUTY(_peer) \
1048+
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONN | BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY, \
1049+
BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2, _peer)
9581050

9591051
/** Non-connectable advertising with private address */
9601052
#define BT_LE_ADV_NCONN BT_LE_ADV_PARAM(0, BT_GAP_ADV_FAST_INT_MIN_2, \
@@ -978,11 +1070,9 @@ struct bt_le_per_adv_param {
9781070
NULL)
9791071

9801072
/** Connectable extended advertising */
981-
#define BT_LE_EXT_ADV_CONN BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | \
982-
BT_LE_ADV_OPT_CONNECTABLE, \
983-
BT_GAP_ADV_FAST_INT_MIN_2, \
984-
BT_GAP_ADV_FAST_INT_MAX_2, \
985-
NULL)
1073+
#define BT_LE_EXT_ADV_CONN \
1074+
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_CONN, BT_GAP_ADV_FAST_INT_MIN_2, \
1075+
BT_GAP_ADV_FAST_INT_MAX_2, NULL)
9861076

9871077
/**
9881078
* @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)