aboutsummaryrefslogtreecommitdiff
path: root/ports/unix
diff options
context:
space:
mode:
authorJim Mussared2020-08-14 15:43:09 +1000
committerDamien George2020-09-08 11:41:31 +1000
commited14435a8e6199b845c3404a9052f9ff4213292c (patch)
treec7fc4c10aeb002bbe9557e212c62bb75a735fee8 /ports/unix
parente46aac24bacdafcb5323fbfc702a3bd597f66072 (diff)
extmod/modbluetooth: Refactor stack/hci/driver/port bindings.
Previously the interaction between the different layers of the Bluetooth stack was different on each port and each stack. This commit defines common interfaces between them and implements them for cyw43, btstack, nimble, stm32, unix.
Diffstat (limited to 'ports/unix')
-rw-r--r--ports/unix/Makefile9
-rw-r--r--ports/unix/mpbtstackport.h39
-rw-r--r--ports/unix/mpbtstackport_common.c92
-rw-r--r--ports/unix/mpbtstackport_usb.c (renamed from ports/unix/btstack_usb.c)101
-rw-r--r--ports/unix/mpconfigport.h4
5 files changed, 153 insertions, 92 deletions
diff --git a/ports/unix/Makefile b/ports/unix/Makefile
index 3e2fa63a1..a9ba5edfa 100644
--- a/ports/unix/Makefile
+++ b/ports/unix/Makefile
@@ -140,18 +140,19 @@ ifeq ($(HAVE_LIBUSB),1)
CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH=1
CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1
-CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK=1
+# Runs in a thread, cannot make calls into the VM.
+CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK=0
MICROPY_BLUETOOTH_BTSTACK ?= 1
MICROPY_BLUETOOTH_BTSTACK_USB ?= 1
ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1)
GIT_SUBMODULES += lib/btstack
-
include $(TOP)/extmod/btstack/btstack.mk
endif
endif
+
endif
ifeq ($(MICROPY_PY_FFI),1)
@@ -198,7 +199,9 @@ SRC_C = \
alloc.c \
coverage.c \
fatfs_port.c \
- btstack_usb.c \
+ mpbtstackport_common.c \
+ mpbtstackport_usb.c \
+ mpnimbleport.c \
$(SRC_MOD) \
$(wildcard $(VARIANT_DIR)/*.c)
diff --git a/ports/unix/mpbtstackport.h b/ports/unix/mpbtstackport.h
new file mode 100644
index 000000000..29433cc7f
--- /dev/null
+++ b/ports/unix/mpbtstackport.h
@@ -0,0 +1,39 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jim Mussared
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_UNIX_BTSTACK_PORT_H
+#define MICROPY_INCLUDED_UNIX_BTSTACK_PORT_H
+
+#define MICROPY_HW_BLE_UART_ID (0)
+#define MICROPY_HW_BLE_UART_BAUDRATE (1000000)
+
+bool mp_bluetooth_hci_poll(void);
+
+#if MICROPY_BLUETOOTH_BTSTACK_USB
+void mp_bluetooth_btstack_port_init_usb(void);
+#endif
+
+#endif // MICROPY_INCLUDED_UNIX_BTSTACK_PORT_H
diff --git a/ports/unix/mpbtstackport_common.c b/ports/unix/mpbtstackport_common.c
new file mode 100644
index 000000000..402cf9528
--- /dev/null
+++ b/ports/unix/mpbtstackport_common.c
@@ -0,0 +1,92 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jim Mussared
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "py/runtime.h"
+#include "py/mperrno.h"
+#include "py/mphal.h"
+
+#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
+
+#include "lib/btstack/src/btstack.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_time_ms.h"
+
+#include "extmod/btstack/modbluetooth_btstack.h"
+
+#include "mpbtstackport.h"
+
+// Called by the UART polling thread in mpbthciport.c, or by the USB polling thread in mpbtstackport_usb.c.
+bool mp_bluetooth_hci_poll(void) {
+ if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_HALTING) {
+ // Pretend like we're running in IRQ context (i.e. other things can't be running at the same time).
+ mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
+ btstack_run_loop_embedded_execute_once();
+ MICROPY_END_ATOMIC_SECTION(atomic_state);
+
+ return true;
+ }
+
+ return false;
+}
+
+// The IRQ functionality in btstack_run_loop_embedded.c is not used, so the
+// following three functions are empty.
+
+void hal_cpu_disable_irqs(void) {
+}
+
+void hal_cpu_enable_irqs(void) {
+}
+
+void hal_cpu_enable_irqs_and_sleep(void) {
+}
+
+uint32_t hal_time_ms(void) {
+ return mp_hal_ticks_ms();
+}
+
+void mp_bluetooth_btstack_port_init(void) {
+ static bool run_loop_init = false;
+ 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);
+
+ #if MICROPY_BLUETOOTH_BTSTACK_USB
+ mp_bluetooth_btstack_port_init_usb();
+ #endif
+}
+
+void mp_hal_get_mac(int idx, uint8_t buf[6]) {
+}
+
+#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
diff --git a/ports/unix/btstack_usb.c b/ports/unix/mpbtstackport_usb.c
index ab6a49f39..28d2c8c54 100644
--- a/ports/unix/btstack_usb.c
+++ b/ports/unix/mpbtstackport_usb.c
@@ -31,7 +31,7 @@
#include "py/mperrno.h"
#include "py/mphal.h"
-#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
+#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_USB
#include "lib/btstack/src/btstack.h"
#include "lib/btstack/platform/embedded/btstack_run_loop_embedded.h"
@@ -40,76 +40,15 @@
#include "extmod/btstack/modbluetooth_btstack.h"
+#include "mpbtstackport.h"
+
#if !MICROPY_PY_THREAD
#error Unix btstack requires MICROPY_PY_THREAD
#endif
STATIC const useconds_t USB_POLL_INTERVAL_US = 1000;
-STATIC const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
-
-STATIC uint8_t local_addr[6] = {0};
-STATIC uint8_t static_address[6] = {0};
-STATIC volatile bool have_addr = false;
-STATIC bool using_static_address = false;
-
-STATIC btstack_packet_callback_registration_t hci_event_callback_registration;
-
-STATIC void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
- (void)channel;
- (void)size;
- if (packet_type != HCI_EVENT_PACKET) {
- return;
- }
- switch (hci_event_packet_get_type(packet)) {
- case BTSTACK_EVENT_STATE:
- if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) {
- return;
- }
- gap_local_bd_addr(local_addr);
- if (using_static_address) {
- memcpy(local_addr, static_address, sizeof(local_addr));
- }
- have_addr = true;
- break;
- case HCI_EVENT_COMMAND_COMPLETE:
- if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0) {
- reverse_48(&packet[7], static_address);
- gap_random_address_set(static_address);
- using_static_address = true;
- have_addr = true;
- }
- break;
- default:
- break;
- }
-}
-
-// The IRQ functionality in btstack_run_loop_embedded.c is not used, so the
-// following three functions are empty.
-
-void hal_cpu_disable_irqs(void) {
-}
-
-void hal_cpu_enable_irqs(void) {
-}
-
-void hal_cpu_enable_irqs_and_sleep(void) {
-}
-
-uint32_t hal_time_ms(void) {
- return mp_hal_ticks_ms();
-}
-
-void mp_bluetooth_btstack_port_init(void) {
- static bool run_loop_init = false;
- 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();
- }
-
+void mp_bluetooth_btstack_port_init_usb(void) {
// MICROPYBTUSB can be a ':'' or '-' separated port list.
char *path = getenv("MICROPYBTUSB");
if (path != NULL) {
@@ -128,11 +67,7 @@ void mp_bluetooth_btstack_port_init(void) {
hci_transport_usb_set_path(usb_path_len, usb_path);
}
- // hci_dump_open(NULL, HCI_DUMP_STDOUT);
hci_init(hci_transport_usb_instance(), NULL);
-
- hci_event_callback_registration.callback = &packet_handler;
- hci_add_event_handler(&hci_event_callback_registration);
}
STATIC pthread_t bstack_thread_id;
@@ -142,9 +77,12 @@ void mp_bluetooth_btstack_port_deinit(void) {
// Wait for the poll loop to terminate when the state is set to OFF.
pthread_join(bstack_thread_id, NULL);
- have_addr = false;
}
+
+// Provided by mpbstackport_common.c.
+extern bool mp_bluetooth_hci_poll(void);
+
STATIC void *btstack_thread(void *arg) {
(void)arg;
hci_power_control(HCI_POWER_ON);
@@ -155,19 +93,15 @@ STATIC void *btstack_thread(void *arg) {
// in modbluetooth_btstack.c setting the state back to OFF.
// Or, if a timeout results in it being set to TIMEOUT.
- while (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) {
- // Pretend like we're running in IRQ context (i.e. other things can't be running at the same time).
- mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
- btstack_run_loop_embedded_execute_once();
- MICROPY_END_ATOMIC_SECTION(atomic_state);
+ while (true) {
+ if (!mp_bluetooth_hci_poll()) {
+ break;
+ }
// The USB transport schedules events to the run loop at 1ms intervals,
// and the implementation currently polls rather than selects.
usleep(USB_POLL_INTERVAL_US);
}
-
- hci_close();
-
return NULL;
}
@@ -179,13 +113,4 @@ void mp_bluetooth_btstack_port_start(void) {
pthread_create(&bstack_thread_id, &attr, &btstack_thread, NULL);
}
-void mp_hal_get_mac(int idx, uint8_t buf[6]) {
- if (idx == MP_HAL_MAC_BDADDR) {
- if (!have_addr) {
- mp_raise_OSError(MP_ENODEV);
- }
- memcpy(buf, local_addr, sizeof(local_addr));
- }
-}
-
-#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
+#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_USB
diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h
index 08605842f..9e24ab10c 100644
--- a/ports/unix/mpconfigport.h
+++ b/ports/unix/mpconfigport.h
@@ -309,9 +309,11 @@ void mp_unix_mark_exec(void);
#define MP_STATE_PORT MP_STATE_VM
-#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
+#if MICROPY_PY_BLUETOOTH
+#if MICROPY_BLUETOOTH_BTSTACK
struct _mp_bluetooth_btstack_root_pointers_t;
#define MICROPY_BLUETOOTH_ROOT_POINTERS struct _mp_bluetooth_btstack_root_pointers_t *bluetooth_btstack_root_pointers;
+#endif
#else
#define MICROPY_BLUETOOTH_ROOT_POINTERS
#endif