← index #8413PR #5875
Related · high · value 0.348
QUERY · ISSUE

btstack on unix port with libusb, result in Segmentation fault

openby meetrogeropened 2022-03-14updated 2026-03-22
port-unix

Tested on commit: eec07332b127587a62f6555644f069620afa6091

Installed libusb by: sudo apt install libusb-1.0-0-dev
I've verified the btstack/port/libusb already works, on both RPI4 and x86 platforms.
Using the "standard" variant, I enabled the scheduler in mpconfigvariant.h

#define MICROPY_ENABLE_SCHEDULER                (1)

And enabled the BT+btstack in mpconfigvariant.mk

MICROPY_PY_BLUETOOTH ?= 1

Then, I run the example:

sudo ./micropython ble_simple_central.py

Simply got the result:

USB Path: 02
Segmentation fault

Tracing down the code, this problem is in ports/unix/mpthreadport.c, pthread_getspecific() returns NULL with errno=11 (Resource temporally not available):

mp_state_thread_t *mp_thread_get_state(void) {
    return (mp_state_thread_t *)pthread_getspecific(tls_key);
}

If I skip this pthread_getspecific() and returns &mp_state_ctx.thread directly, I got a further error: "*** stack smashing detected ***: terminated".

CANDIDATE · PULL REQUEST

unix: Add a modbluetooth implementation supporting USB adaptors.

closedby jimmoopened 2020-04-07updated 2020-04-29
port-unix

This PR adds a "btstack" variant that links in btstack and provides an implementation of the port-specific functionality using the "libusb" btstack port.

Some improvements to the way stack initialisation and deinitialisation were added to modbluetooth_btstack, which should be beneficial for STM32 also.

I've been testing this with a BCM20702A-based USB adaptor (specifically https://au.targus.com/products/bluetooth-4-0-dual-mode-micro-usb-adapter-acb75au). This PR passes the multitests with the six combinations of PYBD-NimBLE (a), PYBD-BTStack (b), Unix-BTStack (c) (ab, ba, bc, cb, ac, ca). (BTW, the multitests were extremely useful in getting this working!).

In theory this can be easily adapted to other btstack ports, of which the libusb-intel, raspi and various posix-h4/h5 are probably the most interesting. It would also be good to make the USB device path configurable via hci_transport_usb_set_path, which would also allow the multi-tests to run with two adaptors on the same machine.

The filtering for run-multitests.py isn't ideal, open to suggestions there (maybe the test .py or .exp could specify these). I'll try and figure out a way to suppress these messages from btstack/libusb.

Note this PR requires #5859 (as it uses the scheduler from multiple threads), and includes the commits from that PR. It also needs bluekitchen/btstack#278.

@mringwal FYI

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied