Skip to content

Commit 67f98ba

Browse files
committed
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 <[email protected]>
1 parent 4f946ba commit 67f98ba

File tree

8 files changed

+44
-28
lines changed

8 files changed

+44
-28
lines changed

extmod/btstack/btstack_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#define ENABLE_LE_PERIPHERAL
77
#define ENABLE_LE_CENTRAL
88
// #define ENABLE_CLASSIC
9-
#define ENABLE_LE_DATA_CHANNELS
9+
#define ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
1010
// #define ENABLE_LOG_INFO
1111
// #define ENABLE_LOG_DEBUG
1212
#define ENABLE_LOG_ERROR

extmod/btstack/btstack_hci_uart.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ const btstack_uart_block_t mp_bluetooth_btstack_hci_uart_block = {
159159
&btstack_uart_get_supported_sleep_modes,
160160
&btstack_uart_set_sleep,
161161
&btstack_uart_set_wakeup_handler,
162+
163+
// The following are needed for H5 mode only.
164+
NULL, // set_frame_received
165+
NULL, // set_frame_sent,
166+
NULL, // receive_frame,
167+
NULL, // send_frame,
162168
};
163169

164170
void mp_bluetooth_btstack_hci_uart_process(void) {

extmod/btstack/btstack_hci_uart.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#ifndef MICROPY_INCLUDED_EXTMOD_BTSTACK_HCI_UART_H
2929
#define MICROPY_INCLUDED_EXTMOD_BTSTACK_HCI_UART_H
3030

31-
#include "lib/btstack/src/btstack.h"
31+
#include "lib/btstack/src/btstack_uart_block.h"
3232

3333
// --- Used by the port to create the HCI transport ---------------------------
3434
extern const btstack_uart_block_t mp_bluetooth_btstack_hci_uart_block;

extmod/btstack/modbluetooth_btstack.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
368368
event_type == SM_EVENT_PAIRING_COMPLETE ||
369369
// event_type == GAP_EVENT_DEDICATED_BONDING_COMPLETED || // No conn_handle
370370
event_type == HCI_EVENT_ENCRYPTION_CHANGE) {
371-
DEBUG_printf(" --> enc/auth/pair/bond change\n", );
371+
DEBUG_printf(" --> enc/auth/pair/bond change\n");
372372
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
373373
uint16_t conn_handle;
374374
switch (event_type) {
@@ -420,6 +420,11 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
420420
mp_bluetooth_gap_on_scan_result(address_type, address, adv_event_type, rssi, data, length);
421421
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
422422
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
423+
} else if (event_type == GATT_EVENT_MTU) {
424+
// This is triggered in client mode.
425+
uint16_t conn_handle = gatt_event_mtu_get_handle(packet);
426+
uint16_t mtu = gatt_event_mtu_get_MTU(packet);
427+
mp_bluetooth_gatts_on_mtu_exchanged(conn_handle, mtu);
423428
} else if (event_type == GATT_EVENT_QUERY_COMPLETE) {
424429
uint16_t conn_handle = gatt_event_query_complete_get_handle(packet);
425430
uint16_t status = gatt_event_query_complete_get_att_status(packet);
@@ -625,6 +630,19 @@ STATIC void set_random_address(void) {
625630
DEBUG_printf("set_random_address: Address loaded by controller\n");
626631
}
627632

633+
STATIC void deinit_stack(void) {
634+
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
635+
636+
// Deinitialise BTstack components.
637+
sm_deinit();
638+
l2cap_deinit();
639+
hci_deinit();
640+
btstack_memory_deinit();
641+
btstack_run_loop_deinit();
642+
643+
MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL;
644+
}
645+
628646
int mp_bluetooth_init(void) {
629647
DEBUG_printf("mp_bluetooth_init\n");
630648

@@ -702,8 +720,8 @@ int mp_bluetooth_init(void) {
702720
// Attempt a shutdown (may not do anything).
703721
mp_bluetooth_btstack_port_deinit();
704722

705-
// Clean up.
706-
MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL;
723+
// Clean up BTstack.
724+
deinit_stack();
707725

708726
return timeout ? MP_ETIMEDOUT : MP_EINVAL;
709727
}
@@ -757,8 +775,8 @@ void mp_bluetooth_deinit(void) {
757775
}
758776
btstack_run_loop_remove_timer(&btstack_init_deinit_timeout);
759777

760-
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
761-
MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL;
778+
// Clean up BTstack.
779+
deinit_stack();
762780

763781
DEBUG_printf("mp_bluetooth_deinit: complete\n");
764782
}

ports/stm32/mpbtstackport.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
3232

3333
#include "lib/btstack/src/btstack.h"
34+
#include "lib/btstack/src/hci_transport_h4.h"
3435
#include "extmod/mpbthci.h"
3536
#include "extmod/btstack/btstack_hci_uart.h"
3637
#include "extmod/btstack/modbluetooth_btstack.h"
@@ -137,16 +138,10 @@ void mp_bluetooth_hci_poll(void) {
137138
}
138139

139140
void mp_bluetooth_btstack_port_init(void) {
140-
static bool run_loop_init = false;
141-
if (!run_loop_init) {
142-
run_loop_init = true;
143-
btstack_run_loop_init(&mp_btstack_runloop_stm32);
144-
} else {
145-
mp_btstack_runloop_stm32.init();
146-
}
141+
btstack_run_loop_init(&mp_btstack_runloop_stm32);
147142

148143
// hci_dump_open(NULL, HCI_DUMP_STDOUT);
149-
const hci_transport_t *transport = hci_transport_h4_instance(&mp_bluetooth_btstack_hci_uart_block);
144+
const hci_transport_t *transport = hci_transport_h4_instance_for_uart(&mp_bluetooth_btstack_hci_uart_block);
150145
hci_init(transport, &hci_transport_config_uart);
151146

152147
#ifdef MICROPY_HW_BLE_BTSTACK_CHIPSET_INSTANCE

ports/unix/mpbtstackport_common.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,7 @@ uint32_t hal_time_ms(void) {
7979
}
8080

8181
void mp_bluetooth_btstack_port_init(void) {
82-
static bool run_loop_init = false;
83-
if (!run_loop_init) {
84-
run_loop_init = true;
85-
btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
86-
} else {
87-
btstack_run_loop_embedded_get_instance()->init();
88-
}
82+
btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
8983

9084
// hci_dump_open(NULL, HCI_DUMP_STDOUT);
9185

ports/unix/mpbtstackport_h4.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_H4
3434

35+
#include "lib/btstack/src/hci_transport_h4.h"
3536
#include "lib/btstack/chipset/zephyr/btstack_chipset_zephyr.h"
3637

3738
#include "extmod/btstack/btstack_hci_uart.h"
@@ -42,11 +43,12 @@
4243
#define DEBUG_printf(...) // printf(__VA_ARGS__)
4344

4445
STATIC hci_transport_config_uart_t hci_transport_config_uart = {
45-
HCI_TRANSPORT_CONFIG_UART,
46-
1000000, // initial baudrate
47-
0, // main baudrate
48-
1, // flow control
49-
NULL, // device name
46+
.type = HCI_TRANSPORT_CONFIG_UART,
47+
.baudrate_init = 1000000,
48+
.baudrate_main = 0,
49+
.flowcontrol = 1,
50+
.device_name = NULL,
51+
.parity = BTSTACK_UART_PARITY_OFF,
5052
};
5153

5254
void mp_bluetooth_hci_poll_h4(void) {
@@ -58,7 +60,7 @@ void mp_bluetooth_hci_poll_h4(void) {
5860
void mp_bluetooth_btstack_port_init_h4(void) {
5961
DEBUG_printf("mp_bluetooth_btstack_port_init_h4\n");
6062

61-
const hci_transport_t *transport = hci_transport_h4_instance(&mp_bluetooth_btstack_hci_uart_block);
63+
const hci_transport_t *transport = hci_transport_h4_instance_for_uart(&mp_bluetooth_btstack_hci_uart_block);
6264
hci_init(transport, &hci_transport_config_uart);
6365

6466
hci_set_chipset(btstack_chipset_zephyr_instance());

ports/unix/mpbtstackport_usb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_USB
3535

3636
#include "lib/btstack/src/btstack.h"
37+
#include "lib/btstack/src/hci_transport_usb.h"
3738
#include "lib/btstack/platform/embedded/btstack_run_loop_embedded.h"
3839
#include "lib/btstack/platform/embedded/hal_cpu.h"
3940
#include "lib/btstack/platform/embedded/hal_time_ms.h"

0 commit comments

Comments
 (0)