Skip to content

Commit c9b8e1a

Browse files
[zephyr] Added several improvements to the BLEMgr implementation (project-chip#33189)
* [zephyr] Enabled support for bonding in the BLEManager Currently the Zephyr BLE Manager rotates Bluetooth addresses on every boot and it does this by creating new Bluetooth identity. Because of that the Zephyr stack does not create default Bluetooth ID, which is required e.g. for the bonding purposes. Added creating two separate Bluetooth identities - for the Matter service advertising and for the bonding purposes. Signed-off-by: Kamil Kasperczyk <kamil.kasperczyk@nordicsemi.no> * [zephyr] Added check to drop handling callbacks for BT central Matter BLEManager handles all BT connect and disconnect callbacks no matter if these are Matter related ones or not. It collides with other not Matter-related services that trigger Matter CHIPoBLE service advertising changes. Added role check that allows to at least drop all callbacks related to BT central role. Signed-off-by: Kamil Kasperczyk <kamil.kasperczyk@nordicsemi.no> * [zephyr]: allow BLE advertising restarts * allow BLE advertising restarts in case of failures * which can be triggered by calling SetBLEAdvertisingEnabled(true) ConnectivityMgr public API from the application code * do not register CHIPoBLE GATT services when the advertising cannot be started * this allows the disconnection handler to filter out non-Matter BLE connections that were terminated * fix possible underflow of connection counters Signed-off-by: Marcin Kajor <marcin.kajor@nordicsemi.no> --------- Signed-off-by: Kamil Kasperczyk <kamil.kasperczyk@nordicsemi.no> Signed-off-by: Marcin Kajor <marcin.kajor@nordicsemi.no> Co-authored-by: Marcin Kajor <marcin.kajor@nordicsemi.no>
1 parent 41fa140 commit c9b8e1a

File tree

4 files changed

+191
-46
lines changed

4 files changed

+191
-46
lines changed

src/platform/Zephyr/BLEAdvertisingArbiter.cpp

+29-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ namespace {
2929
// List of advertising requests ordered by priority
3030
sys_slist_t sRequests;
3131

32+
bool sIsInitialized = false;
33+
uint8_t sBtId = 0;
34+
3235
// Cast an intrusive list node to the containing request object
3336
const BLEAdvertisingArbiter::Request & ToRequest(const sys_snode_t * node)
3437
{
@@ -55,8 +58,9 @@ CHIP_ERROR RestartAdvertising()
5558
ReturnErrorOnFailure(System::MapErrorZephyr(bt_le_adv_stop()));
5659
ReturnErrorCodeIf(sys_slist_is_empty(&sRequests), CHIP_NO_ERROR);
5760

58-
const Request & top = ToRequest(sys_slist_peek_head(&sRequests));
59-
const bt_le_adv_param params = BT_LE_ADV_PARAM_INIT(top.options, top.minInterval, top.maxInterval, nullptr);
61+
const Request & top = ToRequest(sys_slist_peek_head(&sRequests));
62+
bt_le_adv_param params = BT_LE_ADV_PARAM_INIT(top.options, top.minInterval, top.maxInterval, nullptr);
63+
params.id = sBtId;
6064
const int result = bt_le_adv_start(&params, top.advertisingData.data(), top.advertisingData.size(), top.scanResponseData.data(),
6165
top.scanResponseData.size());
6266

@@ -70,8 +74,26 @@ CHIP_ERROR RestartAdvertising()
7074

7175
} // namespace
7276

77+
CHIP_ERROR Init(uint8_t btId)
78+
{
79+
if (sIsInitialized)
80+
{
81+
return CHIP_ERROR_INCORRECT_STATE;
82+
}
83+
84+
sBtId = btId;
85+
sIsInitialized = true;
86+
87+
return CHIP_NO_ERROR;
88+
}
89+
7390
CHIP_ERROR InsertRequest(Request & request)
7491
{
92+
if (!sIsInitialized)
93+
{
94+
return CHIP_ERROR_INCORRECT_STATE;
95+
}
96+
7597
CancelRequest(request);
7698

7799
sys_snode_t * prev = nullptr;
@@ -109,6 +131,11 @@ CHIP_ERROR InsertRequest(Request & request)
109131

110132
void CancelRequest(Request & request)
111133
{
134+
if (!sIsInitialized)
135+
{
136+
return;
137+
}
138+
112139
const bool isTopPriority = (sys_slist_peek_head(&sRequests) == &request);
113140
VerifyOrReturn(sys_slist_find_and_remove(&sRequests, &request));
114141

src/platform/Zephyr/BLEAdvertisingArbiter.h

+18
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ struct Request : public sys_snode_t
6161
OnAdvertisingStopped onStopped; ///< (Optional) Callback invoked when the request stops being top-priority.
6262
};
6363

64+
/**
65+
* @brief Initialize BLE advertising arbiter
66+
*
67+
* @note This method must be called before trying to insert or cancel any requests.
68+
*
69+
* @param btId Local Bluetooth LE identifier to be used for the advertising parameters. Currently Bluetooth LE identifier used in
70+
* this method will be used for all advertising requests and changing it dynamically is not supported.
71+
* @return error If the module is already initialized.
72+
* @return success Otherwise.
73+
*/
74+
CHIP_ERROR Init(uint8_t btId);
75+
6476
/**
6577
* @brief Request BLE advertising
6678
*
@@ -74,6 +86,9 @@ struct Request : public sys_snode_t
7486
* @note This method does not take ownership of the request object so the object
7587
* must not get destroyed before it is cancelled.
7688
*
89+
* @note The arbiter module has to be initialized using Init() method before
90+
* invoking this method.
91+
*
7792
* @param request Reference to advertising request that contains priority and
7893
* other advertising parameters.
7994
* @return error If the request is top-priority and failed to restart the
@@ -94,6 +109,9 @@ CHIP_ERROR InsertRequest(Request & request);
94109
* An attempt to cancel a request that has not been registered at the
95110
* advertising arbiter is a no-op. That is, it returns immediately.
96111
*
112+
* @note The arbiter module has to be initialized using Init() method before
113+
* invoking this method.
114+
*
97115
* @param request Reference to advertising request that contains priority and
98116
* other advertising parameters.
99117
*/

0 commit comments

Comments
 (0)