libjade: implement delay functions
What changed, and why it matters
This commit adds helper functions to convert FreeRTOS tick counts into regular clock time, and uses them to implement delay functions for the Blockstream Jade hardware wallet's simulator build. It is a normal feature/fix for the non-firmware simulator environment and does not appear to address a security vulnerability.
No security action required. Treat as routine development. If reviewing further, verify that `portTICK_PERIOD_MS == 1` assumption holds for all supported simulator build configurations and that `nanosleep()` behavior is acceptable for test determinism.
Security signals we found
No security-relevant signals observed in the diff.
Change is limited to simulator delay/timing implementation.
No memory safety issues, cryptographic changes, or input handling changes are present.
Evidence from the diff
The patch introduces timecvt.h, which provides inline helpers timespec_from_ticktype() and absolute_timespec_from_ticktype() for converting FreeRTOS TickType_t ticks to struct timespec, assuming portTICK_PERIOD_MS == 1. It then wires these into vTaskDelay() and vTaskDelayUntil() in libjade.c so that the simulator build (when GUI is enabled) sleeps via nanosleep() instead of being a no-op. The no-GUI simulator path remains unchanged. The change is infrastructure for simulator timing/GUI framerate control.
Changed components
libjade/include/freertos/timecvt.hlibjade/libjade.cFreeRTOS task delay simulation in Blockstream Jade simulator buildInspect captured patch +53 / −2
diff --git a/libjade/include/freertos/timecvt.h b/libjade/include/freertos/timecvt.h
new file mode 100644
index 0000000..ede4506
--- /dev/null
+++ b/libjade/include/freertos/timecvt.h
@@ -0,0 +1,38 @@
+#ifndef _LIBJADE_FREERTOS_TIMECVT_H_
+#define _LIBJADE_FREERTOS_TIMECVT_H_ 1
+
+#include "task.h"
+#include <time.h>
+
+#define TIMECVT_STATIC_ASSERT(cond) \
+ do { \
+ (void)sizeof(char[1 - 2 * !(cond)]); \
+ } while (0)
+
+static inline struct timespec timespec_from_ticktype(TickType_t ticks)
+{
+ // The code below is based on portTICK_PERIOD_MS == 1
+ TIMECVT_STATIC_ASSERT(portTICK_PERIOD_MS == 1);
+ struct timespec ts;
+ ts.tv_sec = ticks / 1000;
+ ts.tv_nsec = (ticks % 1000) * 1000000;
+ return ts;
+}
+
+#undef TIMECVT_STATIC_ASSERT
+
+static inline struct timespec absolute_timespec_from_ticktype(TickType_t ticks)
+{
+ struct timespec now;
+ clock_gettime(CLOCK_REALTIME, &now);
+ struct timespec timeout = timespec_from_ticktype(ticks);
+ timeout.tv_sec += now.tv_sec;
+ timeout.tv_nsec += now.tv_nsec;
+ if (timeout.tv_nsec >= 1000000000) {
+ timeout.tv_sec += 1;
+ timeout.tv_nsec -= 1000000000;
+ }
+ return timeout;
+}
+
+#endif // _LIBJADE_FREERTOS_TIMECVT_H_
diff --git a/libjade/libjade.c b/libjade/libjade.c
index 1acbcf1..a8e8e0c 100644
--- a/libjade/libjade.c
+++ b/libjade/libjade.c
@@ -18,6 +18,7 @@
#undef _GNU_SOURCE
#include "sdkconfig.h"
+#include "freertos/timecvt.h"
#include "libjade.h"
#include "icons.inc"
@@ -80,6 +81,7 @@ static int settimeofday_no_op(const void* yv, const void* tz) { return 0; }
#define settimeofday settimeofday_no_op
#ifndef CONFIG_LIBJADE_NO_GUI
+#include "main/display.h"
#include "main/gui.h"
typedef void* locale_multilang_string_t;
@@ -242,14 +244,25 @@ unsigned int xPortGetFreeHeapSize(void) { return 0xffffff; }
void vTaskDelay(TickType_t delay)
{
+#ifdef CONFIG_LIBJADE_NO_GUI
// Don't delay, since we don't have multiple threads running
// in the firmware to wait on.
+#else
+ struct timespec ts = timespec_from_ticktype(delay);
+ nanosleep(&ts, NULL);
+#endif
}
void vTaskDelayUntil(TickType_t* prev_wake_time, const TickType_t delay)
{
- // Used to control the GUI refresh framerate
- // FIXME: Implement
+#ifndef CONFIG_LIBJADE_NO_GUI
+ // Only used by the GUI main loop
+ TickType_t current_time = xTaskGetTickCount();
+ if (*prev_wake_time + delay > current_time) {
+ vTaskDelay(*prev_wake_time + delay - current_time);
+ }
+ *prev_wake_time += delay;
+#endif
}
void vTaskDelete(void* task)
Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.