From 191fa1a026fb38d579b593ff0ccff0fe2f32bccd Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Fri, 3 Jan 2025 11:42:09 +0100 Subject: [PATCH] fixup! feat(core:): introduce BLE driver --- core/embed/io/ble/stm32/ble.c | 21 +++++++++++---------- core/embed/io/nrf/stm32u5/nrf.c | 4 ++++ core/embed/util/tsqueue/tsqueue.c | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/core/embed/io/ble/stm32/ble.c b/core/embed/io/ble/stm32/ble.c index e743f5ffe7..aafeda741a 100644 --- a/core/embed/io/ble/stm32/ble.c +++ b/core/embed/io/ble/stm32/ble.c @@ -57,10 +57,11 @@ typedef struct { tsqueue_entry_t data_queue_entries[DATA_QUEUE_LEN]; tsqueue_t data_queue; - uint8_t send_buffer[NRF_MAX_TX_DATA_SIZE]; + uint8_t send_buffer[SEND_QUEUE_LEN][NRF_MAX_TX_DATA_SIZE]; tsqueue_entry_t send_queue_entries[SEND_QUEUE_LEN]; tsqueue_t send_queue; + systimer_t *timer; uint16_t ping_cntr; } ble_driver_t; @@ -84,9 +85,6 @@ static void ble_send_advertising_off(void) { } static bool ble_send_erase_bonds(void) { - if (!nrf_is_running()) { - return false; - } uint8_t cmd = INTERNAL_CMD_ERASE_BONDS; nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL); @@ -94,9 +92,6 @@ static bool ble_send_erase_bonds(void) { } static bool ble_send_disconnect(void) { - if (!nrf_is_running()) { - return false; - } uint8_t cmd = INTERNAL_CMD_DISCONNECT; nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL); @@ -205,6 +200,10 @@ static void ble_process_rx_msg_pairing_cancelled(const uint8_t *data, } static void ble_process_rx_msg(const uint8_t *data, uint32_t len) { + if (len < 1) { + return; + } + switch (data[0]) { case INTERNAL_EVENT_STATUS: ble_process_rx_msg_status(data, len); @@ -300,14 +299,14 @@ void ble_init(void) { (uint8_t *)drv->send_buffer, NRF_MAX_TX_DATA_SIZE, SEND_QUEUE_LEN); - systimer_t *timer = systimer_create(ble_loop, NULL); + drv->timer = systimer_create(ble_loop, NULL); - systimer_set_periodic(timer, LOOP_PERIOD_MS); + systimer_set_periodic(drv->timer, LOOP_PERIOD_MS); nrf_init(); nrf_register_listener(NRF_SERVICE_BLE_MANAGER, ble_process_rx_msg); nrf_register_listener(NRF_SERVICE_BLE, ble_process_data); - + ; drv->initialized = true; } @@ -318,6 +317,8 @@ void ble_deinit(void) { return; } + systimer_delete(drv->timer); + tsqueue_reset(&drv->event_queue); tsqueue_reset(&drv->data_queue); tsqueue_reset(&drv->send_queue); diff --git a/core/embed/io/nrf/stm32u5/nrf.c b/core/embed/io/nrf/stm32u5/nrf.c index 5e02f2dc71..b6152cead6 100644 --- a/core/embed/io/nrf/stm32u5/nrf.c +++ b/core/embed/io/nrf/stm32u5/nrf.c @@ -439,6 +439,10 @@ int32_t nrf_send_msg(nrf_service_id_t service, const uint8_t *data, return -1; } + if (!nrf_is_running()) { + return -1; + } + int32_t id = 0; nrf_uart_tx_data_t buffer; diff --git a/core/embed/util/tsqueue/tsqueue.c b/core/embed/util/tsqueue/tsqueue.c index eb04507dca..3c63166e0d 100644 --- a/core/embed/util/tsqueue/tsqueue.c +++ b/core/embed/util/tsqueue/tsqueue.c @@ -71,7 +71,7 @@ static int32_t get_next_id(tsqueue_t *queue) { val = queue->next_id; queue->next_id++; } else { - queue->next_id = 1; + queue->next_id = 2; } return val; }