Skip to content

Commit b3d88cf

Browse files
agattidpgeorge
authored andcommitted
extmod/nimble/modbluetooth_nimble: Handle port init failures.
This commit fixes an issue related to the NimBLE initialisation procedure in low memory environments on ESP32 boards. MicroPython uses at least two different NimBLE stacks across the supported ports, mynewt (imported as an external library), and the one provided by Espressif in their own SDKs. The problem is that these two ports differ in the signature for `nimble_port_init(void)`, with mynewt returning `void`, and Espressif's returning a status code on failure. On ESP32 boards, allocating almost all the available heap and then turning on the Bluetooth stack would trigger a failure in the NimBLE initialisation function that is not handled by the NimBLE integration code, as there's no expectation of a recoverable condition. Since the stack initialisation would progress, then uninitialised memory accesses crash the board. Since we cannot really modify neither mynewt nor Espressif SDKs, the next best thing is to provide two conditional initialisation paths depending on a configuration setting. This would make Espressif ports recover from a failed initialisation whilst retaining the existing behaviour on other ports. This fixes micropython#14293. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
1 parent b1d635f commit b3d88cf

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

extmod/nimble/modbluetooth_nimble.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,17 @@ int mp_bluetooth_init(void) {
618618

619619
// Initialise NimBLE memory and data structures.
620620
DEBUG_printf("mp_bluetooth_init: nimble_port_init\n");
621+
622+
// On some ports `nimble_port_init` may return an error code indicating a
623+
// failed initialisation.
624+
#if MICROPY_BLUETOOTH_NIMBLE_PORT_INIT_RETURNS_ERROR
625+
if (nimble_port_init() < 0) {
626+
mp_bluetooth_deinit();
627+
return MP_EIO;
628+
}
629+
#else
621630
nimble_port_init();
631+
#endif
622632

623633
ble_hs_cfg.reset_cb = reset_cb;
624634
ble_hs_cfg.sync_cb = sync_cb;

ports/esp32/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
#define MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING (1)
109109
#define MICROPY_BLUETOOTH_NIMBLE (1)
110110
#define MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY (1)
111+
#define MICROPY_BLUETOOTH_NIMBLE_PORT_INIT_RETURNS_ERROR (1)
111112
#endif // MICROPY_PY_BLUETOOTH
112113

113114
#define MICROPY_PY_RANDOM_SEED_INIT_FUNC (esp_random())

0 commit comments

Comments
 (0)