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

Bluetooth: Mesh: Use BT_MESH_SUSPENDED flag in the legacy advertiser thread #80627

Merged
merged 2 commits into from
Nov 5, 2024

Conversation

PavelVPV
Copy link
Collaborator

@PavelVPV PavelVPV commented Oct 30, 2024

Instead of checking the enabled flag, check if BT_MESH_SUSPENDED is set in the legacy advertiser thread. BT_MESH_SUSPENDED is set earlier than advertiser is stopped and will prevent the advertiser send anything earlier.

This PR also replaces k_sleep with semaphore before suspending mesh as k_sleep may not be enough to let advertiser send the message. Instead we should rely on the bt_mesh_send_cb.

This PR fixes mesh suspend tests in #79931
Fixes #80910

@PavelVPV
Copy link
Collaborator Author

@valeriosetti, this PR should fix suspend tests in your PR.

/* The advertiser thread relies on BT_MESH_SUSPENDED flag. No point in starting the
* advertiser thread if the flag is not set.
*/
__ASSERT_NO_MSG(ADV_ENABLED);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why assert here (considering asserts can be disabled), may be better to return -EADV?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ever use asserts if they can be disabled?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the main purpose of assertion in assertion post action that is weak in Zephyr and customer is able to implement it on its own purpose. Generally, assertion is changed on hardware reset in final product as the only way for recovering device workability from unknown and unpredictable situation.
https://docs.zephyrproject.org/latest/kernel/services/other/fatal.html

Is this the real place where device requires hardware reset in "fields"?

It seems @omkar3141 suggestion is more suitable. Since functional prototype returns operation status, it is better to return error code.

* thread will exit once the flag is set. The flag is set by the higher layer function. Here
* we need to check that the flag is dropped and ensure that the thread is stopped.
*/
__ASSERT_NO_MSG(!ADV_ENABLED);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 'ensure' works only when asserts are enabled, otherwise this can cause locking, better to return error code to let higher layer handle it.

alxelax
alxelax previously approved these changes Oct 31, 2024
omkar3141
omkar3141 previously approved these changes Oct 31, 2024
@@ -35,10 +35,11 @@ LOG_MODULE_REGISTER(bt_mesh_adv_legacy);
#define ADV_INT_DEFAULT_MS 100
#define ADV_INT_FAST_MS 20

#define ADV_ENABLED (!atomic_test_bit(bt_mesh.flags, BT_MESH_SUSPENDED))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to add an input parameter to this macro (either bt_mesh or bt_mesh.flags). Without it you get the impression that this macro evaluates to a build-time constant, which is not true.

Instead of checking the `enabled` flag, check if BT_MESH_SUSPENDED is
set in the legacy advertiser thread. BT_MESH_SUSPENDED is set earlier
than advertiser is stopped and will prevent the advertiser send anything
earlier.

Signed-off-by: Pavel Vasilyev <[email protected]>
k_sleep may not be enough to let advertiser send the message. Instead we
should rely on the bt_mesh_send_cb.

Signed-off-by: Pavel Vasilyev <[email protected]>
@PavelVPV PavelVPV added this to the v4.0.0 milestone Nov 5, 2024
@mmahadevan108 mmahadevan108 merged commit 457a20c into zephyrproject-rtos:main Nov 5, 2024
28 checks passed
@PavelVPV PavelVPV deleted the fix_legacy_adv_stop branch November 7, 2024 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bluetooth: Mesh: Legacy advertiser may advertise after stack is suspended before advertiser is finally stopped
6 participants