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

backends/characteristic: make max_write_without_response_size dynamic #1586

Merged
merged 2 commits into from
Jun 1, 2024

Conversation

dlech
Copy link
Collaborator

@dlech dlech commented Jun 1, 2024

It has been observed that the max MTU exchange may not be complete before the connection is established, at least on Windows.

This reverts the previous attempt to work around this on Windows and instead makes the max_write_without_response_size dynamic. This way users can implement a workaround if needed but users who don't need it won't be punished with a longer connection time. The timeout in the previous workaround was also too short for some devices so it wan't complexly fixing the issue.

@dlech
Copy link
Collaborator Author

dlech commented Jun 1, 2024

Hi @JPHutchins, this is a different approach to trying to fix your issue.

It is now up to the caller to opt in to the workaround by adding the following code after connection:

            async with asyncio.timeout(10):
                while char.max_write_without_response_size == 20:
                    await asyncio.sleep(0.5)

I feel like this is better because you could still do other things with the device at the same time while waiting for this. And you can make the timeout as long or short as you want.

@JPHutchins
Copy link
Contributor

Great! I think that my main question is whether or not it's always OK to use MTU property on Windows and Mac? On Linux we are using the _acquire_mtu() method and it seems OK.

Should I use the MTU property or the max write without response size for reliable MTU?

@dlech
Copy link
Collaborator Author

dlech commented Jun 1, 2024

max_write_without_response_size should be working on all OSes as long as you meet the minimum BlueZ version requirement stated in the docs. But the device-level property should be fine to use for the foreseeable future as well.

It has been observed that the max MTU exchange may not be complete
before the connection is established, at least on Windows.

This reverts the previous attempt to work around this on Windows and
instead makes the max_write_without_response_size dynamic. This way
users can implement a workaround if needed but users who don't need it
won't be punished with a longer connection time. The timeout in the
previous workaround was also too short for some devices so it wan't
complexly fixing the issue.
@dlech dlech force-pushed the max-write-without-response-size-rework branch from b9e722d to d89ae79 Compare June 1, 2024 18:44
@JPHutchins
Copy link
Contributor

But the device-level property should be fine to use for the foreseeable future as well.

OK! Is it correct that the device-level MTU is valid as soon as the device is "connected", whereas we may have to await the arrival of the max_write_without_response_size?

CHANGELOG.rst Outdated
@@ -14,6 +14,7 @@ Changed
-------
* Retrieve the BLE address required by ``BleakClientWinRT`` from scan response if advertising is None (WinRT).
* Changed type hint for ``adv`` attribute of ``bleak.backends.winrt.scanner._RawAdvData``.
* ``BleakGATTCharacteristic.max_write_without_response_size`` is is now dynamic.
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo: "is is" -> "is"

Copy link
Contributor

Choose a reason for hiding this comment

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

I am interpreting "dynamic" to mean that this property may be mutated asynchronously.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That is the idea.

self.__mtu - 3,
lambda: self.__mtu - 3,
Copy link
Contributor

Choose a reason for hiding this comment

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

Off topic, but it reminds me that it would be nice get rid of name-mangling, IMO. I guess it's only the android client though.

@dlech
Copy link
Collaborator Author

dlech commented Jun 1, 2024

Is it correct that the device-level MTU is valid as soon as the device is "connected", whereas we may have to await the arrival of the max_write_without_response_size?

No, the information is coming from the same place max_write_without_response_size is just mtu_size - 3.

CHANGELOG.rst Outdated Show resolved Hide resolved
@dlech dlech merged commit 425abb3 into develop Jun 1, 2024
14 checks passed
@dlech dlech deleted the max-write-without-response-size-rework branch June 1, 2024 20:21
@dlech
Copy link
Collaborator Author

dlech commented Jun 1, 2024

Thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants