refactor(core): move haptic driver bindings to io crate
What changed, and why it matters
This commit is a straightforward internal code reorganization: it moves the Rust bindings for the device's vibration motor (haptic feedback) from one internal crate to another. There is no change to security-sensitive logic, no bug fix, and no externally visible behavior change. It is purely a refactor.
No security action required. Treat as normal code maintenance/review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change relocates haptic driver FFI bindings and the safe Rust wrapper (HapticEffect, play, play_custom) from core/embed/rust/src/trezorhal/haptic.rs into the io crate (core/embed/io/src/haptic.rs). Call sites are updated to import from io::haptic instead of crate::trezorhal::haptic. The wrapper now returns Result<(), Error> and callers discard the result with let _ = ..., whereas the old wrapper logged errors via ulog. Bindings generation is moved from trezorhal build script to the io/haptic build script. The haptic Cargo feature now enables io/haptic.
Changed components
core/embed/io/haptic build bindingscore/embed/rust/src/trezorhal/haptic.rs (removed)core/embed/io/src/haptic.rs (new)UI button/loader/hold-to-confirm components using haptic feedbackInspect captured patch +74 / −75
### core/embed/io/build.rs
@@ -43,6 +43,9 @@ fn main() -> Result<()> {
xbuild::build(|lib| {
lib.import_lib("sec")?;
+ // `ts_t` is re-exported from the `rtl` crate, see `src/ffi.rs`.
+ lib.configure_rust_bindings(|builder| Ok(builder.blocklist_type("ts_t")));
+
build_mods!(
lib,
[
### core/embed/io/haptic/build.rs
@@ -13,5 +13,13 @@ pub fn def_module(lib: &mut CLibrary) -> Result<()> {
bail_unsupported!();
}
+ lib.add_rust_bindings(|builder| {
+ Ok(builder
+ .header("haptic/inc/io/haptic.h")
+ .allowlist_type("haptic_effect_t")
+ .allowlist_function("haptic_play")
+ .allowlist_function("haptic_play_custom"))
+ })?;
+
Ok(())
}
### core/embed/io/src/ffi.rs
@@ -1,5 +1,8 @@
#![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)]
#![allow(dead_code)]
+#![allow(unused_imports)]
+
+use rtl::error::ts_t;
include!(concat!(env!("OUT_DIR"), "/io.rs"));
### core/embed/io/src/haptic.rs
@@ -0,0 +1,18 @@
+use rtl::error::Error;
+
+use super::ffi;
+
+#[derive(PartialEq, Debug, Eq, Clone, Copy)]
+pub enum HapticEffect {
+ ButtonPress = ffi::haptic_effect_t_HAPTIC_BUTTON_PRESS as _,
+ HoldToConfirm = ffi::haptic_effect_t_HAPTIC_HOLD_TO_CONFIRM as _,
+ BootloaderEntry = ffi::haptic_effect_t_HAPTIC_BOOTLOADER_ENTRY as _,
+}
+
+pub fn play(effect: HapticEffect) -> Result<(), Error> {
+ unsafe { ffi::haptic_play(effect as _).ok() }
+}
+
+pub fn play_custom(amplitude_pct: i8, duration_ms: u16) -> Result<(), Error> {
+ unsafe { ffi::haptic_play_custom(amplitude_pct, duration_ms).ok() }
+}
### core/embed/io/src/lib.rs
@@ -5,6 +5,8 @@
mod ffi;
+#[cfg(feature = "haptic")]
+pub mod haptic;
#[cfg(feature = "nrf")]
pub mod nrf;
#[cfg(feature = "smp")]
### core/embed/rust/Cargo.toml
@@ -70,7 +70,7 @@ display_rgb565 = ["ui_antialiasing"]
display_rgba8888 = ["ui_antialiasing"]
dma2d = []
framebuffer = []
-haptic = []
+haptic = ["io/haptic"]
hw_jpeg_decoder = []
layout_bolt = []
layout_caesar = []
### core/embed/rust/build.rs
@@ -294,10 +294,6 @@ fn generate_trezorhal_bindings(lib: &mut CLibrary) -> Result<()> {
.allowlist_type("button_t")
.allowlist_type("button_event_t")
.allowlist_function("button_get_event")
- // haptic
- .allowlist_type("haptic_effect_t")
- .allowlist_function("haptic_play")
- .allowlist_function("haptic_play_custom")
// jpegdec
.allowlist_var("JPEGDEC_RGBA8888_BUFFER_SIZE")
.allowlist_var("JPEGDEC_MONO8_BUFFER_SIZE")
### core/embed/rust/src/bootloader/mod.rs
@@ -1,4 +1,6 @@
use heapless::Vec;
+#[cfg(all(feature = "haptic", feature = "power_manager"))]
+use io::haptic::{play, HapticEffect};
#[cfg(feature = "power_manager")]
use sys::time::{Duration, Instant};
@@ -9,8 +11,6 @@ use crate::trezorhal::bootloader::{bootloader_process_usb, BootloaderWFResult};
use crate::trezorhal::bootloader::{
debuglink_notify_layout_change, debuglink_process, DebuglinkResult,
};
-#[cfg(all(feature = "haptic", feature = "power_manager"))]
-use crate::trezorhal::haptic::{play, HapticEffect};
use crate::trezorhal::sysevent::{sysevents_poll, Syshandle};
use crate::ui::component::base::AttachType;
use crate::ui::component::{Component, Event, EventCtx};
@@ -26,6 +26,7 @@ use crate::{
ui::display::fade_backlight_duration,
ui::event::PhysicalButton,
};
+
#[cfg(feature = "power_manager")]
const FADE_TIME: Duration = Duration::from_millis(30000);
#[cfg(feature = "power_manager")]
@@ -108,7 +109,7 @@ pub fn run(
if let Some(t) = button_pressed_time {
if let Some(elapsed) = Instant::now().checked_duration_since(t) {
if elapsed >= HIBERNATE_TIME && !haptic_played {
- play(HapticEffect::BootloaderEntry);
+ let _ = play(HapticEffect::BootloaderEntry);
haptic_played = true;
}
}
### core/embed/rust/src/trezorhal/haptic.rs
@@ -1,26 +0,0 @@
-use sys::ulog;
-
-use super::ffi;
-
-#[derive(PartialEq, Debug, Eq, FromPrimitive, Clone, Copy)]
-pub enum HapticEffect {
- ButtonPress = ffi::haptic_effect_t_HAPTIC_BUTTON_PRESS as _,
- HoldToConfirm = ffi::haptic_effect_t_HAPTIC_HOLD_TO_CONFIRM as _,
- BootloaderEntry = ffi::haptic_effect_t_HAPTIC_BOOTLOADER_ENTRY as _,
-}
-
-pub fn play(effect: HapticEffect) {
- unsafe {
- if !ffi::haptic_play(effect as _).is_ok() {
- ulog::error!("haptic_play failed");
- }
- }
-}
-
-pub fn play_custom(amplitude_pct: i8, duration_ms: u16) {
- unsafe {
- if !ffi::haptic_play_custom(amplitude_pct, duration_ms).is_ok() {
- ulog::error!("haptic_play_custom failed");
- }
- }
-}
### core/embed/rust/src/trezorhal/mod.rs
@@ -4,8 +4,6 @@ pub mod bitblt;
pub mod ble;
pub mod display;
mod ffi;
-#[cfg(feature = "haptic")]
-pub mod haptic;
#[cfg(feature = "button")]
pub mod button;
### core/embed/rust/src/ui/layout_bolt/component/button.rs
@@ -1,9 +1,9 @@
+#[cfg(feature = "haptic")]
+use io::haptic::{self, HapticEffect};
use sys::time::ShortDuration;
use super::theme;
use crate::strutil::TString;
-#[cfg(feature = "haptic")]
-use crate::trezorhal::haptic::{self, HapticEffect};
use crate::ui::component::{
Component, ComponentExt, Event, EventCtx, FixedHeightBar, MsgMap, Split, Timer,
};
@@ -259,7 +259,7 @@ impl Component for Button {
if touch_area.contains(pos) {
#[cfg(feature = "haptic")]
if self.haptics {
- haptic::play(HapticEffect::ButtonPress);
+ let _ = haptic::play(HapticEffect::ButtonPress);
}
self.set(ctx, State::Pressed);
if self.long_press != ShortDuration::ZERO {
@@ -303,7 +303,7 @@ impl Component for Button {
if matches!(self.state, State::Pressed) {
#[cfg(feature = "haptic")]
if self.haptics {
- haptic::play(HapticEffect::ButtonPress);
+ let _ = haptic::play(HapticEffect::ButtonPress);
}
self.set(ctx, State::Initial);
return Some(ButtonMsg::LongPressed);
### core/embed/rust/src/ui/layout_bolt/component/loader.rs
@@ -1,10 +1,10 @@
+#[cfg(feature = "haptic")]
+use io::haptic::{self, HapticEffect};
use sys::time::{Duration, Instant};
use super::super::constant;
use super::super::cshape::{render_loader, LoaderRange};
use super::theme;
-#[cfg(feature = "haptic")]
-use crate::trezorhal::haptic::{self, HapticEffect};
use crate::ui::animation::Animation;
use crate::ui::component::{Component, Event, EventCtx, Pad};
use crate::ui::display::toif::Icon;
@@ -176,7 +176,7 @@ impl Component for Loader {
if self.is_completely_grown(now) {
#[cfg(feature = "haptic")]
- haptic::play(HapticEffect::HoldToConfirm);
+ let _ = haptic::play(HapticEffect::HoldToConfirm);
return Some(LoaderMsg::GrownCompletely);
} else if self.is_completely_shrunk(now) {
return Some(LoaderMsg::ShrunkCompletely);
@@ -189,7 +189,7 @@ impl Component for Loader {
let progress = f32::from(self.progress(now).unwrap())
/ f32::from(display::LOADER_MAX);
let ampl = i16::lerp(0, HAPTIC_AMPLITUDE_MAX_PCT, progress);
- haptic::play_custom(ampl as i8, HAPTIC_AMPLITUDE_DURATION_MS);
+ let _ = haptic::play_custom(ampl as i8, HAPTIC_AMPLITUDE_DURATION_MS);
}
}
### core/embed/rust/src/ui/layout_delizia/component/button.rs
@@ -1,9 +1,9 @@
+#[cfg(feature = "haptic")]
+use io::haptic::{play, HapticEffect};
use sys::time::ShortDuration;
use super::theme;
use crate::strutil::TString;
-#[cfg(feature = "haptic")]
-use crate::trezorhal::haptic::{play, HapticEffect};
use crate::ui::component::text::{LineBreaking, PageBreaking, TextStyle};
use crate::ui::component::{Component, Event, EventCtx, TextLayout, Timer};
use crate::ui::display::toif::Icon;
@@ -275,7 +275,7 @@ impl Component for Button {
if touch_area.contains(pos) {
#[cfg(feature = "haptic")]
if self.haptic {
- play(HapticEffect::ButtonPress);
+ let _ = play(HapticEffect::ButtonPress);
}
self.set(ctx, State::Pressed);
if self.long_press != ShortDuration::ZERO {
@@ -345,7 +345,7 @@ impl Component for Button {
if matches!(self.state, State::Pressed) {
#[cfg(feature = "haptic")]
if self.haptic {
- play(HapticEffect::ButtonPress);
+ let _ = play(HapticEffect::ButtonPress);
}
self.set(ctx, State::Initial);
return Some(ButtonMsg::LongPressed);
### core/embed/rust/src/ui/layout_delizia/component/hold_to_confirm.rs
@@ -1,12 +1,12 @@
+#[cfg(feature = "haptic")]
+use io::haptic::{self, HapticEffect};
use pareen;
use sys::time::{Duration, ShortDuration};
use super::theme::{self, TITLE_HEIGHT};
use super::{Button, ButtonContent, ButtonMsg};
use crate::time::Stopwatch;
use crate::translations::TR;
-#[cfg(feature = "haptic")]
-use crate::trezorhal::haptic::{self, HapticEffect};
use crate::ui::component::{Component, Event, EventCtx, Label};
use crate::ui::constant::screen;
use crate::ui::display::Color;
@@ -221,7 +221,7 @@ impl Component for HoldToConfirm {
Some(ButtonMsg::Clicked) => {
if animation_disabled() {
#[cfg(feature = "haptic")]
- haptic::play(HapticEffect::HoldToConfirm);
+ let _ = haptic::play(HapticEffect::HoldToConfirm);
return Some(());
}
if !self.anim.is_locked() {
@@ -247,7 +247,7 @@ impl Component for HoldToConfirm {
if self.anim.is_locked() && !self.finalizing {
self.finalizing = true;
#[cfg(feature = "haptic")]
- haptic::play(HapticEffect::HoldToConfirm);
+ let _ = haptic::play(HapticEffect::HoldToConfirm);
}
} else if self.anim.is_locked() {
return Some(());
@@ -337,7 +337,7 @@ impl Component for HoldToConfirm {
{
let hap = self.anim.get_haptic(elapsed);
if hap > 0 && !self.anim.is_locked() {
- haptic::play_custom(hap as i8, 100);
+ let _ = haptic::play_custom(hap as i8, 100);
}
}
}
### core/embed/rust/src/ui/layout_delizia/component/homescreen.rs
@@ -1,3 +1,5 @@
+#[cfg(feature = "haptic")]
+use io::haptic::{play, HapticEffect};
use sys::time::{Duration, Instant};
use super::super::cshape::{self, UnlockOverlay};
@@ -9,8 +11,6 @@ use crate::micropython::Error;
use crate::strutil::TString;
use crate::time::Stopwatch;
use crate::translations::TR;
-#[cfg(feature = "haptic")]
-use crate::trezorhal::haptic::{play, HapticEffect};
use crate::trezorhal::usb::usb_configured;
use crate::ui::component::base::AttachType;
use crate::ui::component::{Component, Event, EventCtx, Label, Timer};
@@ -887,7 +887,7 @@ impl Component for Lockscreen {
// Always dismiss on TouchEnd in the middle area
Event::Touch(TouchEvent::TouchEnd(pos)) if middle(pos) => {
#[cfg(feature = "haptic")]
- play(HapticEffect::ButtonPress);
+ let _ = play(HapticEffect::ButtonPress);
return Some(HomescreenMsg::Dismissed);
}
// Do nothing on TouchStart in the middle area
### core/embed/rust/src/ui/layout_delizia/component/loader.rs
@@ -1,9 +1,9 @@
+#[cfg(feature = "haptic")]
+use io::haptic::{play, HapticEffect};
use sys::time::{Duration, Instant};
use super::super::cshape::{render_loader, LoaderRange};
use super::{constant, theme};
-#[cfg(feature = "haptic")]
-use crate::trezorhal::haptic::{play, HapticEffect};
use crate::ui::animation::Animation;
use crate::ui::component::{Component, Event, EventCtx, Pad};
use crate::ui::display::toif::Icon;
@@ -180,7 +180,7 @@ impl Component for Loader {
if self.is_completely_grown(now) {
#[cfg(feature = "haptic")]
- play(HapticEffect::HoldToConfirm);
+ let _ = play(HapticEffect::HoldToConfirm);
return Some(LoaderMsg::GrownCompletely);
} else if self.is_completely_shrunk(now) {
return Some(LoaderMsg::ShrunkCompletely);
### core/embed/rust/src/ui/layout_eckhart/component/button.rs
@@ -1,12 +1,12 @@
+#[cfg(feature = "haptic")]
+use io::haptic::{play, HapticEffect};
use sys::time::{Duration, Instant, ShortDuration};
use super::super::component::ConnectionIndicator;
use super::super::theme::{self, Gradient};
use crate::strutil::TString;
#[cfg(feature = "translations")]
use crate::translations::TR;
-#[cfg(feature = "haptic")]
-use crate::trezorhal::haptic::{play, HapticEffect};
use crate::ui::component::text::TextStyle;
use crate::ui::component::{Component, Event, EventCtx, Marquee, Timer};
use crate::ui::constant;
@@ -788,7 +788,7 @@ impl Component for Button {
if touch_area.contains(pos) {
#[cfg(feature = "haptic")]
if matches!(self.haptic, HapticMode::OnPress) {
- play(HapticEffect::ButtonPress);
+ let _ = play(HapticEffect::ButtonPress);
}
self.set(ctx, State::Pressed);
if let Some((duration, _danger)) = self.long_press() {
@@ -820,7 +820,7 @@ impl Component for Button {
// Touch finished in our area, we got clicked.
#[cfg(feature = "haptic")]
if matches!(self.haptic, HapticMode::OnClick) {
- play(HapticEffect::ButtonPress);
+ let _ = play(HapticEffect::ButtonPress);
}
self.set(ctx, State::Initial);
return Some(ButtonMsg::Clicked);
@@ -862,7 +862,7 @@ impl Component for Button {
if matches!(self.state, State::Pressed) {
#[cfg(feature = "haptic")]
if !matches!(self.haptic, HapticMode::Off) {
- play(HapticEffect::ButtonPress);
+ let _ = play(HapticEffect::ButtonPress);
}
self.set(ctx, State::Initial);
return Some(ButtonMsg::LongPressed);
### core/embed/rust/src/ui/layout_eckhart/firmware/action_bar.rs
@@ -1,11 +1,11 @@
+#[cfg(feature = "haptic")]
+use io::haptic::{self, HapticEffect};
use sys::time::Duration;
use super::super::component::{Button, ButtonMsg};
use super::{theme, HoldToConfirmAnim};
use crate::strutil::TString;
use crate::translations::TR;
-#[cfg(feature = "haptic")]
-use crate::trezorhal::haptic::{self, HapticEffect};
use crate::ui::component::{Component, Event, EventCtx, Timeout};
use crate::ui::geometry::{Alignment2D, Insets, Offset, Rect};
use crate::ui::shape::{self, Renderer};
@@ -305,7 +305,7 @@ impl ActionBar {
} else {
// Animations disabled, return confirmed
#[cfg(feature = "haptic")]
- haptic::play(HapticEffect::HoldToConfirm);
+ let _ = haptic::play(HapticEffect::HoldToConfirm);
return Some(ActionBarMsg::Confirmed);
}
}
@@ -319,7 +319,7 @@ impl ActionBar {
}
(ButtonMsg::LongPressed, true) => {
#[cfg(feature = "haptic")]
- haptic::play(HapticEffect::HoldToConfirm);
+ let _ = haptic::play(HapticEffect::HoldToConfirm);
return Some(ActionBarMsg::Confirmed);
}
(ButtonMsg::Clicked, false) => {
### core/embed/rust/src/ui/layout_eckhart/firmware/hold_to_confirm.rs
@@ -1,4 +1,6 @@
#[cfg(feature = "haptic")]
+use io::haptic;
+#[cfg(feature = "haptic")]
use pareen;
use sys::time::Duration;
@@ -8,8 +10,6 @@ use super::super::theme;
use super::constant::SCREEN;
use crate::strutil::TString;
use crate::time::Stopwatch;
-#[cfg(feature = "haptic")]
-use crate::trezorhal::haptic;
use crate::ui::component::{Component, Event, EventCtx};
use crate::ui::display::Color;
use crate::ui::geometry::{Alignment2D, Insets, Offset, Rect};
@@ -200,7 +200,7 @@ impl Component for HoldToConfirmAnim {
self.render_clipped_border(clip, top_gap, u8::MAX, target);
#[cfg(feature = "haptic")]
- haptic::play_custom(self.get_haptic(elapsed), 100);
+ let _ = haptic::play_custom(self.get_haptic(elapsed), 100);
}
AnimState::Finalizing { stopwatch } => {
let elapsed = stopwatch.elapsed();
### core/embed/rust/trezorhal.h
@@ -24,10 +24,6 @@
#include <io/button.h>
#endif
-#ifdef USE_HAPTIC
-#include <io/haptic.h>
-#endif
-
#ifdef USE_RGB_LED
#include <io/rgb_led.h>
#endifWhy this scored 15/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.