feat(core): define times for batt autolock delay
What changed, and why it matters
This commit is a straightforward feature addition for a future battery-powered Trezor device. It renames existing auto-lock delay limits to clearly distinguish between USB-powered and battery-powered modes, and adds separate storage and default values for battery auto-lock delays. There is no security fix or vulnerability here.
No security action required. Treat as normal feature/refactoring code review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces battery-specific auto-lock delay constants and storage helpers gated by utils.USE_POWER_MANAGER. It renames AUTOLOCK_DELAY_MINIMUM/MAXIMUM/DEFAULT to AUTOLOCK_DELAY_USB_*_MS and adds AUTOLOCK_DELAY_BATT_*_MS variants. _normalize_autolock_delay is parameterized so both USB and battery delays can be clamped to their respective ranges. Existing callers in device_menu.py and apply_settings.py are updated to use the renamed USB constants only.
Changed components
core/src/storage/device.pycore/src/apps/homescreen/device_menu.pycore/src/apps/management/apply_settings.pyInspect captured patch +47 / −12
diff --git a/core/src/apps/homescreen/device_menu.py b/core/src/apps/homescreen/device_menu.py
index d0a4c9c2c..66971a691 100644
--- a/core/src/apps/homescreen/device_menu.py
+++ b/core/src/apps/homescreen/device_menu.py
@@ -235,8 +235,8 @@ async def handle_device_menu() -> None:
trezorui_api.request_duration(
title=TR.auto_lock__title,
duration_ms=storage_device.get_autolock_delay_ms(),
- min_ms=storage_device.AUTOLOCK_DELAY_MINIMUM,
- max_ms=storage_device.AUTOLOCK_DELAY_MAXIMUM,
+ min_ms=storage_device.AUTOLOCK_DELAY_USB_MIN_MS,
+ max_ms=storage_device.AUTOLOCK_DELAY_USB_MAX_MS,
description=TR.auto_lock__description,
),
br_name=None,
diff --git a/core/src/apps/management/apply_settings.py b/core/src/apps/management/apply_settings.py
index 6e5ddfd29..f1400830b 100644
--- a/core/src/apps/management/apply_settings.py
+++ b/core/src/apps/management/apply_settings.py
@@ -117,9 +117,9 @@ async def apply_settings(msg: ApplySettings) -> Success:
storage_device.set_passphrase_always_on_device(passphrase_always_on_device)
if auto_lock_delay_ms is not None:
- if auto_lock_delay_ms < storage_device.AUTOLOCK_DELAY_MINIMUM:
+ if auto_lock_delay_ms < storage_device.AUTOLOCK_DELAY_USB_MIN_MS:
raise ProcessError("Auto-lock delay too short")
- if auto_lock_delay_ms > storage_device.AUTOLOCK_DELAY_MAXIMUM:
+ if auto_lock_delay_ms > storage_device.AUTOLOCK_DELAY_USB_MAX_MS:
raise ProcessError("Auto-lock delay too long")
await _require_confirm_change_autolock_delay(auto_lock_delay_ms)
storage_device.set_autolock_delay_ms(auto_lock_delay_ms)
diff --git a/core/src/storage/device.py b/core/src/storage/device.py
index 1c4d8c48e..2abb611e5 100644
--- a/core/src/storage/device.py
+++ b/core/src/storage/device.py
@@ -44,6 +44,8 @@ _DISABLE_HAPTIC_FEEDBACK = const(0x20) # bool (0x01 or empty)
_DISABLE_RGB_LED = const(0x21) # bool (0x01 or empty)
if utils.USE_THP:
_THP_PAIRED_CACHE = const(0x22) # bytes
+if utils.USE_POWER_MANAGER:
+ _AUTOLOCK_DELAY_BATT_MS = const(0x23) # int
SAFETY_CHECK_LEVEL_STRICT : Literal[0] = const(0)
@@ -56,12 +58,19 @@ if TYPE_CHECKING:
LABEL_MAXLENGTH = const(32)
if __debug__:
- AUTOLOCK_DELAY_MINIMUM = 10 * 1000 # 10 seconds
+ AUTOLOCK_DELAY_USB_MIN_MS = 10 * 1000 # 10 seconds
else:
- AUTOLOCK_DELAY_MINIMUM = 60 * 1000 # 1 minute
-AUTOLOCK_DELAY_DEFAULT = const(10 * 60 * 1000) # 10 minutes
+ AUTOLOCK_DELAY_USB_MIN_MS = 60 * 1000 # 1 minute
+
+AUTOLOCK_DELAY_USB_DEFAULT_MS = const(10 * 60 * 1000) # 10 minutes
# autolock intervals larger than AUTOLOCK_DELAY_MAXIMUM cause issues in the scheduler
-AUTOLOCK_DELAY_MAXIMUM = const(0x2000_0000) # ~6 days
+AUTOLOCK_DELAY_USB_MAX_MS = const(0x2000_0000) # ~6 days
+
+if utils.USE_POWER_MANAGER:
+ AUTOLOCK_DELAY_BATT_MIN_MS = 30 * 1000 # 30 seconds
+ AUTOLOCK_DELAY_BATT_DEFAULT_MS = const(40 * 1000) # 40 seconds
+ AUTOLOCK_DELAY_BATT_MAX_MS = const(10 * 60 * 1000) # 10 minutes
+
# Length of SD salt auth tag.
# Other SD-salt-related constants are in sd_salt.py
@@ -244,16 +253,20 @@ def set_flags(flags: int) -> None:
common.set(_NAMESPACE, _FLAGS, flags.to_bytes(4, "big"))
-def _normalize_autolock_delay(delay_ms: int) -> int:
- delay_ms = max(delay_ms, AUTOLOCK_DELAY_MINIMUM)
- delay_ms = min(delay_ms, AUTOLOCK_DELAY_MAXIMUM)
+def _normalize_autolock_delay(
+ delay_ms: int,
+ min_ms: int = AUTOLOCK_DELAY_USB_MIN_MS,
+ max_ms: int = AUTOLOCK_DELAY_USB_MAX_MS,
+) -> int:
+ delay_ms = max(delay_ms, min_ms)
+ delay_ms = min(delay_ms, max_ms)
return delay_ms
def get_autolock_delay_ms() -> int:
b = common.get(_NAMESPACE, _AUTOLOCK_DELAY_MS)
if b is None:
- return AUTOLOCK_DELAY_DEFAULT
+ return AUTOLOCK_DELAY_USB_DEFAULT_MS
else:
return _normalize_autolock_delay(int.from_bytes(b, "big"))
@@ -263,6 +276,28 @@ def set_autolock_delay_ms(delay_ms: int) -> None:
common.set(_NAMESPACE, _AUTOLOCK_DELAY_MS, delay_ms.to_bytes(4, "big"))
+if utils.USE_POWER_MANAGER:
+
+ def get_autolock_delay_battery_ms() -> int:
+ b = common.get(_NAMESPACE, _AUTOLOCK_DELAY_BATT_MS)
+ if b is None:
+ return AUTOLOCK_DELAY_BATT_DEFAULT_MS
+ else:
+ return _normalize_autolock_delay(
+ int.from_bytes(b, "big"),
+ min_ms=AUTOLOCK_DELAY_BATT_MIN_MS,
+ max_ms=AUTOLOCK_DELAY_BATT_MAX_MS,
+ )
+
+ def set_autolock_delay_battery_ms(delay_ms: int) -> None:
+ delay_ms = _normalize_autolock_delay(
+ delay_ms,
+ min_ms=AUTOLOCK_DELAY_BATT_MIN_MS,
+ max_ms=AUTOLOCK_DELAY_BATT_MAX_MS,
+ )
+ common.set(_NAMESPACE, _AUTOLOCK_DELAY_BATT_MS, delay_ms.to_bytes(4, "big"))
+
+
def next_u2f_counter() -> int:
return common.next_counter(_NAMESPACE, U2F_COUNTER, writable_locked=True)
Why 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.