Skip to content

Commit 689ea79

Browse files
adigierlubos
authored andcommitted
[nrf toup][nrfconnect] Retry BLE advertising restart
Replace the sWasDisconnection flag with an atomic restart flag so advertising can be retried when bt_le_adv_start() fails due to lack of available connection object. With CONFIG_CHIP_BLE_MULTI_IDENTITY_SUPPORT, disconnect and recycled callbacks can arrive in an order where a restart attempt runs before all connection slots are available. Re-arm the flag on failure so a subsequent recycled callback schedules another restart attempt. Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no> (cherry picked from commit 6d8ea97)
1 parent 6461e5a commit 689ea79

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

src/platform/Zephyr/BLEAdvertisingArbiter.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ namespace {
3131
// List of advertising requests ordered by priority
3232
sys_slist_t sRequests;
3333

34-
bool sIsInitialized = false;
35-
bool sWasDisconnection = false;
36-
uint8_t sBtId = 0;
34+
bool sIsInitialized = false;
35+
atomic_t sRestart = ATOMIC_INIT(0);
36+
uint8_t sBtId = 0;
3737

3838
// Cast an intrusive list node to the containing request object
3939
const BLEAdvertisingArbiter::Request & ToRequest(const sys_snode_t * node)
@@ -103,25 +103,32 @@ BT_CONN_CB_DEFINE(conn_callbacks) = {
103103
// Ignore disconnections from other identities
104104
VerifyOrReturn(IsOurIdentity(conn));
105105
#endif // CONFIG_CHIP_BLE_MULTI_IDENTITY_SUPPORT
106-
sWasDisconnection = true;
106+
atomic_set(&sRestart, 1);
107107
},
108108
.recycled =
109109
[]() {
110110
// In this callback the connection object was returned to the pool and we can try to re-start connectable
111-
// advertising, but only if the disconnection was detected.
112-
if (sWasDisconnection)
111+
// advertising, but only if the disconnection was detected or a previous restart attempt failed.
112+
constexpr atomic_val_t oldValue = 1;
113+
constexpr atomic_val_t newValue = 0;
114+
const bool shouldRestart = atomic_cas(&sRestart, oldValue, newValue);
115+
116+
if (shouldRestart)
113117
{
118+
114119
SystemLayer().ScheduleLambda([] {
115120
if (!sys_slist_is_empty(&sRequests))
116121
{
117122
// Starting from Zephyr 4.0 Automatic advertiser resumption is deprecated,
118123
// so the BLE Advertising Arbiter has to take over the responsibility of restarting the advertiser.
119124
// Restart advertising in this callback if there are pending requests after the connection is released.
120-
RestartAdvertising();
125+
const CHIP_ERROR result = RestartAdvertising();
126+
if (result != CHIP_NO_ERROR)
127+
{
128+
atomic_set(&sRestart, 1);
129+
}
121130
}
122131
});
123-
// Reset the disconnection flag to avoid restarting advertising multiple times
124-
sWasDisconnection = false;
125132
}
126133
},
127134
};

0 commit comments

Comments
 (0)