Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[zephyr] Added several improvements to the BLEMgr implementation #33189

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading