8000 extmod/btstack: Detect HCI UART init failure. · micropython/micropython@d466483 · GitHub
[go: up one dir, main page]

Skip to content

Commit d466483

Browse files
committed
extmod/btstack: Detect HCI UART init failure.
1 parent 367d02f commit d466483

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

extmod/btstack/btstack_hci_uart.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ STATIC uint8_t *recv_buf;
4949
STATIC size_t recv_len;
5050
STATIC size_t recv_idx;
5151
STATIC void (*recv_handler)(void);
52+
STATIC bool init_success = false;
5253

5354
STATIC int btstack_uart_init(const btstack_uart_config_t *uart_config) {
5455
send_done = false;
@@ -59,14 +60,21 @@ STATIC int btstack_uart_init(const btstack_uart_config_t *uart_config) {
5960

6061
// Set up the UART peripheral, attach IRQ and power up the HCI controller.
6162
// We haven't been told the baud rate yet, so defer that until btstack_uart_set_baudrate.
62-
mp_bluetooth_hci_uart_init(MICROPY_HW_BLE_UART_ID, 0);
63-
mp_bluetooth_hci_controller_init();
63+
if (mp_bluetooth_hci_uart_init(MICROPY_HW_BLE_UART_ID, 0)) {
64+
init_success = false;
65+
return -1;
66+
}
67+
if (mp_bluetooth_hci_controller_init()) {
68+
init_success = false;
69+
return -1;
70+
}
6471

72+
init_success = true;
6573
return 0;
6674
}
6775

6876
STATIC int btstack_uart_open(void) {
69-
return 0;
77+
return init_success ? 0 : 1;
7078
}
7179

7280
STATIC int btstack_uart_close(void) {

extmod/btstack/modbluetooth_btstack.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
317317
// Signal that de-initialisation has completed.
318318
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
319319
}
320+
} else if (event_type == BTSTACK_EVENT_POWERON_FAILED) {
321+
// Signal that initialisation has failed.
322+
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
320323
} else if (event_type == HCI_EVENT_TRANSPORT_PACKET_SENT) {
321324
DEBUG_printf(" --> hci transport packet sent\n");
322325
} else if (event_type == HCI_EVENT_COMMAND_COMPLETE) {
@@ -644,6 +647,8 @@ int mp_bluetooth_init(void) {
644647
if (mp_bluetooth_btstack_state != MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) {
645648
DEBUG_printf("mp_bluetooth_init: stack startup timed out\n");
646649

650+
bool timeout = mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_TIMEOUT;
651+
647652
// Required to stop the polling loop.
648653
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
649654
// Attempt a shutdown (may not do anything).
@@ -652,7 +657,7 @@ int mp_bluetooth_init(void) {
652657
// Clean up.
653658
MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL;
654659

655-
return MP_ETIMEDOUT;
660+
return timeout ? MP_ETIMEDOUT : MP_EINVAL;
656661
}
657662

658663
DEBUG_printf("mp_bluetooth_init: stack startup complete\n");

ports/unix/mphciport.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) {
124124
if (path != NULL) {
125125
strcpy(uart_device_name, path);
126126
}
127-
DEBUG_printf("Using HCI UART: %s\n", uart_device_name);
127+
DEBUG_printf("mp_bluetooth_hci_uart_init: Using HCI UART: %s\n", uart_device_name);
128128

129129
int flags = O_RDWR | O_NOCTTY | O_NONBLOCK;
130130
uart_fd = open(uart_device_name, flags);
131131
if (uart_fd == -1) {
132-
DEBUG_printf("Unable to open port %s", uart_device_name);
132+
printf("mp_bluetooth_hci_uart_init: Unable to open port %s\n", uart_device_name);
133133
return -1;
134134
}
135135

0 commit comments

Comments
 (0)
0