Skip to content

extmod/modbluetooth: Remove non-sync event support. #8945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
10 changes: 3 additions & 7 deletions docs/library/bluetooth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ Configuration
characteristic 0x2a00. This can be set at any time and changed multiple
times.

- ``'rxbuf'``: Get/set the size in bytes of the internal buffer used to store
incoming events. This buffer is global to the entire BLE driver and so
handles incoming data for all events, including all characteristics.
Increasing this allows better handling of bursty incoming data (for
example scan results) and the ability to receive larger characteristic values.

- ``'mtu'``: Get/set the MTU that will be used during a ATT MTU exchange. The
resulting MTU will be the minimum of this and the remote device's MTU.
ATT MTU exchange will not happen automatically (unless the remote device initiates
Expand Down Expand Up @@ -114,7 +108,7 @@ Event Handling
**Note:** As an optimisation to prevent unnecessary allocations, the ``addr``,
``adv_data``, ``char_data``, ``notify_data``, and ``uuid`` entries in the
tuples are read-only memoryview instances pointing to :mod:`bluetooth`'s internal
ringbuffer, and are only valid during the invocation of the IRQ handler
memory, and are only valid during the invocation of the IRQ handler
function. If your program needs to save one of these values to access after
the IRQ handler has returned (e.g. by saving it in a class instance or global
variable), then it needs to take a copy of the data, either by using ``bytes()``
Expand All @@ -128,6 +122,8 @@ Event Handling
used elsewhere in the program. And to print data from within the IRQ handler,
``print(bytes(addr))`` will be needed.

This is also true of the ``data`` tuple itself, do not store a reference to it.

An event handler showing all possible events::

def bt_irq(event, data):
Expand Down
2 changes: 0 additions & 2 deletions extmod/btstack/btstack.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ INC += -I$(TOP)/$(BTSTACK_EXTMOD_DIR)

CFLAGS_EXTMOD += -DMICROPY_BLUETOOTH_BTSTACK=1
CFLAGS_EXTMOD += -DMICROPY_BLUETOOTH_BTSTACK_CONFIG_FILE=$(MICROPY_BLUETOOTH_BTSTACK_CONFIG_FILE)
CFLAGS_EXTMOD += -DMICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS=1
CFLAGS_EXTMOD += -DMICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING=1

BTSTACK_DIR = $(TOP)/lib/btstack

Expand Down
716 changes: 103 additions & 613 deletions extmod/modbluetooth.c

Large diffs are not rendered by default.

18 changes: 2 additions & 16 deletions extmod/modbluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,16 @@

#ifndef MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
// Enable the client by default if we're enabling central mode. It's possible
// to enable client without central though.
// to enable client without central though (e.g. non-connecting scanner).
#define MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT (MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE)
#endif

#ifndef MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS
// This can be enabled if the BLE stack runs entirely in scheduler context
// and therefore is able to call directly into the VM to run Python callbacks.
#define MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS (0)
#endif

// A port can optionally enable support for L2CAP "Connection Oriented Channels".
#ifndef MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS
#define MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS (0)
#endif

// A port can optionally enable support for pairing and bonding.
// Requires MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS.
// Pairing and bonding enabled by default, but can be disabled by a port.
#ifndef MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
#define MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING (0)
#endif
Expand All @@ -72,13 +65,6 @@
#define MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD (0)
#endif

// This is used to protect the ringbuffer.
// A port may no-op this if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS is enabled.
#ifndef MICROPY_PY_BLUETOOTH_ENTER
#define MICROPY_PY_BLUETOOTH_ENTER mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
#define MICROPY_PY_BLUETOOTH_EXIT MICROPY_END_ATOMIC_SECTION(atomic_state);
#endif

// Common constants.
#ifndef MP_BLUETOOTH_DEFAULT_ATTR_LEN
#define MP_BLUETOOTH_DEFAULT_ATTR_LEN (20)
Expand Down
131 changes: 0 additions & 131 deletions extmod/nimble/hal/hal_uart.c

This file was deleted.

48 changes: 0 additions & 48 deletions extmod/nimble/hal/hal_uart.h

This file was deleted.

26 changes: 11 additions & 15 deletions extmod/nimble/modbluetooth_nimble.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,6 @@ STATIC void set_random_address(bool nrpa) {
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
// For ble_hs_pvcy_set_our_irk
#include "nimble/host/src/ble_hs_pvcy_priv.h"
// For ble_hs_hci_util_rand
#include "nimble/host/src/ble_hs_hci_priv.h"
// For ble_hs_misc_restore_irks
#include "nimble/host/src/ble_hs_priv.h"

Expand Down Expand Up @@ -299,7 +297,7 @@ STATIC int load_irk(void) {
} else {
DEBUG_printf("load_irk: Generating new IRK.\n");
uint8_t rand_irk[16];
rc = ble_hs_hci_util_rand(rand_irk, 16);
rc = ble_hs_hci_rand(rand_irk, 16);
if (rc) {
return rc;
}
Expand Down Expand Up @@ -531,12 +529,12 @@ STATIC int central_gap_event_cb(struct ble_gap_event *event, void *arg) {
// TODO: In the future if a port ever needs to customise these functions
// then investigate using MP_WEAK or splitting them out to another .c file.

#include "transport/uart/ble_hci_uart.h"
#include "nimble/transport.h"

void mp_bluetooth_nimble_port_hci_init(void) {
DEBUG_printf("mp_bluetooth_nimble_port_hci_init (nimble default)\n");
// This calls mp_bluetooth_hci_uart_init (via ble_hci_uart_init --> hal_uart_config --> mp_bluetooth_hci_uart_init).
ble_hci_uart_init();
// This calls mp_bluetooth_hci_uart_init (via ble_transport_hs_init --> hal_uart_config --> mp_bluetooth_hci_uart_init).
ble_transport_ll_init();
mp_bluetooth_hci_controller_init();
}

Expand Down Expand Up @@ -618,20 +616,20 @@ int mp_bluetooth_init(void) {
MP_STATE_PORT(bluetooth_nimble_memory) = NULL;
#endif

// Allow port (ESP32) to override NimBLE's HCI init.
// Otherwise default implementation above calls ble_hci_uart_init().
mp_bluetooth_nimble_port_hci_init();

// Static initialization is complete, can start processing events.
mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC;

// Initialise NimBLE memory and data structures.
DEBUG_printf("mp_bluetooth_init: nimble_port_init\n");
nimble_port_init();

// Allow port (ESP32) to override NimBLE's HCI init.
// Otherwise default implementation above calls ble_transport_hs_init().
mp_bluetooth_nimble_port_hci_init();

// Make sure that the HCI UART and event handling task is running.
mp_bluetooth_nimble_port_start();

// Static initialization is complete, can start processing events.
mp_bluetooth_nimble_ble_state = MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC;

// Run the scheduler while we wait for stack startup.
// On non-ringbuffer builds (NimBLE on STM32/Unix) this will also poll the UART and run the event queue.
mp_uint_t timeout_start_ticks_ms = mp_hal_ticks_ms();
Expand Down Expand Up @@ -1832,7 +1830,6 @@ int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf
return MP_EINVAL;
}

MICROPY_PY_BLUETOOTH_ENTER
if (chan->rx_pending) {
size_t avail = OS_MBUF_PKTLEN(chan->rx_pending);

Expand Down Expand Up @@ -1877,7 +1874,6 @@ int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf
*len = 0;
}

MICROPY_PY_BLUETOOTH_EXIT
return 0;
}

Expand Down
9 changes: 5 additions & 4 deletions extmod/nimble/nimble.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ target_include_directories(micropy_extmod_nimble INTERFACE
${NIMBLE_LIB_DIR}/nimble/host/store/ram/include
${NIMBLE_LIB_DIR}/nimble/host/util/include
${NIMBLE_LIB_DIR}/nimble/include
${NIMBLE_LIB_DIR}/nimble/transport/uart/include
${NIMBLE_LIB_DIR}/nimble/transport/include
${NIMBLE_LIB_DIR}/nimble/transport/common/hci_h4/include
${NIMBLE_LIB_DIR}/porting/nimble/include
)

target_sources(micropy_extmod_nimble INTERFACE
${NIMBLE_EXTMOD_DIR}/hal/hal_uart.c
${NIMBLE_EXTMOD_DIR}/transport/uart_ll.c
${NIMBLE_EXTMOD_DIR}/nimble/nimble_npl_os.c
${NIMBLE_LIB_DIR}/ext/tinycrypt/src/aes_encrypt.c
${NIMBLE_LIB_DIR}/ext/tinycrypt/src/cmac_mode.c
Expand Down Expand Up @@ -60,7 +61,6 @@ target_sources(micropy_extmod_nimble INTERFACE
${NIMBLE_LIB_DIR}/nimble/host/src/ble_l2cap_coc.c
${NIMBLE_LIB_DIR}/nimble/host/src/ble_l2cap_sig.c
${NIMBLE_LIB_DIR}/nimble/host/src/ble_l2cap_sig_cmd.c
${NIMBLE_LIB_DIR}/nimble/host/src/ble_monitor.c
${NIMBLE_LIB_DIR}/nimble/host/src/ble_sm.c
${NIMBLE_LIB_DIR}/nimble/host/src/ble_sm_alg.c
${NIMBLE_LIB_DIR}/nimble/host/src/ble_sm_cmd.c
Expand All @@ -70,7 +70,8 @@ target_sources(micropy_extmod_nimble INTERFACE
${NIMBLE_LIB_DIR}/nimble/host/src/ble_store_util.c
${NIMBLE_LIB_DIR}/nimble/host/src/ble_uuid.c
${NIMBLE_LIB_DIR}/nimble/host/util/src/addr.c
${NIMBLE_LIB_DIR}/nimble/transport/uart/src/ble_hci_uart.c
${NIMBLE_LIB_DIR}/nimble/transport/src/transport.c
${NIMBLE_LIB_DIR}/nimble/transport/common/hci_h4/src/hci_h4.c
${NIMBLE_LIB_DIR}/porting/nimble/src/endian.c
${NIMBLE_LIB_DIR}/porting/nimble/src/mem.c
${NIMBLE_LIB_DIR}/porting/nimble/src/nimble_port.c
Expand Down
Loading