aboutsummaryrefslogtreecommitdiff
path: root/ports/unix
diff options
context:
space:
mode:
authorJim Mussared2020-08-19 12:55:38 +1000
committerDamien George2020-09-08 12:53:24 +1000
commit126f972c34f25bffc07d0d1cb3e536921fec0979 (patch)
treea335c247c2075ca93a1cddb52ac002c11fc46c5f /ports/unix
parent311b8519af75be9fe4215b5ee8f77e91d089d5df (diff)
extmod/nimble: Add timeout for HCI sync on startup.
This allows `ble.active(1)` to fail correctly if the HCI controller is unavailable. It also avoids an infine loop in the NimBLE event handler where NimBLE doesn't correctly detect that the HCI controller is unavailable and keeps trying to reset. Furthermore, it fixes an issue where GATT service registrations were left allocated, which led to a bad realloc if the stack was activated multiple times.
Diffstat (limited to 'ports/unix')
-rw-r--r--ports/unix/mpbthciport.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/ports/unix/mpbthciport.c b/ports/unix/mpbthciport.c
index dbfb5e0d0..316a8831f 100644
--- a/ports/unix/mpbthciport.c
+++ b/ports/unix/mpbthciport.c
@@ -105,6 +105,10 @@ STATIC int configure_uart(void) {
// Apply immediately.
if (tcsetattr(uart_fd, TCSANOW, &toptions) < 0) {
DEBUG_printf("Couldn't set term attributes");
+
+ close(uart_fd);
+ uart_fd = -1;
+
return -1;
}
@@ -149,6 +153,10 @@ int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) {
int mp_bluetooth_hci_uart_deinit(void) {
DEBUG_printf("mp_bluetooth_hci_uart_deinit\n");
+ if (uart_fd == -1) {
+ return 0;
+ }
+
// Wait for the poll loop to terminate when the state is set to OFF.
pthread_join(hci_poll_thread_id, NULL);
@@ -168,6 +176,10 @@ int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) {
int mp_bluetooth_hci_uart_readchar(void) {
// DEBUG_printf("mp_bluetooth_hci_uart_readchar\n");
+ if (uart_fd == -1) {
+ return -1;
+ }
+
uint8_t c;
ssize_t bytes_read = read(uart_fd, &c, 1);
@@ -184,6 +196,10 @@ int mp_bluetooth_hci_uart_readchar(void) {
int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) {
// DEBUG_printf("mp_bluetooth_hci_uart_write\n");
+ if (uart_fd == -1) {
+ return 0;
+ }
+
#if DEBUG_HCI_DUMP
printf("[% 8ld] TX: %02x", mp_hal_ticks_ms(), buf[0]);
for (size_t i = 1; i < len; ++i) {