STM32: BLE Bonding Example does not work
I'm using a Pyboad SF2 and nightly build as of
MicroPython v1.23.0-preview.90.ged15b3c6c on 2024-02-06; PYBD-SF2W with STM32F722IEK
When trying the bonding example:
https://github.com/micropython/micropython/blob/master/examples/bluetooth/ble_bonding_peripheral.py
I got the following response:
no secrets available
get secret: 2 0 None
get secret: 10 0 b'irk'
set secret: (10, b'irk') b'\xdc$W\xae\xf1I\xb9\xee\xc0\x9f\xc7\xed,\x82\t\x94'
Traceback (most recent call last):
File "<stdin>", line 199, in <module>
File "<stdin>", line 184, in demo
File "<stdin>", line 81, in __init__
File "<stdin>", line 158, in _advertise
OSError: -513
the problem seems to be related to the ble.config's addr_mode. If I uncomment the call here:
https://github.com/micropython/micropython/blob/master/examples/bluetooth/ble_bonding_peripheral.py#L75
and here:
https://github.com/micropython/micropython/blob/master/examples/bluetooth/ble_bonding_peripheral.py#L157
it doesn't throw the error.
I understand that the ble bonding is still a work in progress demo. However I wanted to have a go on implementing bonding support for the pico_w. , and for this I thought I'd first start with a working example from a supported platform (even if it's from nimble - so another ble stack than on the pico_w).
Also.. is addr_mode set to 2 required for ble bonding?
Thanks for hints and clarifications!
extmod/modbluetooth: Add pairing/bonding support.
WIP, looking for feedback on the approach.
Much of this work was done by @andrewleech in #6289, #6312, and #6447. This PR changes:
- Moved the bonding secret store into Python (via _IRQ_GET_SECRET and _IRQ_SET_SECRET events).
- Adds characteristic flags to enable auto-pairing (e.g. READ_ENCRYPTED)
- Adds passkey authentication.
- gap_pair just takes conn_handle, and pairing/bonding parameters are set via config().
- Documentation
Currently only supported on NimBLE on STM32 and Unix, as it requires synchronous (non-ringbuffer) events. Enabled via MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING macro (which is now enabled by default for NimBLE STM32/Unix). Some BlueKitchen support is implemented, but will need to wait until it's migrated to synchronous events. The secret store is designed to be compatible with BlueKitchen's TLV store API.
Tested against Android and BlueZ.
This PR adds:
- Better error handling for oversize characteristic reads.
- An _IRQ_CONNECTION_UPDATE event. Not specifically related to pairing and bonding, but useful information about the connection state.
- An _IRQ_ENCRYPTION_UPDATE event to get notified about pairing/bonding state change.
- Widens characteristic flags to 16-bit and allows for setting encryption/authentication/authorisation requirements.
- Allows the user to fail the read request IRQ with a custom error code.
- Configuration of pairing/bonding parameters (le_secure, bond, mitm, io)
- gap_pair() method to initiate pairing.
- _IRQ_GET_SECRET and IRQ_SET_SECRET events for persistent key management.
- Simplify the handing of numeric arguments in invoke_irq_handler (used in synchronous events).
- An _IRQ_PASSKEY_ACTION event to be notified of an authentication request.
- gap_passkey() method to respond to an authentication request.
- ble_bonding_peripheral.py example, showing implementation of key persistence and authentication. (WIP)
Four major outstanding pieces:
- Write multi-test.
- Support address resolving in central mode (perhaps via a _IRQ_RESOLVED_SCAN_RESULT event)
- Improve handing of RPA mode. NimBLE doesn't seem to support using a non-fixed IRK (see https://github.com/apache/mynewt-nimble/issues/887)
- PYBD doesn't work with RPA mode. (Works fine on Unix+Zephyr and WB55)