Skip to content

Commit

Permalink
[zephyr] Added several improvements to the BLEMgr implementation (pro…
Browse files Browse the repository at this point in the history
…ject-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 <[email protected]>

* [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 <[email protected]>

* [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 <[email protected]>

---------

Signed-off-by: Kamil Kasperczyk <[email protected]>
Signed-off-by: Marcin Kajor <[email protected]>
Co-authored-by: Marcin Kajor <[email protected]>
  • Loading branch information
kkasperczyk-no and markaj-nordic committed May 8, 2024
1 parent c2811d8 commit 2211869
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 46 deletions.
31 changes: 29 additions & 2 deletions src/platform/Zephyr/BLEAdvertisingArbiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ namespace {
// List of advertising requests ordered by priority
sys_slist_t sRequests;

bool sIsInitialized = false;
uint8_t sBtId = 0;

// Cast an intrusive list node to the containing request object
const BLEAdvertisingArbiter::Request & ToRequest(const sys_snode_t * node)
{
Expand All @@ -55,8 +58,9 @@ CHIP_ERROR RestartAdvertising()
ReturnErrorOnFailure(System::MapErrorZephyr(bt_le_adv_stop()));
ReturnErrorCodeIf(sys_slist_is_empty(&sRequests), CHIP_NO_ERROR);

const Request & top = ToRequest(sys_slist_peek_head(&sRequests));
const bt_le_adv_param params = BT_LE_ADV_PARAM_INIT(top.options, top.minInterval, top.maxInterval, nullptr);
const Request & top = ToRequest(sys_slist_peek_head(&sRequests));
bt_le_adv_param params = BT_LE_ADV_PARAM_INIT(top.options, top.minInterval, top.maxInterval, nullptr);
params.id = sBtId;
const int result = bt_le_adv_start(&params, top.advertisingData.data(), top.advertisingData.size(), top.scanResponseData.data(),
top.scanResponseData.size());

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

} // namespace

CHIP_ERROR Init(uint8_t btId)
{
if (sIsInitialized)
{
return CHIP_ERROR_INCORRECT_STATE;
}

sBtId = btId;
sIsInitialized = true;

return CHIP_NO_ERROR;
}

CHIP_ERROR InsertRequest(Request & request)
{
if (!sIsInitialized)
{
return CHIP_ERROR_INCORRECT_STATE;
}

CancelRequest(request);

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

void CancelRequest(Request & request)
{
if (!sIsInitialized)
{
return;
}

const bool isTopPriority = (sys_slist_peek_head(&sRequests) == &request);
VerifyOrReturn(sys_slist_find_and_remove(&sRequests, &request));

Expand Down
18 changes: 18 additions & 0 deletions src/platform/Zephyr/BLEAdvertisingArbiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ struct Request : public sys_snode_t
OnAdvertisingStopped onStopped; ///< (Optional) Callback invoked when the request stops being top-priority.
};

/**
* @brief Initialize BLE advertising arbiter
*
* @note This method must be called before trying to insert or cancel any requests.
*
* @param btId Local Bluetooth LE identifier to be used for the advertising parameters. Currently Bluetooth LE identifier used in
* this method will be used for all advertising requests and changing it dynamically is not supported.
* @return error If the module is already initialized.
* @return success Otherwise.
*/
CHIP_ERROR Init(uint8_t btId);

/**
* @brief Request BLE advertising
*
Expand All @@ -74,6 +86,9 @@ struct Request : public sys_snode_t
* @note This method does not take ownership of the request object so the object
* must not get destroyed before it is cancelled.
*
* @note The arbiter module has to be initialized using Init() method before
* invoking this method.
*
* @param request Reference to advertising request that contains priority and
* other advertising parameters.
* @return error If the request is top-priority and failed to restart the
Expand All @@ -94,6 +109,9 @@ CHIP_ERROR InsertRequest(Request & request);
* An attempt to cancel a request that has not been registered at the
* advertising arbiter is a no-op. That is, it returns immediately.
*
* @note The arbiter module has to be initialized using Init() method before
* invoking this method.
*
* @param request Reference to advertising request that contains priority and
* other advertising parameters.
*/
Expand Down
Loading

0 comments on commit 2211869

Please sign in to comment.