diff --git a/core/embed/io/ble/stm32/ble.c b/core/embed/io/ble/stm32/ble.c index f8b068433d..456c45db77 100644 --- a/core/embed/io/ble/stm32/ble.c +++ b/core/embed/io/ble/stm32/ble.c @@ -69,45 +69,48 @@ typedef struct { static ble_driver_t g_ble_driver = {0}; -static void ble_send_state_request(void) { +static bool ble_send_state_request(void) { uint8_t cmd = INTERNAL_CMD_PING; - nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL); + return nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL) >= + 0; } -static void ble_send_advertising_on(bool whitelist) { +static bool ble_send_advertising_on(bool whitelist) { uint8_t data[2]; data[0] = INTERNAL_CMD_ADVERTISING_ON; data[1] = whitelist ? 1 : 0; - nrf_send_msg(NRF_SERVICE_BLE_MANAGER, data, sizeof(data), NULL, NULL); + return nrf_send_msg(NRF_SERVICE_BLE_MANAGER, data, sizeof(data), NULL, + NULL) >= 0; } -static void ble_send_advertising_off(void) { +static bool ble_send_advertising_off(void) { uint8_t cmd = INTERNAL_CMD_ADVERTISING_OFF; - nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL); + return nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL) >= + 0; } static bool ble_send_erase_bonds(void) { uint8_t cmd = INTERNAL_CMD_ERASE_BONDS; - nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL); - - return true; + return nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL) >= + 0; } static bool ble_send_disconnect(void) { uint8_t cmd = INTERNAL_CMD_DISCONNECT; - nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL); - - return true; + return nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL) >= + 0; } -static void ble_send_pairing_reject(void) { +static bool ble_send_pairing_reject(void) { uint8_t cmd = INTERNAL_CMD_REJECT_PAIRING; - nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL); + return nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL) >= + 0; } -static void ble_send_pairing_accept(void) { +static bool ble_send_pairing_accept(void) { uint8_t cmd = INTERNAL_CMD_ALLOW_PAIRING; - nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL); + return nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL) >= + 0; } static void ble_process_rx_msg_status(const uint8_t *data, uint32_t len) { @@ -187,7 +190,10 @@ static void ble_process_rx_msg_pairing_request(const uint8_t *data, ble_event_t event = {.type = BLE_PAIRING_REQUEST, .data_len = 6}; memcpy(event.data, &data[1], 6); - tsqueue_enqueue(&drv->event_queue, (uint8_t *)&event, sizeof(event), NULL); + if (!tsqueue_enqueue(&drv->event_queue, (uint8_t *)&event, sizeof(event), + NULL)) { + ble_send_pairing_reject(); + } } static void ble_process_rx_msg_pairing_cancelled(const uint8_t *data, @@ -430,6 +436,8 @@ bool ble_issue_command(ble_command_t command) { return false; } + bool result = false; + switch (command) { case BLE_SWITCH_OFF: drv->mode_requested = BLE_MODE_OFF; @@ -441,23 +449,23 @@ bool ble_issue_command(ble_command_t command) { drv->mode_requested = BLE_MODE_PAIRING; break; case BLE_DISCONNECT: - ble_send_disconnect(); + result = ble_send_disconnect(); break; case BLE_ERASE_BONDS: - ble_send_erase_bonds(); + result = ble_send_erase_bonds(); break; case BLE_ALLOW_PAIRING: - ble_send_pairing_accept(); + result = ble_send_pairing_accept(); break; case BLE_REJECT_PAIRING: - ble_send_pairing_reject(); + result = ble_send_pairing_reject(); break; default: // unknown command return false; } - return true; + return result; } bool ble_get_event(ble_event_t *event) { diff --git a/core/embed/util/tsqueue/inc/util/tsqueue.h b/core/embed/util/tsqueue/inc/util/tsqueue.h index bd97309f88..653ce7d91d 100644 --- a/core/embed/util/tsqueue/inc/util/tsqueue.h +++ b/core/embed/util/tsqueue/inc/util/tsqueue.h @@ -32,16 +32,16 @@ typedef struct { typedef struct { tsqueue_entry_t *entries; // Array of queue entries - int rix; // Read index - int wix; // Write index - int qlen; // Queue length + uint16_t rix; // Read index + uint16_t wix; // Write index + uint16_t qlen; // Queue length uint16_t size; // Size of each buffer int32_t next_id; // ID of the next item } tsqueue_t; // Initialize the queue void tsqueue_init(tsqueue_t *queue, tsqueue_entry_t *entries, - uint8_t *buffer_mem, uint16_t size, int qlen); + uint8_t *buffer_mem, uint16_t size, uint16_t qlen); void tsqueue_reset(tsqueue_t *queue); diff --git a/core/embed/util/tsqueue/tsqueue.c b/core/embed/util/tsqueue/tsqueue.c index 3c63166e0d..d0fb408c28 100644 --- a/core/embed/util/tsqueue/tsqueue.c +++ b/core/embed/util/tsqueue/tsqueue.c @@ -24,23 +24,18 @@ // Initialize the queue void tsqueue_init(tsqueue_t *queue, tsqueue_entry_t *entries, - uint8_t *buffer_mem, uint16_t size, int qlen) { + uint8_t *buffer_mem, uint16_t size, uint16_t qlen) { irq_key_t key = irq_lock(); queue->entries = entries; - queue->rix = 0; - queue->wix = 0; queue->qlen = qlen; queue->size = size; - queue->next_id = 1; for (int i = 0; i < qlen; i++) { - if (buffer_mem != NULL) { - queue->entries[i].buffer = buffer_mem + i * size; - memset(queue->entries[i].buffer, 0, size); - } - queue->entries[i].len = 0; + queue->entries[i].buffer = buffer_mem + i * size; } + tsqueue_reset(queue); + irq_unlock(key); }