From a265089ad96687027d4ce24fc1ba8ce57cfd1def Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Wed, 8 Jan 2025 11:25:52 +0100 Subject: [PATCH] fixup! feat(core:): introduce BLE driver --- core/embed/io/ble/inc/io/ble.h | 3 +++ core/embed/io/ble/stm32/ble.c | 29 +++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/core/embed/io/ble/inc/io/ble.h b/core/embed/io/ble/inc/io/ble.h index 432b367648..6c8ca26e60 100644 --- a/core/embed/io/ble/inc/io/ble.h +++ b/core/embed/io/ble/inc/io/ble.h @@ -56,6 +56,9 @@ typedef struct { typedef struct { bool connected; + bool connectable; + bool pairing; + bool pairing_requested; uint8_t peer_count; } ble_state_t; diff --git a/core/embed/io/ble/stm32/ble.c b/core/embed/io/ble/stm32/ble.c index 456c45db77..961494c2f6 100644 --- a/core/embed/io/ble/stm32/ble.c +++ b/core/embed/io/ble/stm32/ble.c @@ -51,6 +51,7 @@ typedef struct { bool initialized; bool status_valid; bool accept_msgs; + bool pairing_requested; ble_event_t event_queue_buffers[EVENT_QUEUE_LEN]; tsqueue_entry_t event_queue_entries[EVENT_QUEUE_LEN]; tsqueue_t event_queue; @@ -103,14 +104,26 @@ static bool ble_send_disconnect(void) { static bool ble_send_pairing_reject(void) { uint8_t cmd = INTERNAL_CMD_REJECT_PAIRING; - return nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL) >= - 0; + bool result = + nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL); + + if (result) { + g_ble_driver.pairing_requested = false; + } + + return result; } static bool ble_send_pairing_accept(void) { uint8_t cmd = INTERNAL_CMD_ALLOW_PAIRING; - return nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL) >= - 0; + bool result = + nrf_send_msg(NRF_SERVICE_BLE_MANAGER, &cmd, sizeof(cmd), NULL, NULL); + + if (result) { + g_ble_driver.pairing_requested = false; + } + + return result; } static void ble_process_rx_msg_status(const uint8_t *data, uint32_t len) { @@ -152,6 +165,8 @@ static void ble_process_rx_msg_status(const uint8_t *data, uint32_t len) { if (drv->mode_current == BLE_MODE_PAIRING) { drv->mode_requested = BLE_MODE_CONNECTABLE; } + + drv->pairing_requested = false; } drv->connected = msg.connected; @@ -193,6 +208,8 @@ static void ble_process_rx_msg_pairing_request(const uint8_t *data, if (!tsqueue_enqueue(&drv->event_queue, (uint8_t *)&event, sizeof(event), NULL)) { ble_send_pairing_reject(); + } else { + drv->pairing_requested = true; } } @@ -205,6 +222,7 @@ static void ble_process_rx_msg_pairing_cancelled(const uint8_t *data, ble_event_t event = {.type = BLE_PAIRING_CANCELLED, .data_len = 0}; tsqueue_enqueue(&drv->event_queue, (uint8_t *)&event, sizeof(event), NULL); + drv->pairing_requested = false; } static void ble_process_rx_msg(const uint8_t *data, uint32_t len) { @@ -489,6 +507,9 @@ void ble_get_state(ble_state_t *state) { state->connected = drv->connected; state->peer_count = drv->peer_count; + state->pairing = drv->mode_current == BLE_MODE_PAIRING; + state->connectable = drv->mode_current == BLE_MODE_CONNECTABLE; + state->pairing_requested = drv->pairing_requested; } #endif