Skip to content

Commit e2561b9

Browse files
committed
Merge branch 'reset-ble-2' into staging/ble
2 parents 7b3e13c + 08471fa commit e2561b9

File tree

6 files changed

+37
-5
lines changed

6 files changed

+37
-5
lines changed

src/reset.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ static void _show_reset_label(bool status)
4848
}
4949
#endif
5050

51-
#if !defined(TESTING)
52-
static void _ble_reset(void)
51+
void reset_ble(void)
5352
{
53+
#if !defined(TESTING)
5454
struct ringbuffer uart_queue;
5555
uint8_t uart_queue_buf[64];
5656
ringbuffer_init(&uart_queue, &uart_queue_buf[0], sizeof(uart_queue_buf));
5757
da14531_reset(&uart_queue);
5858
while (ringbuffer_num(&uart_queue)) {
5959
uart_poll(NULL, 0, NULL, &uart_queue);
6060
}
61-
}
6261
#endif
62+
}
6363

6464
void reset_reset(bool status)
6565
{
@@ -98,7 +98,7 @@ void reset_reset(bool status)
9898

9999
// The ble chip needs to be restarted to load the new secrets.
100100
if (memory_get_platform() == MEMORY_PLATFORM_BITBOX02_PLUS) {
101-
_ble_reset();
101+
reset_ble();
102102
}
103103

104104
reboot();

src/reset.h

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
#include <stdbool.h>
1919

20+
/**
21+
* Restarts the Bluetooth chip. This also means it will re-load the Bluetooth firmware from SPI
22+
* memory.
23+
*/
24+
void reset_ble(void);
25+
2026
/**
2127
* Resets the device:
2228
* - Updates secure chip KDF keys.

src/rust/bitbox02-rust/src/async_usb.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ where
101101

102102
*state = UsbTaskState::Running(Some(task), WaitingForNextRequestState::Idle);
103103
}
104+
// This panic could happen e.g. if someone reconnects to the BitBox while a task is running,
105+
// before the 500ms timeout cancels the task. The proper way to handle would be to let the
106+
// host know we are busy so the host can re-retry after some time.
104107
_ => panic!("spawn: wrong state"),
105108
}
106109
}
@@ -149,7 +152,13 @@ pub fn spin() {
149152
_ => None,
150153
};
151154
if let Some(ref mut task) = popped_task {
152-
match spin_task(task) {
155+
let spin_result = spin_task(task);
156+
if matches!(*USB_TASK_STATE.0.borrow(), UsbTaskState::Nothing) {
157+
// The task was cancelled while it was running, so there is nothing to do with the
158+
// result.
159+
return;
160+
}
161+
match spin_result {
153162
Poll::Ready(result) => {
154163
*USB_TASK_STATE.0.borrow_mut() = UsbTaskState::ResultAvailable(result);
155164
}
@@ -208,6 +217,11 @@ pub fn copy_response(dst: &mut [u8]) -> Result<usize, CopyResponseErr> {
208217

209218
/// Cancel and drop a running task. Returns true if a task was cancelled, false if no task was
210219
/// running.
220+
///
221+
/// Call this inside a running task only if you expect that the host may not be able to read the
222+
/// result (e.g. when resetting the BLE chip as part of a task), so another task can spawn
223+
/// afterwards immediately (before the timeout auto-cancles it), which currently would run into a
224+
/// panic until the response was read and the current task concluded. See the comment in `spawn()`
211225
pub fn cancel() -> bool {
212226
let mut state = USB_TASK_STATE.0.borrow_mut();
213227
if let UsbTaskState::Running(_, _) = *state {

src/rust/bitbox02-rust/src/hww/api/bluetooth.rs

+7
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ async fn process_upgrade(
163163

164164
if response.is_ok() {
165165
hal.ui().status("Upgrade\nsuccessful", true).await;
166+
bitbox02::reset_ble();
167+
if bitbox02::communication_mode_ble_enabled() {
168+
// Since the Bluetooth host will not be there anymore to read this response, this task
169+
// will not be cleared by the executor. We do it manually to make space for the next
170+
// task upon reconnection.
171+
crate::async_usb::cancel();
172+
}
166173
} else {
167174
hal.ui().status("Upgrade failed", false).await;
168175
}

src/rust/bitbox02-sys/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ const ALLOWLIST_FNS: &[&str] = &[
120120
"random_mock_reset",
121121
"reboot_to_bootloader",
122122
"reset_reset",
123+
"reset_ble",
123124
"screen_print_debug",
124125
"screen_process",
125126
"screen_saver_disable",

src/rust/bitbox02/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ pub fn reset(status: bool) {
113113
unsafe { bitbox02_sys::reset_reset(status) }
114114
}
115115

116+
pub fn reset_ble() {
117+
unsafe { bitbox02_sys::reset_ble() }
118+
}
119+
116120
pub struct Tm {
117121
tm: bitbox02_sys::tm,
118122
}

0 commit comments

Comments
 (0)