extmod/btstack: Update BTstack bindings to work with latest BTstack.

The following multi-tests pass (eg with PYBD_SF6+LEGO_HUB_NO6):

    ble_gap_advertise.py
    ble_gap_connect.py
    ble_gap_device_name.py
    ble_gattc_discover_services.py
    ble_gatt_data_transfer.py
    perf_gatt_char_write.py
    perf_gatt_notify.py
    stress_log_filesystem.py

These are the same tests that passed prior to this BTstack update.

Also tested on the unix port using H4 transport.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2022-09-08 12:51:19 +10:00
parent 4f946ba963
commit 67f98ba10c
8 changed files with 44 additions and 28 deletions

View File

@ -6,7 +6,7 @@
#define ENABLE_LE_PERIPHERAL #define ENABLE_LE_PERIPHERAL
#define ENABLE_LE_CENTRAL #define ENABLE_LE_CENTRAL
// #define ENABLE_CLASSIC // #define ENABLE_CLASSIC
#define ENABLE_LE_DATA_CHANNELS #define ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
// #define ENABLE_LOG_INFO // #define ENABLE_LOG_INFO
// #define ENABLE_LOG_DEBUG // #define ENABLE_LOG_DEBUG
#define ENABLE_LOG_ERROR #define ENABLE_LOG_ERROR

View File

@ -159,6 +159,12 @@ const btstack_uart_block_t mp_bluetooth_btstack_hci_uart_block = {
&btstack_uart_get_supported_sleep_modes, &btstack_uart_get_supported_sleep_modes,
&btstack_uart_set_sleep, &btstack_uart_set_sleep,
&btstack_uart_set_wakeup_handler, &btstack_uart_set_wakeup_handler,
// The following are needed for H5 mode only.
NULL, // set_frame_received
NULL, // set_frame_sent,
NULL, // receive_frame,
NULL, // send_frame,
}; };
void mp_bluetooth_btstack_hci_uart_process(void) { void mp_bluetooth_btstack_hci_uart_process(void) {

View File

@ -28,7 +28,7 @@
#ifndef MICROPY_INCLUDED_EXTMOD_BTSTACK_HCI_UART_H #ifndef MICROPY_INCLUDED_EXTMOD_BTSTACK_HCI_UART_H
#define MICROPY_INCLUDED_EXTMOD_BTSTACK_HCI_UART_H #define MICROPY_INCLUDED_EXTMOD_BTSTACK_HCI_UART_H
#include "lib/btstack/src/btstack.h" #include "lib/btstack/src/btstack_uart_block.h"
// --- Used by the port to create the HCI transport --------------------------- // --- Used by the port to create the HCI transport ---------------------------
extern const btstack_uart_block_t mp_bluetooth_btstack_hci_uart_block; extern const btstack_uart_block_t mp_bluetooth_btstack_hci_uart_block;

View File

@ -368,7 +368,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
event_type == SM_EVENT_PAIRING_COMPLETE || event_type == SM_EVENT_PAIRING_COMPLETE ||
// event_type == GAP_EVENT_DEDICATED_BONDING_COMPLETED || // No conn_handle // event_type == GAP_EVENT_DEDICATED_BONDING_COMPLETED || // No conn_handle
event_type == HCI_EVENT_ENCRYPTION_CHANGE) { event_type == HCI_EVENT_ENCRYPTION_CHANGE) {
DEBUG_printf(" --> enc/auth/pair/bond change\n", ); DEBUG_printf(" --> enc/auth/pair/bond change\n");
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
uint16_t conn_handle; uint16_t conn_handle;
switch (event_type) { switch (event_type) {
@ -420,6 +420,11 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
mp_bluetooth_gap_on_scan_result(address_type, address, adv_event_type, rssi, data, length); mp_bluetooth_gap_on_scan_result(address_type, address, adv_event_type, rssi, data, length);
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE #endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
} else if (event_type == GATT_EVENT_MTU) {
// This is triggered in client mode.
uint16_t conn_handle = gatt_event_mtu_get_handle(packet);
uint16_t mtu = gatt_event_mtu_get_MTU(packet);
mp_bluetooth_gatts_on_mtu_exchanged(conn_handle, mtu);
} else if (event_type == GATT_EVENT_QUERY_COMPLETE) { } else if (event_type == GATT_EVENT_QUERY_COMPLETE) {
uint16_t conn_handle = gatt_event_query_complete_get_handle(packet); uint16_t conn_handle = gatt_event_query_complete_get_handle(packet);
uint16_t status = gatt_event_query_complete_get_att_status(packet); uint16_t status = gatt_event_query_complete_get_att_status(packet);
@ -625,6 +630,19 @@ STATIC void set_random_address(void) {
DEBUG_printf("set_random_address: Address loaded by controller\n"); DEBUG_printf("set_random_address: Address loaded by controller\n");
} }
STATIC void deinit_stack(void) {
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
// Deinitialise BTstack components.
sm_deinit();
l2cap_deinit();
hci_deinit();
btstack_memory_deinit();
btstack_run_loop_deinit();
MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL;
}
int mp_bluetooth_init(void) { int mp_bluetooth_init(void) {
DEBUG_printf("mp_bluetooth_init\n"); DEBUG_printf("mp_bluetooth_init\n");
@ -702,8 +720,8 @@ int mp_bluetooth_init(void) {
// Attempt a shutdown (may not do anything). // Attempt a shutdown (may not do anything).
mp_bluetooth_btstack_port_deinit(); mp_bluetooth_btstack_port_deinit();
// Clean up. // Clean up BTstack.
MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL; deinit_stack();
return timeout ? MP_ETIMEDOUT : MP_EINVAL; return timeout ? MP_ETIMEDOUT : MP_EINVAL;
} }
@ -757,8 +775,8 @@ void mp_bluetooth_deinit(void) {
} }
btstack_run_loop_remove_timer(&btstack_init_deinit_timeout); btstack_run_loop_remove_timer(&btstack_init_deinit_timeout);
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; // Clean up BTstack.
MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL; deinit_stack();
DEBUG_printf("mp_bluetooth_deinit: complete\n"); DEBUG_printf("mp_bluetooth_deinit: complete\n");
} }

View File

@ -31,6 +31,7 @@
#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
#include "lib/btstack/src/btstack.h" #include "lib/btstack/src/btstack.h"
#include "lib/btstack/src/hci_transport_h4.h"
#include "extmod/mpbthci.h" #include "extmod/mpbthci.h"
#include "extmod/btstack/btstack_hci_uart.h" #include "extmod/btstack/btstack_hci_uart.h"
#include "extmod/btstack/modbluetooth_btstack.h" #include "extmod/btstack/modbluetooth_btstack.h"
@ -137,16 +138,10 @@ void mp_bluetooth_hci_poll(void) {
} }
void mp_bluetooth_btstack_port_init(void) { void mp_bluetooth_btstack_port_init(void) {
static bool run_loop_init = false; btstack_run_loop_init(&mp_btstack_runloop_stm32);
if (!run_loop_init) {
run_loop_init = true;
btstack_run_loop_init(&mp_btstack_runloop_stm32);
} else {
mp_btstack_runloop_stm32.init();
}
// hci_dump_open(NULL, HCI_DUMP_STDOUT); // hci_dump_open(NULL, HCI_DUMP_STDOUT);
const hci_transport_t *transport = hci_transport_h4_instance(&mp_bluetooth_btstack_hci_uart_block); const hci_transport_t *transport = hci_transport_h4_instance_for_uart(&mp_bluetooth_btstack_hci_uart_block);
hci_init(transport, &hci_transport_config_uart); hci_init(transport, &hci_transport_config_uart);
#ifdef MICROPY_HW_BLE_BTSTACK_CHIPSET_INSTANCE #ifdef MICROPY_HW_BLE_BTSTACK_CHIPSET_INSTANCE

View File

@ -79,13 +79,7 @@ uint32_t hal_time_ms(void) {
} }
void mp_bluetooth_btstack_port_init(void) { void mp_bluetooth_btstack_port_init(void) {
static bool run_loop_init = false; btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
if (!run_loop_init) {
run_loop_init = true;
btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
} else {
btstack_run_loop_embedded_get_instance()->init();
}
// hci_dump_open(NULL, HCI_DUMP_STDOUT); // hci_dump_open(NULL, HCI_DUMP_STDOUT);

View File

@ -32,6 +32,7 @@
#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_H4 #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_H4
#include "lib/btstack/src/hci_transport_h4.h"
#include "lib/btstack/chipset/zephyr/btstack_chipset_zephyr.h" #include "lib/btstack/chipset/zephyr/btstack_chipset_zephyr.h"
#include "extmod/btstack/btstack_hci_uart.h" #include "extmod/btstack/btstack_hci_uart.h"
@ -42,11 +43,12 @@
#define DEBUG_printf(...) // printf(__VA_ARGS__) #define DEBUG_printf(...) // printf(__VA_ARGS__)
STATIC hci_transport_config_uart_t hci_transport_config_uart = { STATIC hci_transport_config_uart_t hci_transport_config_uart = {
HCI_TRANSPORT_CONFIG_UART, .type = HCI_TRANSPORT_CONFIG_UART,
1000000, // initial baudrate .baudrate_init = 1000000,
0, // main baudrate .baudrate_main = 0,
1, // flow control .flowcontrol = 1,
NULL, // device name .device_name = NULL,
.parity = BTSTACK_UART_PARITY_OFF,
}; };
void mp_bluetooth_hci_poll_h4(void) { void mp_bluetooth_hci_poll_h4(void) {
@ -58,7 +60,7 @@ void mp_bluetooth_hci_poll_h4(void) {
void mp_bluetooth_btstack_port_init_h4(void) { void mp_bluetooth_btstack_port_init_h4(void) {
DEBUG_printf("mp_bluetooth_btstack_port_init_h4\n"); DEBUG_printf("mp_bluetooth_btstack_port_init_h4\n");
const hci_transport_t *transport = hci_transport_h4_instance(&mp_bluetooth_btstack_hci_uart_block); const hci_transport_t *transport = hci_transport_h4_instance_for_uart(&mp_bluetooth_btstack_hci_uart_block);
hci_init(transport, &hci_transport_config_uart); hci_init(transport, &hci_transport_config_uart);
hci_set_chipset(btstack_chipset_zephyr_instance()); hci_set_chipset(btstack_chipset_zephyr_instance());

View File

@ -34,6 +34,7 @@
#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_USB #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_USB
#include "lib/btstack/src/btstack.h" #include "lib/btstack/src/btstack.h"
#include "lib/btstack/src/hci_transport_usb.h"
#include "lib/btstack/platform/embedded/btstack_run_loop_embedded.h" #include "lib/btstack/platform/embedded/btstack_run_loop_embedded.h"
#include "lib/btstack/platform/embedded/hal_cpu.h" #include "lib/btstack/platform/embedded/hal_cpu.h"
#include "lib/btstack/platform/embedded/hal_time_ms.h" #include "lib/btstack/platform/embedded/hal_time_ms.h"