bluetooth icon indicating copy to clipboard operation
bluetooth copied to clipboard

{darwin,linux,windows,sd}: add get mtu function

Open jagobagascon opened this issue 3 years ago • 6 comments

This PR adds a function to retrieve the MTU of a characteristic on Darwin, Linux, Windows and SoftDevice.

Implementation details

I tried to keep the same signature on both implementations. So I had to adapt the Darwin implementation a little bit to match the requirements of linux:

  • In Linux (bluez) we read the MTU property in the characteristic. So I did the same in other platforms, even though we read the MTU from the device.
  • Since in Linux reading the MTU can fail, I added the same return type ((uint16, error)) on other platforms, even though it cannot fail.

Regarding Linux implementation, the MTU property is quite recent (bluez 5.62), so it is going to fail to anyone using an older version of bluez. I though about calling AcquireWrite as a fallback. This method returns a file descriptor to write messages and its MTU, and we could just call it and immediately close the file descriptor to get the MTU. But it felt ugly, and hacky, and I'm not sure if it has any other implications.

jagobagascon avatar May 18 '22 10:05 jagobagascon

It would be good to add a similar implementation for SoftDevice. It could probably look something like this completed untested bit of code:

func (c DeviceCharacteristic) GetMTU() (uint16, error) {
	return uint16(C.BLE_GATT_ATT_MTU_DEFAULT), nil
}

@aykevl any thoughts here?

deadprogram avatar May 28 '22 08:05 deadprogram

Hi @deadprogram. I'm open to add the implementation for SoftDevice myself, but I have no means for testing it.

jagobagascon avatar Aug 29 '22 09:08 jagobagascon

I'm open to add the implementation for SoftDevice myself, but I have no means for testing it.

I can help with that, certainly. Thanks!

deadprogram avatar Aug 29 '22 17:08 deadprogram

In Linux (bluez) we read the MTU property in the characteristic.

That's kind of odd. I don't understand why BlueZ does that (but then again, I don't understand a lot of the design choices in BlueZ...). I think having the MTU on the device object makes a lot more sense, because it is a property of the connection, not of the characteristic.

Not sure how to deal with this. Ideally I would like to see this property on the Device object, but I'm not sure whether it is possible to do that on BlueZ/Linux.

(Sidenote: kind of wishing for a pulseaudio/pipewire style rewrite of BlueZ, it's a bit of a mess right now).

aykevl avatar Sep 08 '22 14:09 aykevl

I added the implementation for SoftDevice (although I could not test it), so let me know if it requires any change.

The only platform missing is Windows right now. So we could merge this as it is and add the GetMTU function with the Windows implementation. Or just wait for the Windows implementation to be ready and include the GetMTU function on this PR. Either works for me.

jagobagascon avatar Sep 13 '22 10:09 jagobagascon

I just included the GetMTU function for windows. So this PR already contains a way of getting the mtu for all platforms 😄 .

jagobagascon avatar Sep 15 '22 14:09 jagobagascon

I just fixed the conflicts on this PR and rebased the branch to the latest commit of dev. If there's anything I can do to push this PR forward please let me know 😊 .

jagobagascon avatar Oct 19 '22 09:10 jagobagascon

I think it is time to merge this. It may not have the MTU function in the place we would want it, but there is not much we can easily do about that with BlueZ etc.

Thank you for all the work on this @jagobagascon

deadprogram avatar Oct 19 '22 18:10 deadprogram