-
Notifications
You must be signed in to change notification settings - Fork 295
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
Adding mtu_size client #525
Conversation
Could you also add this function for the other backends? |
@dlech I added support for the other three backends. A few notes:
1b) If I get to test on Windows, how do I test the two different win backends?
|
Thanks for the effort! I think for Windows, right now the backend depends on the Python runtime version, so if you don't want to install different versions of Python, you probably just have to edit Bleak to be sure to load the one you want. On BlueZ, I agree this is probably the best we can do. Instead of making the user have to provide a characteristic, would it be possible to go through all of the cached characteristics and chose the "best" one? I think the "best" one would be one where we can call Also, we should close the file descriptor returned by these D-Bus methods, otherwise it will probably block other operations. It would also be good to cache the returned MTU value on the BlueZ backend so that we only have to call the complex lookup once per connection. |
Another idea: Make this a property instead of an async method. On Windows and Mac, this property will just work. On Linux, this property will return an error unless the user first calls a special method to fetch and cache the MTU value. This way, the user has full control over which characteristic to use (and possibly a choice of using AquireWrite or AquireNotify). Then if BlueZ ever adds a proper MTU property, we won't be stuck with an unnecessary async method in Bleak. |
Thanks for the feedback! I still need to run some tests, but here for early review.
To be tested:
|
Pushed another round of changes. The only option we have is to let the user call the method manually (now called discover_mtu_size, to distinguish from "get"), passing the UUID of a characteristic and the preferred property to use for the discovery. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. Just one minor comment.
It would be nice to have an example program too, but we can save that for a separate PR.
bleak/backends/bluezdbus/client.py
Outdated
elif char_property is "write-without-response": | ||
member = "AcquireWrite" | ||
else: | ||
raise BleakError( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be ValueError
rather than BleakError
.
I also just noticed that we are setting |
Good catch. I tried with Also, things aren't looking great for If those can't be easily addressed, I'll probably revert the BlueZ changes, and go back to the simple MacOS and Windows only solution (still pending windows testing). |
I would be happy with BlueZ just returning 23 for now along with logging a warning that it is not properly implemented. Then we can look at fixing dbus-next and/or adding an mtu property to BlueZ to eventually get this working right. |
thanks! I just pushed the simplified implementation for bluez. I'll test on windows and then this should be good to go |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After making the suggested change (and merging the fix from #537), the winrt backend works.
bleak/backends/bluezdbus/client.py
Outdated
@@ -570,6 +570,14 @@ def is_connected(self) -> bool: | |||
False if self._bus is None else self._properties.get("Connected", False) | |||
) | |||
|
|||
@property | |||
def mtu_size(self) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return type is int
, not bool
@@ -125,6 +125,12 @@ def is_connected(self) -> bool: | |||
False if manager is None else manager.isConnected | |||
) | |||
|
|||
@property | |||
def mtu_size(self) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return type should be int
bleak/backends/dotnet/client.py
Outdated
@@ -305,6 +305,11 @@ def is_connected(self) -> bool: | |||
else self._requester.ConnectionStatus == BluetoothConnectionStatus.Connected | |||
) | |||
|
|||
@property | |||
def mtu_size(self) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return type should be int
bleak/backends/winrt/client.py
Outdated
@@ -280,6 +280,11 @@ def is_connected(self) -> bool: | |||
== BluetoothConnectionStatus.CONNECTED | |||
) | |||
|
|||
@property | |||
def mtu_size(self) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return type should be int
bleak/backends/winrt/client.py
Outdated
@property | ||
def mtu_size(self) -> bool: | ||
"""Get ATT MTU size for active connection""" | ||
return self._session.MaxPduSize |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the winrt API uses lower_snake_case, so max_pdu_size
here
Hmm.... I'm playing with this on Windows more and I just got the wrong MTU (517 instead of 23 as expected for the device I'm using). So I guess that Windows has a MaxPduSizeChanged event but I'm not sure that I would trust it to always be called when a device is connected and to only be called once. If it did work this way, we could wait for the event when connecting. |
After some more experimentation, it seems that the device isn't connecting properly or is becoming disconnected before I read the mtu, so I think we can ignore my previous comment. UPDATE: Doh, I had another BLE device advertising that I didn't know was on. 🤦 |
thanks for the feedback and for testing on Windows! |
I have confirmed that MaxPduSize does return the actual MTU. |
thanks for the approve! I don't see a merge button ("Only those with write access to this repository can merge pull requests."), should someone else merge this? |
merged, thanks! |
Adding a simple method to the macOS client to get the current ATT MTU size.
The ATT MTU size includes the ATT header, while CoreBluetooth returns the maximum ATT payload size, hence the +3 in the getMtuSize method.