From 6241c0b9864de1e9f2ad73e965cdc3ebf46db3c0 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Fri, 11 Apr 2025 15:44:13 +0200 Subject: [PATCH] fixup! feat(core): add BLE to bootloader --- .../projects/bootloader/wire/wire_iface_ble.c | 41 +++++++++++++++++-- .../projects/bootloader/wire/wire_iface_ble.h | 4 +- .../workflow/wf_ble_pairing_request.c | 26 ++++-------- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/core/embed/projects/bootloader/wire/wire_iface_ble.c b/core/embed/projects/bootloader/wire/wire_iface_ble.c index 6b23765f49..9d36e4072d 100644 --- a/core/embed/projects/bootloader/wire/wire_iface_ble.c +++ b/core/embed/projects/bootloader/wire/wire_iface_ble.c @@ -144,18 +144,39 @@ void ble_iface_deinit(void) { memset(iface, 0, sizeof(wire_iface_t)); } -void ble_iface_start_pairing(void) { +void ble_iface_end_pairing(void) { ble_state_t state = {0}; ble_get_state(&state); - while (state.connected) { + if (state.peer_count > 0) { + ble_command_t cmd = {.cmd_type = BLE_SWITCH_ON}; + ble_issue_command(&cmd); + } else { + ble_command_t cmd = {.cmd_type = BLE_SWITCH_OFF}; + ble_issue_command(&cmd); + } +} + +bool ble_iface_start_pairing(void) { + ble_state_t state = {0}; + + ble_get_state(&state); + + uint16_t retry_cnt = 0; + + while (state.connected && retry_cnt < 10) { ble_command_t cmd_disconnect = { .cmd_type = BLE_DISCONNECT, }; ble_issue_command(&cmd_disconnect); systick_delay_ms(20); ble_get_state(&state); + retry_cnt++; + } + + if (state.connected) { + return false; } ble_command_t cmd = { @@ -165,9 +186,23 @@ void ble_iface_start_pairing(void) { .name = "Trezor Bootloader", .static_mac = false, }}, - .data_len = 0, }; ble_issue_command(&cmd); + + retry_cnt = 0; + ble_get_state(&state); + while (!state.pairing && retry_cnt < 10) { + systick_delay_ms(20); + ble_get_state(&state); + retry_cnt++; + } + + if (!state.pairing) { + ble_iface_end_pairing(); + return false; + } + + return true; } #endif diff --git a/core/embed/projects/bootloader/wire/wire_iface_ble.h b/core/embed/projects/bootloader/wire/wire_iface_ble.h index 091986b45f..7e1855fd94 100644 --- a/core/embed/projects/bootloader/wire/wire_iface_ble.h +++ b/core/embed/projects/bootloader/wire/wire_iface_ble.h @@ -25,4 +25,6 @@ wire_iface_t* ble_iface_init(void); void ble_iface_deinit(void); -void ble_iface_start_pairing(void); +bool ble_iface_start_pairing(void); + +void ble_iface_end_pairing(void); diff --git a/core/embed/projects/bootloader/workflow/wf_ble_pairing_request.c b/core/embed/projects/bootloader/workflow/wf_ble_pairing_request.c index da904e2f6c..38c8b621ef 100644 --- a/core/embed/projects/bootloader/workflow/wf_ble_pairing_request.c +++ b/core/embed/projects/bootloader/workflow/wf_ble_pairing_request.c @@ -39,23 +39,11 @@ static bool encode_pairing_code(uint32_t code, uint8_t *outbuf) { return true; } -static void end_pairing_mode(void) { - ble_state_t state = {0}; - - ble_get_state(&state); - - if (state.peer_count > 0) { - ble_command_t cmd = {.cmd_type = BLE_SWITCH_ON}; - ble_issue_command(&cmd); - } else { - ble_command_t cmd = {.cmd_type = BLE_SWITCH_OFF}; - ble_issue_command(&cmd); - } -} - workflow_result_t workflow_ble_pairing_request(const vendor_header *const vhdr, const image_header *const hdr) { - ble_iface_start_pairing(); + if (!ble_iface_start_pairing()) { + return WF_OK_PAIRING_FAILED; + } uint8_t buf[1024] = {0}; screen_pairing_mode(ui_get_initial_setup(), buf, sizeof(buf)); @@ -65,12 +53,12 @@ workflow_result_t workflow_ble_pairing_request(const vendor_header *const vhdr, workflow_host_control(vhdr, hdr, buf, sizeof(buf), &code); if (res != WF_OK_UI_ACTION) { - end_pairing_mode(); + ble_iface_end_pairing(); return res; } if (code == PAIRING_MODE_CANCEL) { - end_pairing_mode(); + ble_iface_end_pairing(); return WF_OK_PAIRING_FAILED; } @@ -114,13 +102,13 @@ workflow_result_t workflow_ble_pairing_request(const vendor_header *const vhdr, pairing_mode_finalization_result_t r = screen_pairing_mode_finalizing(ui_get_initial_setup()); if (r == PAIRING_FINALIZATION_FAILED) { - end_pairing_mode(); + ble_iface_end_pairing(); return WF_OK_PAIRING_FAILED; } if (r == PAIRING_FINALIZATION_CANCEL) { ble_command_t disconnect = {.cmd_type = BLE_DISCONNECT}; ble_issue_command(&disconnect); - end_pairing_mode(); + ble_iface_end_pairing(); return WF_OK_PAIRING_FAILED; } }