ubluetooth: ability to set MTU after connecting to peripheral device?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
pyrocket
Posts: 3
Joined: Thu Mar 26, 2020 6:24 pm

ubluetooth: ability to set MTU after connecting to peripheral device?

Post by pyrocket » Thu Mar 26, 2020 6:42 pm

New to micropython & the ESP32, somewhat familiar with BLE. Using the ubluetooth library on the ESP32, I've been able to successfully gap_scan, gap_connect and gattc_read (mostly). However, on a read, the full payload I am expecting is larger than the default MTU. I found that the 'rxbuf' can be configured and tried setting that to a few different values up to 517 (the max if I understand correctly), but this didn't change the data returned.

I have been able to successfully connect & read from this other peripheral using the nRF Connect app on Android, but with the same results until I configured a larger MTU size. As I understand, this actually triggers a MTU exchange request to the gatt server on the peripheral, which then responds with a MTU exchange response.

Is there a way to exchange a new MTU in micropython?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ubluetooth: ability to set MTU after connecting to peripheral device?

Post by jimmo » Thu Mar 26, 2020 11:48 pm

Unfortunately right now there's no Python-level API for this. It's on the TODO list: https://github.com/micropython/micropython/issues/5186

If you're happy to build your own firmware, then you can modify MP_BLUETOOTH_DEFAULT_ATTR_LEN in extmod/modbluetooth.h or modify the call to ble_att_set_preferred_mtu in extmod/nimble/modbluetooth_nimble.c

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ubluetooth: ability to set MTU after connecting to peripheral device?

Post by jimmo » Thu Mar 26, 2020 11:49 pm

If you have thoughts on how this should work, please comment on that bug.

Some ideas:
- If you modify hte rxbuf size, it should also implicitly set the mtu to be at least that big?
- Add an explicit "set MTU" function to the BLE type.

pyrocket
Posts: 3
Joined: Thu Mar 26, 2020 6:24 pm

Re: ubluetooth: ability to set MTU after connecting to peripheral device?

Post by pyrocket » Fri Mar 27, 2020 4:37 pm

Thanks for the response @jimmo. Might be fun to give building MP myself a try, I'll post up if I have any success.

If I get into the weeds enough to have any worthwhile suggestions I'll definitely add a comment. Without being too familiar with the internals, as a user of the API I would either prefer an explicit function or perhaps a MTU arg on the gap_connect function, which would immediately set the MTU on successful connection. The arg 'rxbuf' isn't readily apparent as equivalent to MTU to somebody new to the scene.

pyrocket
Posts: 3
Joined: Thu Mar 26, 2020 6:24 pm

Re: ubluetooth: ability to set MTU after connecting to peripheral device?

Post by pyrocket » Fri Apr 03, 2020 1:01 pm

I was able to build MP myself successfully and flash it to the esp32, so I then took a shot at modifying the constant specified above, but it didn't appear to have any effect. I also tried changing the call to ble_att_set_preferred_mtu to a few different values without any apparent change in behavior. I made sure to clean and make again each time, and even tried a bogus value to ensure my changes were being picked up. I haven't dug any further to see if I can determine if the MTU request is actually being sent/acknowledged yet.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ubluetooth: ability to set MTU after connecting to peripheral device?

Post by jimmo » Tue Apr 14, 2020 6:52 am

pyrocket wrote:
Fri Apr 03, 2020 1:01 pm
I haven't dug any further to see if I can determine if the MTU request is actually being sent/acknowledged yet.
OK, interesting. I'll hopefully get around to this in the next few weeks (but lots of other priorities too), so keep an eye on https://github.com/micropython/micropython/issues/5186

Post Reply