diff options
| author | Damien George <damien@micropython.org> | 2025-07-20 22:01:54 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2025-07-23 12:53:28 +1000 |
| commit | c6423d5d8e47ee0dc6b60fe150b4a2b540116940 (patch) | |
| tree | f3e6b85712b1f37a64285df022dcd939cd16f933 | |
| parent | 10ef3e4ac2767b68f746f0ebb995672076eee67d (diff) | |
tests/multi_bluetooth: Synchronise MTU exchange in BLE MTU tests.
With the recent update to ESP-IDF 5.4.2, there is a change in BLE event
behaviour which makes `tests/multi_bluetooth/ble_mtu.py` and
`tests/multi_bluetooth/ble_mtu_peripheral.py` now fail on ESP32 with IDF
5.4.2.
The change in behaviour is that MTU_EXCHANGE events can now occur before
CENTRAL_CONNECT/PERIPHERAL_CONNECT events. That seems a bit strange,
because the MTU exchange occurs after the connection. And looking at the
timing of the events there is exactly 100ms between them, ie MTU_EXCHANGE
fires and then exactly 100ms later CENTRAL_CONNECT/PERIPHERAL_CONNECT
fires.
It's unknown if this is a bug in (Espressif's) NimBLE, a subtle change in
scheduling with still valid behaviour, an intended change, a change allowed
under the BLE spec, or something else.
But in order to move forward with updating to IDF 5.4.2, the relevant tests
have been adjusted so they can pass. The test just needs to wait a bit
between doing the connect and doing the MTU exchange, so the other side
sees the original/correct ordering of events. This wait is done using the
multitest synchronisation primitives (broadcast and wait).
Signed-off-by: Damien George <damien@micropython.org>
| -rw-r--r-- | tests/multi_bluetooth/ble_mtu.py | 10 | ||||
| -rw-r--r-- | tests/multi_bluetooth/ble_mtu_peripheral.py | 10 |
2 files changed, 20 insertions, 0 deletions
diff --git a/tests/multi_bluetooth/ble_mtu.py b/tests/multi_bluetooth/ble_mtu.py index 5f00b270c..92a40fbb4 100644 --- a/tests/multi_bluetooth/ble_mtu.py +++ b/tests/multi_bluetooth/ble_mtu.py @@ -106,6 +106,9 @@ def instance0(): # Wait for central to connect to us. conn_handle = wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) + # Inform the central that we have been connected to. + multitest.broadcast("peripheral is connected") + mtu = wait_for_event(_IRQ_MTU_EXCHANGED, TIMEOUT_MS) multitest.wait(f"client:discovery:{i}") @@ -138,6 +141,13 @@ def instance1(): ble.gap_connect(BDADDR[0], BDADDR[1], TIMEOUT_MS) conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) + # Wait for peripheral to be ready for MTU exchange. + # On ESP32 with NimBLE and IDF 5.4.2 (at least), if the MTU exchange occurs + # immediately after the peripheral connect event, the peripheral may see its + # _IRQ_MTU_EXCHANGED event before its _IRQ_CENTRAL_CONNECT event. The wait + # here ensures correct event ordering on the peripheral. + multitest.wait("peripheral is connected") + # Central-initiated mtu exchange. print("gattc_exchange_mtu") ble.gattc_exchange_mtu(conn_handle) diff --git a/tests/multi_bluetooth/ble_mtu_peripheral.py b/tests/multi_bluetooth/ble_mtu_peripheral.py index 1c0de40a0..50882bcd7 100644 --- a/tests/multi_bluetooth/ble_mtu_peripheral.py +++ b/tests/multi_bluetooth/ble_mtu_peripheral.py @@ -101,6 +101,13 @@ def instance0(): # Wait for central to connect to us. conn_handle = wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) + # Wait for central to be ready for MTU exchange. + # On ESP32 with NimBLE and IDF 5.4.2 (at least), if the MTU exchange occurs + # immediately after the central connect event, the central may see its + # _IRQ_MTU_EXCHANGED event before its _IRQ_PERIPHERAL_CONNECT event. The + # wait here ensures correct event ordering on the central. + multitest.wait("central is connected") + # Peripheral-initiated mtu exchange. print("gattc_exchange_mtu") ble.gattc_exchange_mtu(conn_handle) @@ -142,6 +149,9 @@ def instance1(): ble.gap_connect(BDADDR[0], BDADDR[1], 5000) conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) + # Inform the peripheral that we have been connected to. + multitest.broadcast("central is connected") + mtu = wait_for_event(_IRQ_MTU_EXCHANGED, TIMEOUT_MS) print("gattc_discover_characteristics") |
