Skip to content

Commit 93c7b09

Browse files
[nrf toup][Zephyr] Retry starting BLE advertising after releasing connection.
In Zephyr 4.0, the BT_LE_ADV_OPT_CONNECTABLE and BT_LE_ADV_OPT_CONNECTABLE advertising options have been deprecated and we need to use BT_LE_ADV_OPT_CONN instead. Due to the fact that the automatic advertiser resumption is deprecated, we must ensure that the BLE advertising arbiter restarts advertising in a different way if the service could not be started, for example, due to an existing BLE connection, or connection has been closed. We can use the recycled callback of the BT_CONN_CB to check if there is pending request in the list and if so, try to restart advertising. Signed-off-by: Arkadiusz Balys <arkadiusz.balys@nordicsemi.no>
1 parent c22e808 commit 93c7b09

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/platform/Zephyr/BLEAdvertisingArbiter.cpp

+32-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
#include <lib/support/CodeUtils.h>
2121
#include <lib/support/logging/CHIPLogging.h>
22+
#include <platform/CHIPDeviceLayer.h>
2223
#include <system/SystemError.h>
24+
#include <zephyr/bluetooth/conn.h>
2325

2426
namespace chip {
2527
namespace DeviceLayer {
@@ -29,8 +31,9 @@ namespace {
2931
// List of advertising requests ordered by priority
3032
sys_slist_t sRequests;
3133

32-
bool sIsInitialized = false;
33-
uint8_t sBtId = 0;
34+
bool sIsInitialized = false;
35+
bool sWasDisconnection = false;
36+
uint8_t sBtId = 0;
3437

3538
// Cast an intrusive list node to the containing request object
3639
const BLEAdvertisingArbiter::Request & ToRequest(const sys_snode_t * node)
@@ -64,6 +67,11 @@ CHIP_ERROR RestartAdvertising()
6467
const int result = bt_le_adv_start(&params, top.advertisingData.data(), top.advertisingData.size(), top.scanResponseData.data(),
6568
top.scanResponseData.size());
6669

70+
if (result == -ENOMEM)
71+
{
72+
ChipLogProgress(DeviceLayer, "Advertising start failed, will retry once connection is released");
73+
}
74+
6775
if (top.onStarted != nullptr)
6876
{
6977
top.onStarted(result);
@@ -72,6 +80,28 @@ CHIP_ERROR RestartAdvertising()
7280
return System::MapErrorZephyr(result);
7381
}
7482

83+
BT_CONN_CB_DEFINE(conn_callbacks) = {
84+
.disconnected = [](struct bt_conn * conn, uint8_t reason) { sWasDisconnection = true; },
85+
.recycled =
86+
[]() {
87+
// In this callback the connection object was returned to the pool and we can try to re-start connectable
88+
// advertising, but only if the disconnection was detected.
89+
if (sWasDisconnection)
90+
{
91+
SystemLayer().ScheduleLambda([] {
92+
if (!sys_slist_is_empty(&sRequests))
93+
{
94+
// Starting from Zephyr 4.0 Automatic advertiser resumption is deprecated,
95+
// so the BLE Advertising Arbiter has to take over the responsibility of restarting the advertiser.
96+
// Restart advertising in this callback if there are pending requests after the connection is released.
97+
RestartAdvertising();
98+
}
99+
});
100+
// Reset the disconnection flag to avoid restarting advertising multiple times
101+
sWasDisconnection = false;
102+
}
103+
},
104+
};
75105
} // namespace
76106

77107
CHIP_ERROR Init(uint8_t btId)

src/platform/Zephyr/BLEManagerImpl.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <zephyr/random/random.h>
4545
#include <zephyr/sys/byteorder.h>
4646
#include <zephyr/sys/util.h>
47+
#include <zephyr/version.h>
4748

4849
#ifdef CONFIG_BT_BONDABLE
4950
#include <zephyr/settings/settings.h>
@@ -61,8 +62,14 @@ namespace Internal {
6162

6263
namespace {
6364

65+
#if KERNEL_VERSION_MAJOR >= 4
66+
// In Zephyr 4.0 BT_LE_ADV_OPT_CONNECTABLE and BT_LE_ADV_OPT_ONE_TIME have been deprecated
67+
// and replaced with BT_LE_ADV_OPT_CONN.
68+
constexpr uint32_t kAdvertisingOptions = BT_LE_ADV_OPT_CONN;
69+
#else
6470
constexpr uint32_t kAdvertisingOptions = BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_ONE_TIME;
65-
constexpr uint8_t kAdvertisingFlags = BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR;
71+
#endif
72+
constexpr uint8_t kAdvertisingFlags = BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR;
6673

6774
const bt_uuid_128 UUID128_CHIPoBLEChar_RX =
6875
BT_UUID_INIT_128(0x11, 0x9D, 0x9F, 0x42, 0x9C, 0x4F, 0x9F, 0x95, 0x59, 0x45, 0x3D, 0x26, 0xF5, 0x2E, 0xEE, 0x18);

0 commit comments

Comments
 (0)