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: fix GATT service reregistering #82062

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

akredalen
Copy link
Collaborator

@akredalen akredalen commented Nov 26, 2024

  • Fixed issue with reregistering of a GATT services.
  • Added unit tests covering the GATT reregistering scenario.

Fixes #67377

Copy link
Collaborator

@PavelVPV PavelVPV left a comment

Choose a reason for hiding this comment

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

Please add Fixes #67377 to the commit message.

include/zephyr/bluetooth/gatt.h Outdated Show resolved Hide resolved

for (int i = 0; i < 10; i++) {

/* Attempt to register service A */
Copy link
Collaborator

Choose a reason for hiding this comment

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

(here and below) This comment is the same as the function name. Better to write the test procedure before the test function. Same for the other new tests.

"Test service A registration failed");

/* Attempt to unregister service A */
zassert_false(bt_gatt_service_unregister(&local_test_svc),
Copy link
Collaborator

Choose a reason for hiding this comment

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

The registration function may at some point be rewritten in such a way that the test still passes, but the handles are not reset. Therefore it is better to check that all handles are zero-ed after unregistering the service. This will guarantee that we get exactly what we want.

Copy link
Collaborator Author

@akredalen akredalen Nov 27, 2024

Choose a reason for hiding this comment

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

I removed this check because I thought it was a test for the unregister function, which should probably not be done inside this test. Should I still add the check here or should this be added to the test of the unregister function instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I think it makes sense to check handles here.

"Test service A registration failed");

/* Attempt to unregister service A */
zassert_false(bt_gatt_service_unregister(&local_test_svc),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment as above, but here (and below) we should check that the handles are the same after unregistering as they were before registering the service. Otherwise, if it is zero-ed, the service will still be registered, but this won't be what we expect.

Comment on lines 268 to 269
/* Pre-allocate handles for service A */
for (int j = 0; j < local_test_svc.attr_count; j++) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is better to name both services such that it is clear which one has pre-allocated handles and which one will get handles by the host.

Comment on lines 278 to 309
zassert_false(bt_gatt_service_unregister(&local_test_svc),
"Test service A unregister failed");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as two comments above: check handles after calling bt_gatt_service_unregister.

@jhedberg
Copy link
Member

Please add Fixes #67377 to the commit message.

That should be in the PR description, not necessarily in the commit message (in fact, references like that in commit messages loose their meaning if the project ever moves to a different location or if someone looks at the commit history in some fork of upstream).

@PavelVPV
Copy link
Collaborator

Please add Fixes #67377 to the commit message.

That should be in the PR description, not necessarily in the commit message (in fact, references like that in commit messages loose their meaning if the project ever moves to a different location or if someone looks at the commit history in some fork of upstream).

Ok, I just find it useful to myself at least when going through the git history through git log.

@jhedberg
Copy link
Member

Ok, I just find it useful to myself at least when going through the git history through git log.

Sure, which is why I wasn't trying to outright forbid it. However, GitHub doesn't look at commit messages when it links PRs and issues together, which is why it's important that the reference is at least in the PR description (a contributor might forget to update that if they just modify the commit message and push).

@PavelVPV
Copy link
Collaborator

Ok, I just find it useful to myself at least when going through the git history through git log.

Sure, which is why I wasn't trying to outright forbid it. However, GitHub doesn't look at commit messages when it links PRs and issues together, which is why it's important that the reference is at least in the PR description (a contributor might forget to update that if they just modify the commit message and push).

I see. Usually, when PR has a single commit, it content is put into the description. But yes, you are right, this should be in the PR description for GitHub to match the PR with the issue.

@akredalen akredalen force-pushed the fix_rereg_gatt_service branch 2 times, most recently from 150f1a9 to 6e66a3b Compare November 27, 2024 16:40
Comment on lines 305 to 306
uint16_t perm: 15;
uint16_t _auto_assigned_handle: 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

@jhedberg correct me if I'm wrong, but I think we want to avoid bitfields in the public header? And prefer to use macros to get and set bits instead.

IIRC the reasoning is that these type of bitfields may not work well with non-arm architectures, but unsure if we actually have that in writing anywhere in Zephyr

Copy link
Member

Choose a reason for hiding this comment

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

@jhedberg correct me if I'm wrong, but I think we want to avoid bitfields in the public header? And prefer to use macros to get and set bits instead.

That would be the case for any packed structs which are supposed to map to some canonical protocol binary representation. I think in this case that's not the case, i.e. nothing depends on a specific in-memory layout of these struct members.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay. I'm pretty sure I've been asked to not use bitfields in the public header files, even for non-packed structs several times, and mostly for performance reasons since some architectures do not support it.

Anyways, since we do not (AFAIK) have any coding guidelines on this, it's OK as is :)

@akredalen akredalen force-pushed the fix_rereg_gatt_service branch 2 times, most recently from 5d27478 to bf1660a Compare November 28, 2024 12:26
Comment on lines 300 to 303
* @note The `_auto_assigned_handle` flag in this bitfield indicates if the attribute
* handle was automatically assigned by the stack (1) or manually set by the
* application (0).
* This flag must not be modified by the application.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be moved to a separate Doxygen comment for _auto_assigned_handle, and should include @internal.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@internal cannot be used to hide struct members from documentation. @cond INTERNAL_HIDDEN needs to be used for that purpose

Copy link
Collaborator

@alwa-nordic alwa-nordic Nov 29, 2024

Choose a reason for hiding this comment

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

I played around with this. It seems Doxygen will hide members that being with an underscore.

@akredalen, Since this field starts with an underscore, it's already private and there is no need to add anything. Sorry for the confusion.

@akredalen akredalen force-pushed the fix_rereg_gatt_service branch 2 times, most recently from 2a37cc3 to 1157688 Compare November 29, 2024 11:50
tests/bluetooth/gatt/src/main.c Outdated Show resolved Hide resolved
tests/bluetooth/gatt/src/main.c Show resolved Hide resolved
* Fixed issue with reregistering of GATT services.
* Added unit tests covering the GATT reregistering scenario.

Signed-off-by: Stine Åkredalen <[email protected]>
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: GATT: Unable to re-register GATT service
6 participants